Posts

Installing Python 2.7.13 on CentOS 6.5

This post describes how to install python 2.7 from source without affecting the existing python installation. It'll install the python2.7 in a separate location other than the standard location in CentOS. Install required packages:  yum -y update yum groupinstall -y 'development tools' yum install -y gcc zlib-devel bzip2-devel openssl-devel xz-libs wget Download and extract python source: wget http://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz  xz -d Python-2.7.13.tar.xz  tar -xvf Python-2.7.13.tar Create a directory where to install python e.g. ~/localpython: mkdir /home/nayan/localpython Go to extracted python source directory: cd Python-2.7.13 Run the configure: ./configure --prefix=/home/nayan/localpython --enable-shared Compile and install it:  make make altinstall Check if python installed by running python2.7 Make a link to Python library path: On 32 bit OS ln -s /home/nayan/localpython/lib/libpython2.7.so.1...

Generate model class in peewee

peewee is a very lighweight but rich orm for python. Often we need to generate model classes from an existing database table. It's possible to do this in peewee using an addon pwiz model generator . After installing pwiz, use the following command to generate model classes from an existing database. python -m pwiz -e mysql -u root -H localhost -P pesp_db > models.py Here is a brief description of the options: -e : name of the database engine e.g. mysql, postgresql -u : name of db user -H: name of db host The last parameter pesp_db in this case is the name of the database schema. If you run this command, you'll be prompted to provide your password. After providing the password, the model classes will be generated and written in the models.py

Generate a Excel file from Spring

We often need to generate a spreadsheet file from server and allow users to download the file. Here is how to do that with Spring using the Apache POI library. Dependency: We need to add the following dependencies in our spring application. Here is the Gradle dependencies. compile group: 'org.apache.poi', name: 'poi', version: '3.15' compile group: 'org.apache.poi', name: 'poi-ooxml', version: '3.15' Once we add the dependencies we'll be able to access the POI library in our application. Generate WorkBook We need to generate a Workbook object in POI. Here is a sample method that returns a Workbook object public Workbook downloadPersonList() throws IOException {     // sample list. this can be the output of a DB query     List<Person> personList = new ArrayList<>();       personList.add(new Person("nayan", 29));     personList.add(new Person("rafiq", 35)) ;     personList.add(ne...

Print a progress in Python

Here is how to do this: import sys sys.stdout.write("\r%d" % i) sys.stdout.flush() Writing '\r' will move the cursor back to the beginning of the line. If we want to print a %, we need to print an additional % like the folllowing: sys.stdout.write("\r%d%%" % i)

Automatic MongoDB backup in windows

It's a very good idea to take backups of our MongoDB periodically. We can use the following script to create a folder with current date time and dump the database in it. The script also runs 7zip and compresses the backup directory. When compression is done, this script will delete the backup directory. This will save a lot of disk space. @echo OFF :: This will create a timestamp like yyyy-mm-dd-hh-mm-ss. set BACKUPNAME=E:\mongo-db-backup set BACKUPNAME=%BACKUPNAME%\%DATE:~10,4%-%DATE:~4,2%-%DATE:~7,2%-%TIME:~0,2%-%TIME:~3,2%-%TIME:~6,2% @echo BACKUPNAME=%BACKUPNAME% :: Create a new directory md "%BACKUPNAME%" echo Running backup "%BACKUPNAME%" mongodump -h localhost -d stipend_icr -u stipend_icr_user -p pr0g0t1 -o "%BACKUPNAME%" REM ZIP the backup directory echo Running 7zip on backup "%BACKUPNAME%" "C:\Program Files\7-Zip\7z.exe" a -tzip "%BACKUPNAME%.zip" "%BACKUPNAME%" REM Delete the back...

Map GET parameters to Object in Spring

It's possbile to map GET parameters to an object in Spring. It's really useful when we need to pass lots of parameters as query string e.g. submitting a search form with many parameters. The following controller demonstrates this.   @RestController @RequestMapping("hello") public class MyController {     @RequestMapping(value = "data", method = RequestMethod.GET)     public MyRequest getData(MyRequest request){             return request;     }     public static class  MyRequest {         private String name;          private int age;         public String getName(){return this.name;}         public int getAge(){return this.name;}         public void setName(String name){this.name=name;}          public void setAge(int age){this.age=ag...

Pass state parameter from url in angular-ui-router

URL parameters are automatically sent as state parameters in angular-ui-router . All we need to do is declare the parameter names in state configuration. Here is an example of state configuration: .state('home.profile', {     url: '/profile?id',     templateUrl: 'templates/profile.html',     controller: 'ProfileCtrl' }) Here we've declared a parameter id in url attribute.  Now if we hit the url with parameter id e.g /profile?id=1234343 , w e'll be able to receive id=1234343 in state params.  We'll also be able to pass state parameter through ui-sre f lik e the following: <a ui-sref="home.profile({id: '1234343' })">Go to profile</a> Here is an example controller that demonstrates how to receive the parameter : .controller('ProfileCtrl', ['$scope', '$stateParams',     function ($scope, $stateParams) {         $scope.id = $stateParams.id; // id will be 1234...