Tuesday, June 19, 2018

Python - Installing virtualenv in Ubuntu

Use the below command to install virtualenv in Ubuntu:

$ sudo apt-get install virtualenv
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  python3-virtualenv
The following NEW packages will be installed:
  python3-virtualenv virtualenv
0 upgraded, 2 newly installed, 0 to remove and 9 not upgraded.
Need to get 47.8 kB of archives.
After this operation, 171 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://in.archive.ubuntu.com/ubuntu bionic/universe amd64 python3-virtualenv all 15.1.0+ds-1.1 [43.4 kB]
Get:2 http://in.archive.ubuntu.com/ubuntu bionic/universe amd64 virtualenv all 15.1.0+ds-1.1 [4,476 B]
Fetched 47.8 kB in 1s (49.2 kB/s)     
Selecting previously unselected package python3-virtualenv.
(Reading database ... 205693 files and directories currently installed.)
Preparing to unpack .../python3-virtualenv_15.1.0+ds-1.1_all.deb ...
Unpacking python3-virtualenv (15.1.0+ds-1.1) ...
Selecting previously unselected package virtualenv.
Preparing to unpack .../virtualenv_15.1.0+ds-1.1_all.deb ...
Unpacking virtualenv (15.1.0+ds-1.1) ...
Setting up python3-virtualenv (15.1.0+ds-1.1) ...
Processing triggers for man-db (2.8.3-2) ...
Setting up virtualenv (15.1.0+ds-1.1) ...
$ 


Refer:

Python - Install Jinja - template engine

Install Jinja2 templating engine using pip:

$ sudo pip install Jinja2
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting Jinja2
  Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)
    100% |████████████████████████████████| 133kB 374kB/s 
Collecting MarkupSafe>=0.23 (from Jinja2)
  Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz
Installing collected packages: MarkupSafe, Jinja2
  Running setup.py install for MarkupSafe ... done
Successfully installed Jinja2-2.10 MarkupSafe-1.0
$ 


Check Installation:

$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import jinja2 
>>> exit()
$

Installation successful.

Python - PDF reports module - weasyprint - install

Follow the below steps to install weasyprint module for python on Ubuntu 18.04:

Step 1: Install the dependencies for weasyprint:

Install dependencies using the command:
sudo apt-get install build-essential python3-dev python3-pip python3-setuptools python3-wheel python3-cffi libcairo2 libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info



Step 2: Install using pip :

$ sudo pip install weasyprint
[sudo] password for ubuntu: 
The directory '/home/ubuntu/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/ubuntu/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting weasyprint
  Downloading https://files.pythonhosted.org/packages/7e/4c/cf2ec7abf7f84a2d1325d01dcac1d4bcb77f41117101fe564eb76952c65f/WeasyPrint-0.42.3.tar.gz (399kB)
    100% |████████████████████████████████| 409kB 1.4MB/s 
Collecting Pyphen>=0.8 (from weasyprint)
  Downloading https://files.pythonhosted.org/packages/dd/c4/74859f895e2361d92cfbb6208ea7afd06c2f1f05c491da71cbd7ce3887be/Pyphen-0.9.4-py2.py3-none-any.whl (1.9MB)
    100% |████████████████████████████████| 2.0MB 516kB/s 
Collecting cairocffi>=0.5 (from weasyprint)
  Downloading https://files.pythonhosted.org/packages/7a/2f/78179fea3413322fd20be407897738e5569e876e7cb6e4647a79b1ffd8dd/cairocffi-0.8.1.tar.gz (79kB)
    100% |████████████████████████████████| 81kB 2.9MB/s 
Collecting cffi>=0.6 (from weasyprint)
  Downloading https://files.pythonhosted.org/packages/14/dd/3e7a1e1280e7d767bd3fa15791759c91ec19058ebe31217fe66f3e9a8c49/cffi-1.11.5-cp27-cp27mu-manylinux1_x86_64.whl (407kB)
    100% |████████████████████████████████| 409kB 1.5MB/s 
Collecting cssselect2>=0.1 (from weasyprint)
  Downloading https://files.pythonhosted.org/packages/12/e2/91fcd4cd32545beec6e11628d64d3e20f11b5a95dd1ccf3216fd69f176b7/cssselect2-0.2.1-py2.py3-none-any.whl
Collecting html5lib>=0.999999999 (from weasyprint)
  Downloading https://files.pythonhosted.org/packages/a5/62/bbd2be0e7943ec8504b517e62bab011b4946e1258842bc159e5dfde15b96/html5lib-1.0.1-py2.py3-none-any.whl (117kB)
    100% |████████████████████████████████| 122kB 2.5MB/s 
Collecting pdfrw>=0.4 (from weasyprint)
  Downloading https://files.pythonhosted.org/packages/c0/84/af442c4458756bb0c0d2424102d1200616f3ff9b82c48aaa130e08549bf6/pdfrw-0.4-py2.py3-none-any.whl (69kB)
    100% |████████████████████████████████| 71kB 2.9MB/s 
