Python Training at VMWare Pune - Day 2

Dec 13-15, 2017 Vikrant Patil

These notes are available online at http://notes.pipal.in/2017/vmware-nov-python

© Pipal Academy LLP

Day 1 | Day 2 | Day 3

problem:

  • Write a csvparser which parses data written in csv format and loads it in memory as 2 dimensional list.
In [1]:
def csvparser(filename):
    return [line.split(",") for line in open(filename)]
In [4]:
csvparser("data1.csv")
Out[4]:
[['A1', 'B1', 'C1\n'],
 ['A2', 'B2', 'C2\n'],
 ['A3', 'B3', 'C3\n'],
 ['A4', 'B4', 'C4\n']]

with statement

In [6]:
with open("numbers.txt", "a") as f:
    f.write("ten\n")
    f.write("nine\n")

import cat
cat.cat(["numbers.txt"])
one
two
three
four
five
six
ten
nine
ten
nine

Lets make our parsers and writers generic

In [39]:
def genericparser(file, delimiter=","):
    return [line.strip().split(delimiter) for line in open(file)]
In [40]:
def genericwriter(data, filename, delimiter=","):
    with open(filename, "w") as fhandle:
        for row in data:
            fhandle.write(delimiter.join(row) + "\n")
    
In [16]:
data = [[str(i*j) for i in range(1,11)] for j in range(1,6)]
In [17]:
data
Out[17]:
[['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
 ['2', '4', '6', '8', '10', '12', '14', '16', '18', '20'],
 ['3', '6', '9', '12', '15', '18', '21', '24', '27', '30'],
 ['4', '8', '12', '16', '20', '24', '28', '32', '36', '40'],
 ['5', '10', '15', '20', '25', '30', '35', '40', '45', '50']]
In [18]:
genericwriter(data, "tables.tsv", delimiter="\t")
In [19]:
!python cat.py tables.tsv
1	2	3	4	5	6	7	8	9	10
2	4	6	8	10	12	14	16	18	20
3	6	9	12	15	18	21	24	27	30
4	8	12	16	20	24	28	32	36	40
5	10	15	20	25	30	35	40	45	50

how do you transpose data?

In [27]:
data[0] # 0th row
Out[27]:
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
In [28]:
data[0][0] # item from 0th row and 0th column
Out[28]:
'1'
In [23]:
#data[rownumber][columnnumber]
In [24]:
[data[rownumber][0] for rownumber in range(len(data))]
Out[24]:
['1', '2', '3', '4', '5']
In [26]:
[data[rownumber][1] for rownumber in range(len(data))]
Out[26]:
['2', '4', '6', '8', '10']
In [29]:
rows = len(data)
In [30]:
cols = len(data[0])
In [31]:
data
Out[31]:
[['1', '2', '3', '4', '5', '6', '7', '8', '9', '10'],
 ['2', '4', '6', '8', '10', '12', '14', '16', '18', '20'],
 ['3', '6', '9', '12', '15', '18', '21', '24', '27', '30'],
 ['4', '8', '12', '16', '20', '24', '28', '32', '36', '40'],
 ['5', '10', '15', '20', '25', '30', '35', '40', '45', '50']]
In [32]:
[data[rownum][0] for rownum in range(rows)]
Out[32]:
['1', '2', '3', '4', '5']
In [33]:
[data[rownum][1] for rownum in range(rows)]
Out[33]:
['2', '4', '6', '8', '10']
In [34]:
[[data[rownum][colnum] for rownum in range(rows)]  for colnum in range(cols) ]
Out[34]:
[['1', '2', '3', '4', '5'],
 ['2', '4', '6', '8', '10'],
 ['3', '6', '9', '12', '15'],
 ['4', '8', '12', '16', '20'],
 ['5', '10', '15', '20', '25'],
 ['6', '12', '18', '24', '30'],
 ['7', '14', '21', '28', '35'],
 ['8', '16', '24', '32', '40'],
 ['9', '18', '27', '36', '45'],
 ['10', '20', '30', '40', '50']]
In [35]:
def transpose(data):
    rows = len(data)
    cols = len(data[0])
    return [[data[rownum][colnum] for rownum in range(rows)]  for colnum in range(cols) ]
In [36]:
genericwriter(transpose(data), "tables.tsv", delimiter="\t")
In [37]:
!python cat.py tables.tsv
1	2	3	4	5
2	4	6	8	10
3	6	9	12	15
4	8	12	16	20
5	10	15	20	25
6	12	18	24	30
7	14	21	28	35
8	16	24	32	40
9	18	27	36	45
10	20	30	40	50

In [41]:
genericparser("tables.tsv", delimiter="\t")
Out[41]:
[['1', '2', '3', '4', '5'],
 ['2', '4', '6', '8', '10'],
 ['3', '6', '9', '12', '15'],
 ['4', '8', '12', '16', '20'],
 ['5', '10', '15', '20', '25'],
 ['6', '12', '18', '24', '30'],
 ['7', '14', '21', '28', '35'],
 ['8', '16', '24', '32', '40'],
 ['9', '18', '27', '36', '45'],
 ['10', '20', '30', '40', '50']]
In [42]:
csvparser = lambda filename: genericparser(filename, delimiter=",")
In [43]:
csvparser("data1.csv")
Out[43]:
[['A1', 'B1', 'C1'],
 ['A2', 'B2', 'C2'],
 ['A3', 'B3', 'C3'],
 ['A4', 'B4', 'C4']]
In [44]:
def csvparser(filename):
    return genericparser(filename, delimiter=",")

Writing to standard error and output

In [45]:
import sys
In [46]:
sys.stdout.write("Hello this is just output!")
Hello this is just output!
In [47]:
sys.stderr.write("Something went wrong !!")
Something went wrong !!

Dictionaries

In [48]:
person = {"name":"lewis carrol",
         "books":["ALice in wonderland", "Looking through the glass"],
         "language":"English"} 
In [49]:
person['name']
Out[49]:
'lewis carrol'
In [50]:
person['name'] = "lewis"
In [51]:
person
Out[51]:
{'books': ['ALice in wonderland', 'Looking through the glass'],
 'language': 'English',
 'name': 'lewis'}
In [52]:
del person['language']
In [53]:
person
Out[53]:
{'books': ['ALice in wonderland', 'Looking through the glass'],
 'name': 'lewis'}
In [54]:
person['language']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-54-808e22d5936d> in <module>()
----> 1 person['language']

KeyError: 'language'
In [55]:
person.get("language", "english")
Out[55]:
'english'
In [56]:
person.get("name", "unknown")
Out[56]:
'lewis'
In [57]:
person['books'][0]
Out[57]:
'ALice in wonderland'
In [58]:
person['books'][1]
Out[58]:
'Looking through the glass'

grub conf on my laptop looks like this

In [59]:
!python cat.py /etc/default/grub
# If you change this file, run 'update-grub' afterwards to update
# /boot/grub/grub.cfg.
# For full documentation of the options in this file, see:
#   info -f grub -n 'Simple configuration'

GRUB_DEFAULT=0
GRUB_HIDDEN_TIMEOUT=0
GRUB_HIDDEN_TIMEOUT_QUIET=true
GRUB_TIMEOUT=10
GRUB_DISTRIBUTOR=`lsb_release -i -s 2> /dev/null || echo Debian`
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
GRUB_CMDLINE_LINUX=""

# Uncomment to enable BadRAM filtering, modify to suit your needs
# This works with Linux (no patch required) and with any kernel that obtains
# the memory map information from GRUB (GNU Mach, kernel of FreeBSD ...)
#GRUB_BADRAM="0x01234567,0xfefefefe,0x89abcdef,0xefefefef"

# Uncomment to disable graphical terminal (grub-pc only)
#GRUB_TERMINAL=console

# The resolution used on graphical terminal
# note that you can use only modes which your graphic card supports via VBE
# you can see them in real GRUB with the command `vbeinfo'
#GRUB_GFXMODE=640x480

# Uncomment if you don't want GRUB to pass "root=UUID=xxx" parameter to Linux
#GRUB_DISABLE_LINUX_UUID=true

# Uncomment to disable generation of recovery mode menu entries
#GRUB_DISABLE_RECOVERY="true"

# Uncomment to get a beep at grub start
#GRUB_INIT_TUNE="480 440 1"

Example : parse conf file and load as dictionary

In [68]:
def confreader(file):
    data = {}
    for line in open(file):
        if line.startswith("#") or line.strip()=="":
            continue
        items = line.strip().split("=")
        data[items[0]] = "".join(items[1:])
    return data
In [69]:
confreader("/etc/default/grub")
Out[69]:
{'GRUB_CMDLINE_LINUX': '""',
 'GRUB_CMDLINE_LINUX_DEFAULT': '"quiet splash"',
 'GRUB_DEFAULT': '0',
 'GRUB_DISTRIBUTOR': '`lsb_release -i -s 2> /dev/null || echo Debian`',
 'GRUB_HIDDEN_TIMEOUT': '0',
 'GRUB_HIDDEN_TIMEOUT_QUIET': 'true',
 'GRUB_TIMEOUT': '10'}
In [66]:
confdata = confreader("/etc/default/grub")
In [67]:
confdata['GRUB_CMDLINE_LINUX_DEFAULT']
Out[67]:
'"quiet splash"'
In [71]:
digits = [("one",1), ("two",2), ("three",3)]
In [72]:
dict(digits)
Out[72]:
{'one': 1, 'three': 3, 'two': 2}
In [73]:
names = ["Anand", "Nouful", "David", "Lewis", "Ronbinson"]
place = ["India", "India", "USA", "UK", "USA"]
In [75]:
d = dict(zip(names, place))

Iterating over dictionaries

In [76]:
for item in d:
    print(item)
Anand
Nouful
David
Lewis
Ronbinson
In [77]:
for key in d.keys():
    print(key, d[key])
Anand India
Nouful India
David USA
Lewis UK
Ronbinson USA
In [78]:
for value in d.values():
    print(value)
India
India
USA
UK
USA
In [80]:
for key,value in d.items():
    print(key, value)
Anand India
Nouful India
David USA
Lewis UK
Ronbinson USA
In [81]:
items = ["Pencil", "Colorbox", "Pen", "Eraser"]
prices = [10, 25, 12, 5]
In [82]:
cart = dict(zip(items, prices))
In [83]:
cart
Out[83]:
{'Colorbox': 25, 'Eraser': 5, 'Pen': 12, 'Pencil': 10}
In [84]:
for item, price in cart.items():
    print(item.rjust(10), price)
print("-"*20)
print("Total".rjust(10), sum(cart.values()))
    Pencil 10
  Colorbox 25
       Pen 12
    Eraser 5
--------------------
     Total 52

problem :

  • Write a program to count frequency of words from given file
In [86]:
%%file words.txt
one
one two
one two three
one two three four
one two three four five
one two three four five six
six seven eight nine ten
seven eight nine ten
eight nine ten
nine ten
ten
Overwriting words.txt
In [87]:
d
Out[87]:
{'Anand': 'India',
 'David': 'USA',
 'Lewis': 'UK',
 'Nouful': 'India',
 'Ronbinson': 'USA'}
In [88]:
"alice" in ["alice", "alex"]
Out[88]:
True
In [89]:
"Anand" in d
Out[89]:
True
In [91]:
"Anand" in d.keys()
Out[91]:
True
In [92]:
"India" in d.values()
Out[92]:
True
In [93]:
def getwords(filename):
    return open(filename).read().split()
In [94]:
words = getwords("words.txt")
In [95]:
words
Out[95]:
['one',
 'one',
 'two',
 'one',
 'two',
 'three',
 'one',
 'two',
 'three',
 'four',
 'one',
 'two',
 'three',
 'four',
 'five',
 'one',
 'two',
 'three',
 'four',
 'five',
 'six',
 'six',
 'seven',
 'eight',
 'nine',
 'ten',
 'seven',
 'eight',
 'nine',
 'ten',
 'eight',
 'nine',
 'ten',
 'nine',
 'ten',
 'ten']
In [96]:
def wordfreq(words):
    freq = {}
    
    for word in words:
        if word in freq:
            freq[word] += 1
        else:
            freq[word] = 1
    return freq
In [97]:
wordfreq(words)
Out[97]:
{'eight': 3,
 'five': 2,
 'four': 3,
 'nine': 4,
 'one': 6,
 'seven': 2,
 'six': 2,
 'ten': 5,
 'three': 4,
 'two': 5}
In [98]:
def wordfreq1(words):
    freq  = {}
    for w in words:
        freq[w] = freq.get(w, 0) + 1
    return freq
In [99]:
wordfreq1(words)
Out[99]:
{'eight': 3,
 'five': 2,
 'four': 3,
 'nine': 4,
 'one': 6,
 'seven': 2,
 'six': 2,
 'ten': 5,
 'three': 4,
 'two': 5}
In [100]:
def wordfreq2(words):
    freq = {}
    unique = set(words)
    for w in unique:
        freq[w] = words.count(w)
    return freq
In [102]:
def wordfreq3(words):
    freq = dict(zip(words, [0]*len(words)))
    for w in words:
        freq[w] += 1
    return freq
In [103]:
wordfreq3(words)
Out[103]:
{'eight': 3,
 'five': 2,
 'four': 3,
 'nine': 4,
 'one': 6,
 'seven': 2,
 'six': 2,
 'ten': 5,
 'three': 4,
 'two': 5}
In [104]:
freq = wordfreq3(words)
In [105]:
for key in freq:
    print(key, freq[key])
one 6
two 5
three 4
four 3
five 2
six 2
seven 2
eight 3
nine 4
ten 5
In [106]:
for key in sorted(freq):
    print(key, freq[key])
eight 3
five 2
four 3
nine 4
one 6
seven 2
six 2
ten 5
three 4
two 5
In [107]:
for key in sorted(freq, key=lambda k:freq[k]):
    print(key, freq[key])
five 2
six 2
seven 2
four 3
eight 3
three 4
nine 4
two 5
ten 5
one 6
In [108]:
for key in sorted(freq, key=lambda k:freq[k], reverse=True):
    print(key, freq[key])
one 6
two 5
ten 5
three 4
nine 4
four 3
eight 3
five 2
six 2
seven 2
In [109]:
for key in sorted(freq, key=lambda k:freq[k], reverse=True):
    print(key.rjust(6), "*"*freq[key])
   one ******
   two *****
   ten *****
 three ****
  nine ****
  four ***
 eight ***
  five **
   six **
 seven **
In [110]:
d
Out[110]:
{'Anand': 'India',
 'David': 'USA',
 'Lewis': 'UK',
 'Nouful': 'India',
 'Ronbinson': 'USA'}
In [111]:
[ name for name,place in d.items() if place=="India"]
Out[111]:
['Anand', 'Nouful']
In [112]:
[ name for name,place in d.items() if place=="USA"]
Out[112]:
['David', 'Ronbinson']

Pitfalls

In [113]:
x = [1,2,3,4]
y = x
y.append(5)
print(x)
[1, 2, 3, 4, 5]
In [114]:
x = [1,2,3,4,5]
y = x
x = [1,2,3]
print(y)
[1, 2, 3, 4, 5]
In [116]:
d.keys()
Out[116]:
dict_keys(['Anand', 'Nouful', 'David', 'Lewis', 'Ronbinson'])
In [117]:
d.values()
Out[117]:
dict_values(['India', 'India', 'USA', 'UK', 'USA'])
In [118]:
def unzip(data):
    keys = list(data.keys())
    values = [data[k] for k in keys]
    return keys, values
In [119]:
unzip(d)
Out[119]:
(['Anand', 'Nouful', 'David', 'Lewis', 'Ronbinson'],
 ['India', 'India', 'USA', 'UK', 'USA'])

Classes

In [137]:
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
        
    def get_real(self):
        return self.real
    
    def get_imag(self):
        return self.imag

    def display(self):
        print(self.real ,"+", str(self.imag)+"j")
        
    def add(self, c):
        pass
In [128]:
Complex
Out[128]:
__main__.Complex
In [129]:
print(Complex)
<class '__main__.Complex'>
In [130]:
c1 = Complex(2,3)
In [131]:
c1
Out[131]:
<__main__.Complex at 0x7f778005ec18>
In [132]:
c1.get_imag()
Out[132]:
3
In [133]:
c1.get_real()
Out[133]:
2
In [134]:
c1.display()
2 + 3j
In [135]:
isinstance(c1, Complex)
Out[135]:
True
In [139]:
class Complex:
    def __init__(self, real, imag):
        self.real = real
        self.imag = imag
        
    def get_real(self):
        return self.real
    
    def get_imag(self):
        return self.imag

    def display(self):
        print(self.real ,"+", str(self.imag)+"j")
        
    def add(self, c):
        return Complex(self.real+c.real, self.imag+c.imag)
    
    def double(self):
        return Complex(2*self.real, 2*self.imag)
In [140]:
c = Complex(1,2)
In [142]:
c2 = c.double()
In [143]:
c2.display()
2 + 4j
In [144]:
c.display()
1 + 2j
In [145]:
c3 = c.add(c2)
In [146]:
c3.display()
3 + 6j

Why classes?

In [147]:
%%file module1.py
x = 10

def getx():
    return x

def getdouble():
    return 2*x
Overwriting module1.py
In [148]:
%%file module2.py
x = 10

def getx():
    print("from module2")
    return x

def getdouble():
    print("from module2")
    return 2*x
Writing module2.py
In [149]:
import module1
In [150]:
import module2
In [151]:
module1.x
Out[151]:
10
In [152]:
module2.x
Out[152]:
10
In [153]:
module2.x = 20
In [154]:
module2.getx()
from module2
Out[154]:
20
In [155]:
%%file bank0.py
balance = 0

def get_balance():
    return balance

def withdraw(amount):
    global balance
    balance = balance - amount
    
def deposite(amount):
    global balance
    balance += amount
    
if __name__ == "__main__":
    deposite(100)
    print(get_balance())
    withdraw(20)
    print(get_balance())
Writing bank0.py
In [156]:
!python bank0.py
100
80
In [157]:
%%file bank1.py

def create_account():
    return {"balance":0}

def get_balance(account):
    return account['balance']

def withdraw(account, amount):
    account['balance'] -= amount
    
def deposit(account, amount):
    account['balance'] += amount
    

if __name__ == "__main__":
    a1 = create_account()
    a2 = create_account()
    deposit(a1, 100)
    deposit(a2, 200)
    print("a1 : ", get_balance(a1))
    print("a2 : ", get_balance(a2))
    withdraw(a1, 10)
    withdraw(a2, 20)
    print("a1 : ", get_balance(a1))
    print("a2 : ", get_balance(a2))
Writing bank1.py
In [158]:
!python bank1.py
a1 :  100
a2 :  200
a1 :  90
a2 :  180
In [161]:
%%file bank2.py

class BankAccount:
    def __init__(self):
        self.balance = 0
        
    def get_balance(self):
        return self.balance
    
    def deposit(self, amount):
        self.balance += amount
        
    def withdraw(self, amount):
        self.balance -= amount
        
if __name__ == "__main__":
    a1 = BankAccount()
    a2 = BankAccount()
    a1.deposit(100)
    a2.deposit(200)
    print("a1 : ", a1.get_balance())
    print("a2 : ", a2.get_balance())
    a1.withdraw(10)
    a2.withdraw(20)
    print("a1 : ", a1.get_balance())
    print("a2 : ", a2.get_balance())
Overwriting bank2.py
In [162]:
!python bank2.py
a1 :  100
a2 :  200
a1 :  90
a2 :  180
In [163]:
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
In [165]:
p = Point(1, 2)
In [166]:
p.x
Out[166]:
1
In [167]:
p.y
Out[167]:
2
In [168]:
p.z = 3
In [169]:
p.z
Out[169]:
3
In [170]:
p.x
Out[170]:
1
In [171]:
p.y
Out[171]:
2
In [172]:
p.__dict__
Out[172]:
{'x': 1, 'y': 2, 'z': 3}
In [173]:
class ColoredPoint(Point):
    color = (0,0,0) #rgb
In [175]:
cp = ColoredPoint(2, 3)
In [176]:
cp.color
Out[176]:
(0, 0, 0)
In [177]:
cp.x
Out[177]:
2
In [178]:
cp.y
Out[178]:
3
In [179]:
cp.__dict__
Out[179]:
{'x': 2, 'y': 3}
In [180]:
type(ColoredPoint)
Out[180]:
type
In [181]:
type(cp)
Out[181]:
__main__.ColoredPoint
In [182]:
ColoredPoint.__dict__
Out[182]:
mappingproxy({'__doc__': None, '__module__': '__main__', 'color': (0, 0, 0)})
In [183]:
cp.__dict__
Out[183]:
{'x': 2, 'y': 3}

problem:

  • Wrtie a class Timer which can be used to time a task. it should work as shown below. **hint: use time.time() to get timestamp**
    t = Timer()
    t.start()
    do some stuff (may a loop with some calculations)
    t.stop()
    print(t.get_time_taken())
In [184]:
import time
In [185]:
time.time()
Out[185]:
1513321010.1740944
In [188]:
import time

class Timer:
    def __init__(self):
        self._start = 0
        self._end = 0
        
    def start(self):
        self._start = time.time()
        
    def stop(self):
        self._end = time.time()
        
    def get_time_taken(self):
        return self._end - self._start
    
    
In [193]:
t = Timer()
t.start()
s = 0
for i in range(1000):
    for j in range(10000):
        s += i*j*1.0
t.stop()
print("{0:.3f}".format(t.get_time_taken()))
2.875

Exceptions

In [194]:
x + 2
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-194-d693fb1bd54c> in <module>()
----> 1 x + 2

TypeError: can only concatenate list (not "int") to list
In [195]:
2 + "3"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-195-2068cae7beb7> in <module>()
----> 1 2 + "3"

TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [196]:
doom
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-196-301636d58674> in <module>()
----> 1 doom

NameError: name 'doom' is not defined
In [197]:
int("hello")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-197-045de671ab8a> in <module>()
----> 1 int("hello")

ValueError: invalid literal for int() with base 10: 'hello'
In [198]:
try:
    a = int("dfdf")
except ValueError as e:
    print("Handled ValueError", e)
    a = 0
Handled ValueError invalid literal for int() with base 10: 'dfdf'
In [199]:
a
Out[199]:
0
In [201]:
%%file missing.csv
1,2,3,4,5,Nan
1,2,3,4,5,6
1,,3,2,4,5
Nan,1,1,1,1,1
Overwriting missing.csv
In [202]:
csvparser("missing.csv")
Out[202]:
[['1', '2', '3', '4', '5', 'Nan'],
 ['1', '2', '3', '4', '5', '6'],
 ['1', '', '3', '2', '4', '5'],
 ['Nan', '1', '1', '1', '1', '1']]
In [203]:
def parsenumerccsv(filename):
    data = []
    for line in open(filename):
        items = line.strip().split(",")
        data.append([int(i) for i in items])
    return data
In [204]:
parsenumerccsv("missing.csv")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-204-e7497688da74> in <module>()
----> 1 parsenumerccsv("missing.csv")

<ipython-input-203-6b54f33a74c4> in parsenumerccsv(filename)
      3     for line in open(filename):
      4         items = line.strip().split(",")
----> 5         data.append([int(i) for i in items])
      6     return data

<ipython-input-203-6b54f33a74c4> in <listcomp>(.0)
      3     for line in open(filename):
      4         items = line.strip().split(",")
----> 5         data.append([int(i) for i in items])
      6     return data

ValueError: invalid literal for int() with base 10: 'Nan'
In [208]:
def parsenumerccsv(filename):
    def parseint(s):
        try:
            return int(s)
        except ValueError as e:
            return 0
        
    data = []
    for line in open(filename):
        items = line.strip().split(",")
        data.append([parseint(i) for i in items])
    return data
In [209]:
parsenumerccsv("missing.csv")
Out[209]:
[[1, 2, 3, 4, 5, 0],
 [1, 2, 3, 4, 5, 6],
 [1, 0, 3, 2, 4, 5],
 [0, 1, 1, 1, 1, 1]]
In [210]:
try:
    x = int(s)
except ValueError as v:
    pass
except NameError as n:
    pass
except TypeError as t:
    pass

Commandline applications

In [211]:
%%file grep.py
import argparse
    
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("pattern",
                        type=str,
                        help="pattern to be serched")
    parser.add_argument("filename",
                        type=str,
                       help="file in which pattern has to be searched")
    return parser.parse_args()


if __name__ == "__main__":
    args = parse_args()
    print(args)
    
Writing grep.py
In [213]:
!python grep.py "def" bank2.py
Namespace(filename='bank2.py', pattern='def')
In [214]:
%%file grep.py
import argparse
    
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("pattern",
                        type=str,
                        help="pattern to be serched")
    parser.add_argument("filename",
                        type=str,
                       help="file in which pattern has to be searched")
    return parser.parse_args()

def grep(pattern, filename):
    for line in open(filename):
        if pattern in line:
            print(line.strip())
        
if __name__ == "__main__":
    args = parse_args()
    print(args)
    grep(args.pattern, args.filename)
    
    
Overwriting grep.py
In [215]:
!python grep.py def bank2.py
Namespace(filename='bank2.py', pattern='def')
def __init__(self):
def get_balance(self):
def deposit(self, amount):
def withdraw(self, amount):
In [216]:
!python grep.py --help
usage: grep.py [-h] pattern filename

positional arguments:
  pattern     pattern to be serched
  filename    file in which pattern has to be searched

optional arguments:
  -h, --help  show this help message and exit
In [220]:
%%file grep.py
import argparse
    
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("pattern",
                        type=str,
                        help="pattern to be serched")
    parser.add_argument("filename",
                        type=str,
                       help="file in which pattern has to be searched")
    parser.add_argument("-v", "--invert_match",
                        action="store_true",
                        help="lines which do not have given pattern")
    
    return parser.parse_args()

def grep(pattern, filename):
    for line in open(filename):
        if pattern in line:
            print(line.strip())
            
def grep_invert(pattern, filename):
    for line in open(filename):
        if pattern not in line:
            print(line.strip())
        
if __name__ == "__main__":
    args = parse_args()
    print(args)
    if args.invert_match:
        grep_invert(args.pattern, args.filename)
    else:
        grep(args.pattern, args.filename)
    
    
Overwriting grep.py
In [221]:
!python grep.py -h
usage: grep.py [-h] [-v] pattern filename

positional arguments:
  pattern             pattern to be serched
  filename            file in which pattern has to be searched

optional arguments:
  -h, --help          show this help message and exit
  -v, --invert_match  lines which do not have given pattern
In [222]:
!python grep.py -v balance bank0.py
Namespace(filename='bank0.py', invert_match=True, pattern='balance')


def withdraw(amount):

def deposite(amount):

if __name__ == "__main__":
deposite(100)
withdraw(20)
In [223]:
%%file grep.py
import argparse
    
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("pattern",
                        type=str,
                        help="pattern to be serched")
    parser.add_argument("filename",
                        type=str,
                       help="file in which pattern has to be searched")
    parser.add_argument("-v", "--invert_match",
                        action="store_true",
                        help="lines which do not have given pattern")
    parser.add_argument("-b", "--backcontext",
                        type=int,
                        help="number of context lines to be printed")
    
    return parser.parse_args()

def grep(pattern, filename):
    for line in open(filename):
        if pattern in line:
            print(line.strip())
            
def grep_invert(pattern, filename):
    for line in open(filename):
        if pattern not in line:
            print(line.strip())
        
if __name__ == "__main__":
    args = parse_args()
    print(args)
    if args.invert_match:
        grep_invert(args.pattern, args.filename)
    else:
        grep(args.pattern, args.filename)
    
Overwriting grep.py
In [224]:
!python grep.py -h
usage: grep.py [-h] [-v] [-b BACKCONTEXT] pattern filename

positional arguments:
  pattern               pattern to be serched
  filename              file in which pattern has to be searched

optional arguments:
  -h, --help            show this help message and exit
  -v, --invert_match    lines which do not have given pattern
  -b BACKCONTEXT, --backcontext BACKCONTEXT
                        number of context lines to be printed
In [225]:
!python grep.py -b 5 def bank0.py
Namespace(backcontext=5, filename='bank0.py', invert_match=False, pattern='def')
def get_balance():
def withdraw(amount):
def deposite(amount):

problem:

  • Write a command fib.py which has two arguments. one is positional and default argument n, with this you should print nth fibonacci number. second argument is optional and boolean argument -s/--sequence. if -s option is given you should print sequence of first n fibonacci numbers.
    python fib.py 3
    2
    python fib.py -s 6
    1 1 2 3 5 8

Downloading stuff from internet

you can install requests library using

pip3 install requests
In [226]:
import requests
In [227]:
resp = requests.get("http://httpbin.org/get", params={"param1":1, "param2":"2"})
In [228]:
resp.status_code
Out[228]:
200
In [229]:
resp.text[:50]
Out[229]:
'{\n  "args": {\n    "param1": "1", \n    "param2": "2'
In [230]:
resp.text
Out[230]:
'{\n  "args": {\n    "param1": "1", \n    "param2": "2"\n  }, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Connection": "close", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.14.2"\n  }, \n  "origin": "42.107.68.185", \n  "url": "http://httpbin.org/get?param1=1&param2=2"\n}\n'
In [231]:
resp.json()
Out[231]:
{'args': {'param1': '1', 'param2': '2'},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Connection': 'close',
  'Host': 'httpbin.org',
  'User-Agent': 'python-requests/2.14.2'},
 'origin': '42.107.68.185',
 'url': 'http://httpbin.org/get?param1=1&param2=2'}
