Saturday, October 29, 2016

SPA using Angular

Single Page Application using Angular



  • Angular and knockout comes with data binding
  • Backbone is also quite easy to grasp.
  • Angular is very powerful and deals with complex models

Original blog

Wednesday, October 26, 2016


1. Django project files significance

a. => enables the django project settings path to the environment
b. => this file contains the settings of django project like debug mode, database settings, templates path, static_url path etc.,
c. this file is the controller file which maps the web url to the respective view function/class
d. file => this file will have the environment settings like virtual environment site-packages path, django project path. path of this should be included in the apache configuration file apache2.conf
e. => this file will have the information models and members of the models
f. => this file will have the views functions/classes

2. Django commands

django-admin start-project my_pro => Creates django project
django-admin startapp myapp => creates django application inside the django project
python ./ runserver => runs the development server
python ./ makemigrations => creates the database migration scripts
python ./ migrate => applies the migrations
python ./ inspectdb >  => Creates the models wrt to the already existing database
python ./ collectstatic => collects all the static files in the django project
python ./ dbshell  => Opens the database shell
python ./ shell => opens the django/python shell

Thursday, July 28, 2016

Map Vs List Comprehension

Case 1. Map Vs List comprehension for user defined function
:~ sadatravu$ python -mtimeit -s'xs = xrange(10)' 'def ch(x): return x+10' 'map(ch ,xs)'
1000000 loops, best of 3: 1.3 usec per loop
:~ sadatravu$ python -mtimeit -s'xs = xrange(10)' 'def ch(x): return x+10' '[ch(x) for x in xs]'
1000000 loops, best of 3: 1.39 usec per loop

Case 2. Map Vs List comprehension for inbuilt function
:~ sadatravu$ python -mtimeit -s'xs = xrange(10)'  'map(hex,xs)'
1000000 loops, best of 3: 0.926 usec per loop
:~ sadatravu$ python -mtimeit -s'xs = xrange(10)'  '[hex(x) for x in xs]'
1000000 loops, best of 3: 1.14 usec per loop

Case 3. Map Vs List comprehension for function built by lambda
python -mtimeit -s'xs = xrange(10)'  'map(lambda x: x+10,xs)'
1000000 loops, best of 3: 1.29 usec per loop
python -mtimeit -s'xs = xrange(10)'  '[lambda x: x+10 for x in xs]'
1000000 loops, best of 3: 0.958 usec per loop

Time taken by map function is lesser in Case1&2, whereas higher in Case3

                                                        Map                     ListComprehension

1. User defined function                  1.3 usec                      1.39 usec

2. Inbuilt function                           0.926 usec                  1.14 usec   

3. function built by lambda            1.29 usec                    0.958 usec

Monday, July 18, 2016

MySQL for beginners

Install mysql-server and mysql-client  using the command for example in ubuntu sudo apt-get install mysql-server mysql-client, this will prompt for admin password, provide the password and remember the password.

logging into the mysql shell for id “root” and password “password"

mysql -u root -ppassword

To list the available databases
> show databases; 

To create database
> create database if not exists django_tut; 

Selecting database
> use django_tut

To delete database
> drop database if exists django_tut;

To list down all the tables in the database
> Show Tables;

Creating a table
> create table if not exists ex_table( p_id INT, P_code varchar 20)

verifying the table
>describe ex_table;

deleting the table “ex_table1"
> drop table if exists ex_table1;

Inserting a records in the table ex_table
> insert into ex_table value (100, “hundred”)
> insert into ex_table value (200, “two hundred”)

Inserting a records in the table ex_table1
> insert into ex_table value (100, “hundred”)
> insert into ex_table value (300, “three hundred”)

Updating a record
>update ex_table set p_code="HUNDRED" where p_id=100

Querying the table
> select * from ex_table;
Querying for a particular column in the table
> select p_id from ex_table;
Querying based on condition
> select p_id from ex_table where p_id =100;

Alter table adding a column in the table
> alter table ex_table add column p_description varchar(50);

>describe table;

Joining a table
> select * from ex_table join ex_table1 on ex_table.p_id = ex_table1.p_id;

taking backup (below command prompt for password)
$mysqldump -u root -p —databases django_tut > ~/django_tut_bkup.sql
restoring backup(below command prompt for password)
$mysql -u root -p django_tut < ~/django_tut_bkup.sql

Monday, June 13, 2016

GIT tag basics

  • Clone the repository => git clone Tut
  • Goto the checkout repository  =>cd Tut/
  • To list all the available tags=> git tag -l 
  • To list all the available branches git branch
  • Creating a tag “first_tag” with the commit revision => git tag -a first_tag 673b992
  • Pushing all the created tags => git push origin --tags
  • Checking out the tag “first_tag” by creating a local branch “first_tag_branch” => git checkout -b first_tag_branch first_tag

Thursday, January 21, 2016

[Hadoop] Importing/Exporting Database MySQL <==> HDFS using Sqoop

  • Import all MySQL tables from "training" database  into the HDFS directory "hdfsdir"
$]sqoop import-all-tables --connect jdbc:mysql://localhost/training --username training --password training --warehouse-dir hdfsdir

  • Loginto the hive shell
  • create the external table(without location) with the schema as of the MySQL database table "movies"
hive> external table movies(eid int, ename String, code String) row format delimited fields terminated by ',';
  • Load data from the hdfs location into the table movies
hive>load data inpath 'hdfs://localhost/user/training/hdfsdir/Movies/part-*' into table movies;
  • Run Hive Queries

hive>select  eid, name, codefrom movies where eid >1000;
  • Overwrite table "movies" with a filter, this will delete "movies" directory in "/user/hive/warehouse" and recreates the directory "/user/hive/warehouse/movies" with a file 0000000_0. Contents of this file would the records of the filter
hive> insert overwrite table movies select * from movies where eid>1670; 

  • Create a table in MySQL with the same scheme

mysql>  CREATE TABLE movies1 ( eid int(11) NOT NULL, ename char(255), code char(255));

  • export data from the HIVE/HDFS into the MySQL
$] sqoop export --connect jdbc:mysql://localhost/training --username training --password training --table movies1 -m 4 --export-dir /user/hive/warehouse/movies

Wednesday, January 13, 2016

BASH Script to Get the Child Process IDs recursively.

function return_child_pid
   for p in `ps -ef | awk '$3=="'"$pd"'" {print $2}'`;
      return_child_pid $p
   if [ $count == 0 ]; then
     echo $1
     return $1

return_child_pid $1