Collecting tinycss2>=0.5 (from weasyprint)
  Downloading https://files.pythonhosted.org/packages/88/95/0ec73db7fc638ec000e662936ffe1a4e1eaa22f2861c8de18b1597c42584/tinycss2-0.6.1-py2.py3-none-any.whl (61kB)
    100% |████████████████████████████████| 71kB 3.6MB/s 
Collecting CairoSVG<2.0.0,>=1.0.20 (from weasyprint)
  Downloading https://files.pythonhosted.org/packages/d9/9b/c241990c86faa9e52a01f0570bba4247ba0f3d66eae2607e179cb9ae773a/CairoSVG-1.0.22.tar.gz
Collecting pycparser (from cffi>=0.6->weasyprint)
  Downloading https://files.pythonhosted.org/packages/8c/2d/aad7f16146f4197a11f8e91fb81df177adcc2073d36a17b1491fd09df6ed/pycparser-2.18.tar.gz (245kB)
    100% |████████████████████████████████| 256kB 2.1MB/s 
Requirement already satisfied: six>=1.9 in ./.local/lib/python2.7/site-packages (from html5lib>=0.999999999->weasyprint)
Collecting webencodings (from html5lib>=0.999999999->weasyprint)
  Downloading https://files.pythonhosted.org/packages/f4/24/2a3e3df732393fed8b3ebf2ec078f05546de641fe1b667ee316ec1dcf3b7/webencodings-0.5.1-py2.py3-none-any.whl
Installing collected packages: Pyphen, pycparser, cffi, cairocffi, webencodings, tinycss2, cssselect2, html5lib, pdfrw, CairoSVG, weasyprint
  Running setup.py install for pycparser ... done
  Running setup.py install for cairocffi ... done
  Running setup.py install for CairoSVG ... done
  Running setup.py install for weasyprint ... done
Successfully installed CairoSVG-1.0.22 Pyphen-0.9.4 cairocffi-0.8.1 cffi-1.11.5 cssselect2-0.2.1 html5lib-1.0.1 pdfrw-0.4 pycparser-2.18 tinycss2-0.6.1 weasyprint-0.42.3 webencodings-0.5.1
$

Check the installation:

$ python
Python 2.7.15rc1 (default, Apr 15 2018, 21:51:34) 
[GCC 7.3.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import weasyprint
>>> exit()
$ 

Installation is successful.

Sunday, June 17, 2018

JavaScript - built in method on Objects

Below code demonstrates the usage of built in methods and properties on Objects:

var person = {
  name: 'Sandra',
  age: 27
}

//deleting properties
// checks if the field name is present in the object person
console.log('name' in person);

//determine if they are there 
// deletes the field from the object
delete person.name;

console.log(person);



var school = {
  name: 'Sadhana Infinity International School',
  age: 1,
  place: "Nallagandla",
  greet: function () {
    return "Welcome to "+this.name;
  }
}

//demonstrates usage of for loop
// to access fields and values in Object
for (var field in school) {
  console.log(field +":"+school[field]);
}





Refer:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete

Practice at:
http://jsbin.com

JavaScript - Objects - defineProperty

The below code demonstrates usage of defineProperty in JS:

var BankAccount = {
    cash: 12000,
    _name:"Default",
    withdraw: function(amount){
      this.cash -= amount;
      console.log('Withdrew '+ amount + ', new cash reserve is: '+ this.cash);
    }
};


//BankAccount.withdraw(1000);


Object.defineProperty(BankAccount, 'deposit',{
   value: function(amount) {
     this.cash += amount;
   }
});


BankAccount.deposit(3000);
BankAccount.withdraw(1000);

/*
Object.defineProperty(BankAccount, 'name',{
  value : 'ID000-1',
  writable: true 
  // by default, properties created using defineProperty = readOnly
});


console.log(BankAccount.name);
*/
BankAccount.name = 'Sandra';
console.log(BankAccount.name);


Object.defineProperty(BankAccount,'name',{
  get: function() {
  return this._name;
}  ,
 set: function(name) {
   this._name=name;
 }
  })

  

BankAccount.name = 'Cassandra';

console.log(BankAccount.name);

Practice at:
http://jsbin.com

Saturday, June 16, 2018

JavaScript - Simulate POST request from JS Console

Below code simulates a POST request from a JavaScript console:

var xhr = new XMLHttpRequest();
xhr.open('POST','https://mathbostock.execute-api.us-east-1.amazonaws.com/dev/random-items');
xhr.onreadystatechange = function(event) {
  console.log(event.target.response);
}

xhr.send();


Practice at:
https://codepen.io

JavaScript - Object - this - bind, call, apply

The below code demonstrates the usage of the following object methods:
  • bind
  • call
  • apply

// here this refers to the window object
function myFunc(message){
  console.log(message+" "+this);
}


var obj = {
  objectFunction:myFunc
};

var Person = {
  name: "Cassandra",
  age: 27
}

// Observe the extra paranthesis in Bind
obj.objectFunction.bind(Person,"Bind says - Hey there ")(); 
// Observe that there are no extra paranthesis
obj.objectFunction.call(Person,"Call says - Hey there ");
// In apply parameters are passed using an array
obj.objectFunction.apply(Person,["Apply says - Hey there "]);




Refer:

  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Function/bind
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

Practice at:
http://jsbin.com