Sunday, October 22, 2017

set in python

#set doesnt have index , it contains unique values

>>> var
set([3, 1, 2, 'john'])

use update method to add multiple item to set
>>> var.update({5,6})
>>> var
set([1, 2, 3, 5, 6, 'john'])

use add to add single item to set
>>> var.add(9)
>>> var
set([1, 2, 3, 5, 6, 7, 9, 'john'])

remove item with 9 but gives error if 9 doesnt exist
>>> var.remove(9)
>>> var
set([1, 2, 3, 5, 6, 7, 'john'])

Use discard to delete 8 from set and it wont spit out error if it doesnt exist
var.discard(8)


# gives union of two sets
set1 | set 2
OR
set1.union(set2)

# gives difference
set1 - set2
OR
set1.difference(set2)

# things that is uniqe to a set
set1 ^ set2
or
set1.symmetric_difference(set2)
example:

>>> cd1={1,2,3,4}
>>> cd2={2,3,5,6}
>>> cd1 ^ cd2
set([1, 4, 5, 6])


# gives intersection
set1 & set2
set1.intersection(set2)

Enumerate in python

Enumerate function takes ordered iterable like string,list,tuple and gives items and its index

 list(enumerate("oranges"))
>>> [(0, 'o'), (1, 'r'), (2, 'a'), (3, 'n'), (4, 'g'), (5, 'e'), (6, 's')]

#!/usr/bin/python3
def main():

    for i,j in list(enumerate("oranges")):
        print(i,j)

if __name__ == "__main__":
    main()

#output
0 o
1 r
2 a
3 n
4 g
5 e
6 s




Tuples

>>> tup=(1,"apple",["jim","sammy"])
>>> tup
(1, 'apple', ['jim', 'sammy'])

# Tuples is immutable / unchangeable
>>> tup[0]=1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment

#The list inside the tuple can be changed though!!

>>> tup[2][1]="tommy"
>>> tup
(1, 'apple', ['jim', 'tommy'])




   

python list and dictionary

#tutorial blog after tihar oct 22/kartik 5

To convert string to a list use split() method
"this is string".split()
>>> "this-is-string".split("-")
['this', 'is', 'string']


join the strings in a list
>>> "".join(["sdf","asdf"])
'sdfasdf'


>>> mylist
['urgen', 'sherpa', 'is', 'persons', 'name', 'w', 'a', 't']
>>> mylist[-3:]=["".join(mylist[-3:])]
>>> mylist
['urgen', 'sherpa', 'is', 'persons', 'name', 'wat']



mydict={"fname":"john","lname":"doe"}
To change keyvalue for key "lname" to "Foe"
and add extra key value - "age"=21

mydict.update({"lname":"Foe","age":21})

result: >>> {'fname': 'john', 'age': 21, 'lname': 'Foe'}

Packing and Unpacking the dictionaries

def main():

    def packer(**kwargs):
        print(kwargs)
     
    def unpacker(fname=None, lname=None):
        if fname and lname:
            print("{} {}".format(fname,lname))
        else:
            print("missing fname or lname")
         
         
    packer(name="urgen",age=22)
    unpacker(**{"fname":"kenneth","lname":"love"})
 
if __name__ ==  "__main__":
    main()
   

def main():

    var={"fname":"urgne","lname":"sherpa","address":"ktm"}
    print("#keys using method 1")
    for k in var:
        #returns the keys
        print(k)
    #keys
    print("#keys using method 2")  

    for k in var.keys():
        #return keys
        print(k)
    #values
    print("#values")
    for k in var.values():
        #returns the key-value individual tuple item
        print(k)
    print("#key and values")
    for i in var.items():
        print(i)
   
if __name__ ==  "__main__":
    main()

Thursday, May 25, 2017

python mysql connect

sudo apt-get install libmysqlclient-dev
pip install MySQL-python

#!/usr/bin/python
import MySQLdb
db = MySQLdb.connect("localhost","root","p@ssowr9d","store_db")
cursor = db.cursor()
sql = "select * from food_tbl"
try:
    cursor.execute(sql)
    #data = cursor.fetchone()
    data = cursor.fetchall()
    for row in data:
        id = row[0]
        name = row[1]
        catagory = row[2]
        price = row[3]
        print "%d %s %s %d " % (id, name, catagory, price)
except:
    print "Error: unable to fetch data"

db.close()

Tuesday, May 23, 2017

python pip and virtualenv and virtualenvwrappers

Virtualenv way that you can separate different python enviroment for different porjects
eg. different package versions for different projects

pip install virtualenv
pip install pyOpenSSL ndg-httpsclient pyasn1

You may need runtime/development packages for python and openssl as well in order for the build to succeed, e.g. python-dev libssl-dev libffi-dev on Ubuntu 14.04.

pip install --upgrade pip
pip list

#To export all package and version numbers for other projects

pip freeze  --local >> requirements.txt
# in new enviroment import/install package version
pip install -r requirements.txt


deactivate  # to getout of VE


# specify specific version of python to use
virtualenv -p /usr/bin/python2.7 py27_env
source py27_env/bin/activate

pip install -r requirements.txt


#virtualenvwrapper  allows to switch between different virtualenv
# Setup:

pip install virtualenvwrapper
path to virtualenvwrapper.sh  is mostly /usr/local/bin/virtualenvwrapper.sh

#  1. Create a directory to hold the virtual environments.
#     (mkdir $HOME/.virtualenvs).
#  2. Add a line like "export WORKON_HOME=$HOME/.virtualenvs"
#     to your .bashrc.
#  3. Add a line like "source /path/to/this/file/virtualenvwrapper.sh"
#     to your .bashrc.
#  4. Run: source ~/.bashrc
#  5. Run: workon
#  6. A list of environments, empty, is printed.
#  7. Run: mkvirtualenv temp
#  8. Run: workon
#  9. This time, the "temp" environment is included.
# 10. Run: workon temp
# 11. The virtual environment is activated.

#Create virtualenviroment

mkvirtualenv enviro100
mkvirtualenv enviro200

user1@localhost ~ $ workon
enviro100
enviro200

#To use enviroment named enviro200

workon enviro200