In [233]:
resp = requests.post("http://httpbin.org/post", data={"name":"python", "param":"hello"})
In [234]:
resp.json()
Out[234]:
{'args': {},
 'data': '',
 'files': {},
 'form': {'name': 'python', 'param': 'hello'},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Connection': 'close',
  'Content-Length': '23',
  'Content-Type': 'application/x-www-form-urlencoded',
  'Host': 'httpbin.org',
  'User-Agent': 'python-requests/2.14.2'},
 'json': None,
 'origin': '42.107.68.185',
 'url': 'http://httpbin.org/post'}
In [235]:
resp = requests.post("http://httpbin.org/post", data="Plain text data")
In [236]:
resp.json()
Out[236]:
{'args': {},
 'data': 'Plain text data',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Connection': 'close',
  'Content-Length': '15',
  'Host': 'httpbin.org',
  'User-Agent': 'python-requests/2.14.2'},
 'json': None,
 'origin': '42.107.68.185',
 'url': 'http://httpbin.org/post'}
In [237]:
resp = requests.post("http://httpbin.org/post", data=open("data1.csv"))
In [238]:
resp.json()
Out[238]:
{'args': {},
 'data': 'A1,B1,C1\nA2,B2,C2\nA3,B3,C3\nA4,B4,C4\n',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Connection': 'close',
  'Content-Length': '36',
  'Host': 'httpbin.org',
  'User-Agent': 'python-requests/2.14.2'},
 'json': None,
 'origin': '42.107.68.185',
 'url': 'http://httpbin.org/post'}
