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