In [239]:
url = "https://api.github.com/orgs/vmware/repos"
In [240]:
resp = requests.get(url)
In [241]:
repos = resp.json()
In [242]:
type(repos)
Out[242]:
list
In [243]:
len(repos)
Out[243]:
30
In [247]:
print(repos[0]['forks'])
4
In [248]:
for r in repos:
    print(r['name'], r['forks'])
pyvco 4
rvc 46
rbvmomi 153
vprobe-toolkit 9
CloudFS 16
vcd-nclient 2
lmock 5
FireBreath 2
weasel 1
vmware-vcenter 86
vmware-vshield 6
vcloud-rest 37
GemstoneWebTools 0
vmware-vcsa 17
vmware-vmware_lib 24
saml20serviceprovider 1
pg_rewind 18
vco-powershel-plugin 2
jenkins-reviewbot 12
dbeekeeper 0
thinapp_factory 16
vmware-cassandra 4
vmware-java 0
data-driven-framework 3
pyvmomi 450
pyvmomi-community-samples 387
open-vm-tools 146
pyvmomi-tools 18
upgrade-framework 11
webcommander 30
In [249]:
def get_forks(repo):
    return repo['forks']

for repo in sorted(repos, key=get_forks, reverse=True)[:5]:
    print(repo['name'], repo['forks'])
pyvmomi 450
pyvmomi-community-samples 387
rbvmomi 153
open-vm-tools 146
vmware-vcenter 86

problem:

  • write a function to find distance between two cities using google api.
    distance("bangalore", "pune")
    800km
In [256]:
def distance(source, dest):
    url = "https://maps.googleapis.com/maps/api/distancematrix/json"
    resp = requests.get(url, params={"origins":source,"destinations":dest, "units":"metric"})
    data = resp.json()
    return data['rows'][0]['elements'][0]['distance']['text']
In [257]:
distance("bangalore", "pune")
Out[257]:
'837 km'
In [258]:
import json
In [260]:
s = json.dumps({"data":[1,2,3,{"x":"y"}]})
In [261]:
s
Out[261]:
'{"data": [1, 2, 3, {"x": "y"}]}'
In [262]:
json.loads(s)
Out[262]:
{'data': [1, 2, 3, {'x': 'y'}]}

References

In [ ]: