Python Training at VMWare - Day 3

Jan 29-31, 2018 Vikrant Patil

These notes are available online at http://notes.pipal.in/2018/vmware-pune-jan-python

© Pipal Academy LLP

Day 1 | Day 2 | Day 3

Writing files

In [1]:
f = open("numbers.txt", "w")
f.write("ones\n")
f.write("two\n")
f.write("three\n")
Out[1]:
6
In [2]:
f.close()
In [3]:
!python cat.py numbers.txt
ones
two
three

In [5]:
with open("primes.txt", "w") as f:
    f.write("two\n")
    f.write("three\n")
    f.write("five\n")
print("Here ends with block")
Here ends with block
In [6]:
!python cat.py primes.txt
two
three
five

In [7]:
with open("primes.txt", "a") as f:
    f.write("seven\n")
    f.write("eleven\n")
In [8]:
!python cat.py primes.txt
two
three
five
seven
eleven

In [9]:
with open("binary.bin", "wb") as f:
    f.write(b"\x23\x2a")
    f.write(b"string as binary data\n")
    
In [10]:
with open("binary.bin", "rb") as f:
    print(f.read())
b'#*string as binary data\n'
In [12]:
with open("regional.txt", "w", encoding="utf-8") as f:
    f.write("आआडढ\n")
    f.write("\u0c65")
    
In [13]:
with open("regional.txt", encoding="utf-8") as f:
    print(f.read())
आआडढ
à±¥

Example

Writing a csv file

In [14]:
t = [[i*j for i in range(1,11)] for j in range(1,5)]
In [15]:
t
Out[15]:
[[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]]
In [19]:
def writecsv(data, filename):
    with open(filename, "w") as csv:
        for row in data:
            csv.write(",".join([str(item) for item in row]))
            csv.write("\n")
                      
In [20]:
writecsv(t, "tables.csv")
In [21]:
!python cat.py tables.csv
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

problem

  • write a csvparser to load numeric data from csv file to a 2d list.
  • Can you make it generic where in you also specify the delimiter.
In [22]:
l = []
l[0] = 3
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-22-b627951f9766> in <module>()
      1 l = []
----> 2 l[0] = 3

IndexError: list assignment index out of range
In [23]:
numbers = ["1","2","3","4","5","6"]
In [24]:
[int(item) for item in numbers]
Out[24]:
[1, 2, 3, 4, 5, 6]
In [25]:
def csvparser(filename):
    data = []
    with open(filename) as f:
        for line in f:
            row_ = line.strip().split(",")
            row = [int(i) for i in row_]
            data.append(row)
    return data
In [26]:
csvparser("tables.csv")
Out[26]:
[[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]]
In [28]:
[[int(i) for i in line.strip().split(",")] for line in open("tables.csv")]
Out[28]:
[[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]]
In [29]:
def csvparser(filename):
    return [[int(i) for i in line.strip().split(",")] for line in open(filename)]
In [33]:
def csvparser(filename):
    return [[int(i) for i in line.strip().split(",")] for line in open(filename)]
In [30]:
csvparser("tables.csv")
Out[30]:
[[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]]
In [31]:
def csvparser(filename):
    data = []
    with open(filename) as f:
        line = f.readline()
        while line:
            row_ = line.strip().split(",")
            row = [int(i) for i in row_]
            data.append(row)
            line = f.readline()
    return data
In [32]:
csvparser("tables.csv")
Out[32]:
[[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]]

writing to stderr and stdout

In [34]:
import sys
sys.stderr.write("Something went wrong!")
Something went wrong!
In [35]:
sys.stdout.write("Just a print")
Just a print

Dictionaries

In [ ]:
 
In [36]:
author = {"name":"Lewis carol",
          "books":["Alice in wonderland","Looking through the glass"],
          "language":"english"}
In [37]:
author['name']
Out[37]:
'Lewis carol'
In [38]:
author['books']
Out[38]:
['Alice in wonderland', 'Looking through the glass']
In [39]:
author['language']="english-uk"
In [40]:
author
Out[40]:
{'books': ['Alice in wonderland', 'Looking through the glass'],
 'language': 'english-uk',
 'name': 'Lewis carol'}
In [41]:
del author["language"]
In [42]:
author
Out[42]:
{'books': ['Alice in wonderland', 'Looking through the glass'],
 'name': 'Lewis carol'}
In [43]:
author['books'][1]
Out[43]:
'Looking through the glass'
In [44]:
"name" in author
Out[44]:
True
In [45]:
"lagguage" in author
Out[45]:
False
In [46]:
author['xyz']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-46-5980827fb7cd> in <module>()
----> 1 author['xyz']

KeyError: 'xyz'
In [47]:
author.get("key",[])
Out[47]:
[]
In [48]:
author.get("key",2)
Out[48]:
2
In [49]:
author
Out[49]:
{'books': ['Alice in wonderland', 'Looking through the glass'],
 'name': 'Lewis carol'}
In [50]:
author['key'] = "hello"
In [51]:
type(author)
Out[51]:
dict
In [52]:
isinstance(author,dict)
Out[52]:
True
In [54]:
author.get("xyz",None)
In [60]:
delattr(author ,"Lewis carol")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-60-ad17a32ea56f> in <module>()
----> 1 delattr(author ,"Lewis carol")

AttributeError: 'dict' object has no attribute 'Lewis carol'
In [61]:
author
Out[61]:
{'books': ['Alice in wonderland', 'Looking through the glass'],
 'key': 'hello',
 'name': 'Lewis carol'}
In [62]:
help(delattr)
Help on built-in function delattr in module builtins:

delattr(obj, name, /)
    Deletes the named attribute from the given object.
    
    delattr(x, 'y') is equivalent to ``del x.y''

In [63]:
d = {}
names = ["Anand","Naufal","David","Alice"]
countries = ["India","India","USA","UK"]
In [64]:
for name,country in zip(names, countries):
    d[name] = country
In [65]:
d
Out[65]:
{'Alice': 'UK', 'Anand': 'India', 'David': 'USA', 'Naufal': 'India'}
In [66]:
items = [1,2,3,4]
In [67]:
item[4]= 5
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-67-0dbe92e659d5> in <module>()
----> 1 item[4]= 5

NameError: name 'item' is not defined
In [68]:
d["Alex"] = "USA"
In [69]:
d
Out[69]:
{'Alex': 'USA',
 'Alice': 'UK',
 'Anand': 'India',
 'David': 'USA',
 'Naufal': 'India'}
In [70]:
dict(zip(names, countries))
Out[70]:
{'Alice': 'UK', 'Anand': 'India', 'David': 'USA', 'Naufal': 'India'}

Example

load grub conf as dictinary

In [71]:
!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"

In [79]:
def load_grubconf(filename):
    conf = {}
    for line in open(filename):
        if line.startswith("#") or line.strip()=="" or ("=" not in line):
            continue
        tokens = line.strip().split("=",1)
        conf[tokens[0]] = tokens[1]
    return conf
In [80]:
load_grubconf("/etc/default/grub")
Out[80]:
{'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'}

problem

  • Write a function to find frquency of all words from given file
In [77]:
%%file words.txt
one
one two
one two three
one two three four
one two three four five
one two three four five six
one two three four five
one two three four
one two three
one two
one
Writing words.txt
In [78]:
"a:".split(":")
Out[78]:
['a', '']
In [81]:
def wordfreq(filename):
    words = open(filename).read().strip().split()
    freq = {}
    for word in words:
        freq[word] = freq.get(word,0) + 1
    return freq
In [82]:
wordfreq("words.txt")
Out[82]:
{'five': 3, 'four': 5, 'one': 11, 'six': 1, 'three': 7, 'two': 9}
In [83]:
def wordfreq(filename):
    words = open(filename).read().strip().split()
    uniq = set(words)
    freq = {}
    for w in uniq:
        freq[w] = words.count(w)
    return freq

Iterating over dictionaries

In [84]:
freq = wordfreq("words.txt")
In [85]:
for item in freq:
    print(item, freq[item])
three 7
five 3
six 1
four 5
one 11
two 9
In [86]:
for item in freq:
    print(item)
three
five
six
four
one
two
In [88]:
for key,value in freq.items():
    print(key, value)
three 7
five 3
six 1
four 5
one 11
two 9
In [89]:
for value in freq.values():
    print(value)
7
3
1
5
11
9
In [90]:
for item in freq.keys():
    print(item)
three
five
six
four
one
two
In [91]:
for key,value in freq.items():
    print(key.rjust(5), str(value).ljust(2))
three 7 
 five 3 
  six 1 
 four 5 
  one 11
  two 9 
In [92]:
for key,value in sorted(freq.items()):
    print(key.rjust(5), str(value).ljust(2))
 five 3 
 four 5 
  one 11
  six 1 
three 7 
  two 9 
In [94]:
for key in sorted(freq, key=lambda x:freq[x]):
    print(key.rjust(5), str(freq[key]).ljust(2))
  six 1 
 five 3 
 four 5 
three 7 
  two 9 
  one 11
In [95]:
for key in sorted(freq, key=lambda x:freq[x], reverse=True):
    print(key.rjust(5), str(freq[key]).ljust(2))
  one 11
  two 9 
three 7 
 four 5 
 five 3 
  six 1 
In [98]:
sorted(freq, key=lambda x:freq[x], reverse=True)
Out[98]:
['one', 'two', 'three', 'four', 'five', 'six']
In [99]:
for key in sorted(freq, key=lambda x:freq[x], reverse=True):
    print(key.rjust(5), str(freq[key]).ljust(2), "*"*freq[key])
  one 11 ***********
  two 9  *********
three 7  *******
 four 5  *****
 five 3  ***
  six 1  *
In [100]:
d
Out[100]:
{'Alex': 'USA',
 'Alice': 'UK',
 'Anand': 'India',
 'David': 'USA',
 'Naufal': 'India'}
In [102]:
v = set(d.values())
In [105]:
[key for key in d if d[key]=="India"] #group all keys based on values
Out[105]:
['Anand', 'Naufal']
In [106]:
[key for key in d if d[key]=="USA"]
Out[106]:
['David', 'Alex']
In [110]:
for item in v:
    print([key for key in d if d[key]==item])
['David', 'Alex']
['Alice']
['Anand', 'Naufal']

pitfalls

In [111]:
x = [1,2,3]
y = x
y.append(4)
x
Out[111]:
[1, 2, 3, 4]
In [112]:
x
Out[112]:
[1, 2, 3, 4]
In [113]:
y
Out[113]:
[1, 2, 3, 4]
In [114]:
x = [1,2,3]
y = x
x = [1,1]
y
Out[114]:
[1, 2, 3]
In [115]:
y = x[:]
In [116]:
y
Out[116]:
[1, 1]
In [117]:
x = [1,2,3]
y = x[:]
y.append(4)
x
Out[117]:
[1, 2, 3]

Why classes

In [118]:
%%file module1.py

x = 5
y = 3

def addx(z):
    return x+z

def addy(z):
    return y+z
Writing module1.py
In [119]:
import module1
In [120]:
module1.x
Out[120]:
5
In [121]:
module1.y
Out[121]:
3
In [122]:
module1.x = 0
In [123]:
module1.x
Out[123]:
0
In [124]:
%%file bank0.py

balance = 0

def getbalnce():
    return balance

def deposite(amount):
    global balance
    balance += amount
    
def withdraw(amount):
    global balance
    balance -= amount
    
if __name__ == "__main__":
    print(getbalnce())
    deposite(100)
    print(getbalnce())
    withdraw(23)
    print(getbalnce())
Writing bank0.py
In [125]:
!python bank0.py
0
100
77
In [126]:
%%file bank1.py

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

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

def deposite(account, amount):
    account['balance'] += amount
    
def withdraw(account, amount):
    account['balance'] -= amount
    
if __name__ == "__main__":
    def print_(a1, a2):
        print("a1: ",get_balance(a1))
        print("a2: ",get_balance(a2))

    a1 = make_account()
    a2 = make_account()
    print_(a1, a2)
    deposite(a1, 1000)
    deposite(a2, 1100)
    print_(a1,a2)
    withdraw(a1, 244)
    withdraw(a2, 300)
    print_(a1, a2)
Writing bank1.py
In [128]:
!python bank1.py
a1:  0
a2:  0
a1:  1000
a2:  1100
a1:  756
a2:  800
In [129]:
module1.z = 3
In [130]:
%%file bank2.py

class BankAccount:
    
    def __init__(self, balance=0):
        self.balance = balance
        
    def get_balance(self):
        return self.balance
    
    def deposite(self, amount):
        self.balance += amount
        
    def withdraw(self, amount):
        self.balance -= amount
    
    def hello():
        print("Hello")
        
if __name__ == "__main__":
    a1 = BankAccount()
    a2 = BankAccount(300)
    print("a1: ", a1.get_balance())
    print("a2: ", a2.get_balance())
    
Writing bank2.py
In [131]:
class Point:
    
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
In [133]:
p = Point(1,2)
In [134]:
p.x
Out[134]:
1
In [135]:
p.y
Out[135]:
2
In [136]:
def add(x,y):
    return x+y
In [137]:
class Foo:
    pass
In [138]:
Foo
Out[138]:
__main__.Foo
In [139]:
type(Foo)
Out[139]:
type
In [140]:
type(p)
Out[140]:
__main__.Point
In [141]:
p.__dict__
Out[141]:
{'x': 1, 'y': 2}
In [142]:
p.z = 3
In [143]:
p.z
Out[143]:
3
In [144]:
p.__dict__
Out[144]:
{'x': 1, 'y': 2, 'z': 3}
In [145]:
Foo.__dict__
Out[145]:
mappingproxy({'__dict__': <attribute '__dict__' of 'Foo' objects>,
              '__doc__': None,
              '__module__': '__main__',
              '__weakref__': <attribute '__weakref__' of 'Foo' objects>})
In [146]:
def f():
    pass
In [147]:
f()
In [148]:
p.newfunc = f
In [149]:
del p.x
In [150]:
p.x
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-150-87ad85736430> in <module>()
----> 1 p.x

AttributeError: 'Point' object has no attribute 'x'

problem

  • Write a class Timer, which can be used to to time a task. make use of time.time()
t = Timer()
t.start()
s = 0
for i in range(1000):
    for j in range(10000):
        s += i*j*1.0

t.stop()
print(t.get_time_taken())
In [151]:
import time
In [152]:
time.time()
Out[152]:
1517386860.2550464
In [194]:
class Complex:
    
    def __init__(self, real, img):
        self.real = real
        self.img = img
        
    def add(self, c):
        return Complex(self.real+c.real, self.img+c.img)
    
    def _display(self):
        return "{r}+{i}j".format(r=self.real, i=self.img)
    
    def __repr__(self):
        return "Complex({r},{i})".format(r=self.real, i=self.img)
        
    def __str__(self):
        return self._display()
    
In [195]:
c = Complex(2,3)
In [196]:
help(Complex)
Help on class Complex in module __main__:

class Complex(builtins.object)
 |  Methods defined here:
 |  
 |  __init__(self, real, img)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  __repr__(self)
 |      Return repr(self).
 |  
 |  __str__(self)
 |      Return str(self).
 |  
 |  add(self, c)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

In [197]:
c._display()
Out[197]:
'2+3j'
In [198]:
c1 = Complex(3,4)
In [199]:
c2 = c.add(c1)
In [200]:
c2._display()
Out[200]:
'5+7j'
In [201]:
print(c) ## coming from __str__
2+3j
In [204]:
c # coming from __repr__
Out[204]:
Complex(2,3)
In [164]:
class Timer:
    
    def start(self):
        self._start = time.time()
        
    def stop(self):
        self._stop = time.time()
        
    def get_time_taken(self):
        return self._stop - self._start
    
    
In [165]:
t = Timer()
In [166]:
t.start()
In [167]:
time.sleep(2)
In [168]:
t.stop()
In [169]:
t.get_time_taken()
Out[169]:
19.798047304153442
In [170]:
t
Out[170]:
<__main__.Timer at 0x7f33a8134438>
In [205]:
class Foo:
    pass

class Foo(object):
    pass
In [206]:
class ColoredPoint(Point):
    
    def __init__(self, x, y, color=(0,0,0)):
        Point.__init__(self, x, y)
        self.color = color
In [207]:
item = [1,2,3,4]
In [208]:
item
Out[208]:
[1, 2, 3, 4]
In [209]:
import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
In [210]:
a
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-210-60b725f10c9c> in <module>()
----> 1 a

NameError: name 'a' is not defined
In [211]:
c
Out[211]:
Complex(2,3)

Example

In [235]:
import math
import matplotlib.pyplot as plt
%matplotlib inline

def imshow(img):
    plt.imshow(img, cmap=plt.cm.gray)
    plt.show()
    
    
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
class Shape:
    
    position = Point(0,0)
        
    def contains(self, point):
        pass
    
def distance(p1, p2):
    dx = p2.x - p1.x
    dy = p2.y - p1.y
    return math.sqrt(dx**2  + dy**2)
    
class Circle(Shape):
    
    def __init__(self, radius):    
        self.radius = radius
        
    def contains(self, p):
        return distance(p, self.position)<=self.radius
    
class Recangle(Shape):
    
    def __init__(self, length, width):
        self.length = length
        self.width = width
        
    def contains(self, point):
        dx = point.x - self.position.x
        dy = point.y - self.position.y
        return dx<=self.width and dy<=self.length
    
    
class Intersect(Shape):
    
    def __init__(self, shapes):
        self.shapes = shapes
        
    def contains(self, point):
        flag = True
        for s in self.shapes:
            flag = flag and s.contains(point)
        return flag
    
class Union(Shape):
    
    def __init__(self, shapes):
        self.shapes = shapes
        
    def contains(self, point):
        flag = False
        for s in self.shapes:
            flag = flag or s.contains(point)
        return flag   

class Canvas:
    
    def __init__(self, x=200, y=200):
        self.x = x
        self.y = y
        self.data = [[False for i in range(x)] for j in range(y)]
    
    def paint(self, shape):
        for i in range(self.y):
            for j in range(self.x):
                self.data[i][j] = shape.contains(Point(i,j))

    def display(self):
        imshow(self.data)

    
In [221]:
c = Circle(20)
In [222]:
canvas = Canvas()
In [223]:
canvas.paint(c)
In [224]:
canvas.display()
In [228]:
r = Recangle(100,50)
In [229]:
canvas.paint(r)
In [230]:
canvas.display()
In [234]:
s = [Circle(50),Recangle(,100)]
intersect = Intersect(s)
c = Canvas()
c.paint(intersect)
c.display()
In [237]:
s = [Circle(50),Recangle(20,100)]
u = Union(s)
c = Canvas()
c.paint(u)
c.display()

Exceptions

In [238]:
%%file missing.txt
1,2,3,4,5,6,,8,9,10
2,4,6,8,10,Nan,14,16,18,20
3,6,9,12,15,18,21,24,27,30
4,8,12,16,20,24,,32,36,40
Writing missing.txt
In [239]:
csvparser("missing.txt")
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-239-dbe448edae73> in <module>()
----> 1 csvparser("missing.txt")

<ipython-input-33-2983edcb116a> in csvparser(filename)
      1 def csvparser(filename):
----> 2     return [[int(i) for i in line.strip().split(",")] for line in open(filename)]

<ipython-input-33-2983edcb116a> in <listcomp>(.0)
      1 def csvparser(filename):
----> 2     return [[int(i) for i in line.strip().split(",")] for line in open(filename)]

<ipython-input-33-2983edcb116a> in <listcomp>(.0)
      1 def csvparser(filename):
----> 2     return [[int(i) for i in line.strip().split(",")] for line in open(filename)]

ValueError: invalid literal for int() with base 10: ''
In [252]:
import sys
def parseint(strnm):
    try:
        return int(strnm)
    except ValueError as v:
        sys.stderr.write(str(v))
        return 0
In [253]:
def csvparser(filename):
    return [[parseint(v) for v in line.strip().split(",")] for line in open(filename)]
In [254]:
csvparser("missing.txt")
invalid literal for int() with base 10: ''invalid literal for int() with base 10: 'Nan'invalid literal for int() with base 10: ''
Out[254]:
[[1, 2, 3, 4, 5, 6, 0, 8, 9, 10],
 [2, 4, 6, 8, 10, 0, 14, 16, 18, 20],
 [3, 6, 9, 12, 15, 18, 21, 24, 27, 30],
 [4, 8, 12, 16, 20, 24, 0, 32, 36, 40]]

building commandline applications

In [272]:
%%file head.py
import argparse

def head(filename, lines=5):
    f = open(filename)
    for i in range(lines):
        print(f.readline().strip())


def parse_arguments():
    parser = argparse.ArgumentParser(
        description="head command shows first few lines of file")
    # if only one name given it is 
    parser.add_argument("filename", type=str,
                       help="File whose contents are to be shown")
    #if long and short name is given then this is optional
    parser.add_argument("-n", "--lines", type=int,
                       help="number of lines to show")
    return parser.parse_args()
    

if __name__ == "__main__":
    args = parse_arguments()
    print(args)
    if args.lines:
        head(args.filename, args.lines)
    else:
        head(args.filename)
   
Overwriting head.py
In [273]:
!python head.py
usage: head.py [-h] [-n LINES] filename
head.py: error: the following arguments are required: filename
In [274]:
!python head.py -h
usage: head.py [-h] [-n LINES] filename

head command shows first few lines of file

positional arguments:
  filename              File whose contents are to be shown

optional arguments:
  -h, --help            show this help message and exit
  -n LINES, --lines LINES
                        number of lines to show
In [275]:
!python head.py data.txt
Namespace(filename='data.txt', lines=None)
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
In [276]:
!python head.py -n 10 data.txt
Namespace(filename='data.txt', lines=10)
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
In [278]:
!python head.py -n xsx data.txt
usage: head.py [-h] [-n LINES] filename
head.py: error: argument -n/--lines: invalid int value: 'xsx'
In [289]:
%%file grep.py

import argparse

def grep(pattern, filename):
    for line in open(filename):
        if pattern in line:
            print(line, end="")
            
def invertgrep(pattern, filename):
    for line in open(filename):
        if pattern not in line:
            print(line, end="")

def parse_arguments():
    parser = argparse.ArgumentParser(
        description="grep command greps for given patters in a file")
    # if only one name given it is 
    parser.add_argument("pattern", type=str,
                       help="pattern to search")
    parser.add_argument("filename", type=str,
                       help="File in which to look for pattern")
    #if long and short name is given then this is optional
    parser.add_argument("-v", "--invert",
                       help="Invert the match",
                       action="store_true")
    return parser.parse_args()
    

if __name__ == "__main__":
    args = parse_arguments()
    if args.invert:
        invertgrep(args.pattern, args.filename)
    else:
        grep(args.pattern, args.filename)
Overwriting grep.py
In [290]:
!python grep.py "def" head.py
def head(filename, lines=5):
def parse_arguments():
In [291]:
!python grep.py -v "def" head.py
import argparse

    f = open(filename)
    for i in range(lines):
        print(f.readline().strip())


    parser = argparse.ArgumentParser(
        description="head command shows first few lines of file")
    # if only one name given it is 
    parser.add_argument("filename", type=str,
                       help="File whose contents are to be shown")
    #if long and short name is given then this is optional
    parser.add_argument("-n", "--lines", type=int,
                       help="number of lines to show")
    return parser.parse_args()
    

if __name__ == "__main__":
    args = parse_arguments()
    print(args)
    if args.lines:
        head(args.filename, args.lines)
    else:
        head(args.filename)
   

working with web api

In [292]:
import requests

install requests using

pip3 install requests
In [293]:
resp = requests.get("http://httpbin.org/html")
In [294]:
print(resp.text[:100])
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
      <h1>Herman Melville - Moby-Dick</h1>

     
In [295]:
resp.status_code
Out[295]:
200
In [296]:
resp = requests.get("http://httpbin.org/get", params={"param1":"python",
                                                     "param2":"xyz"})
In [297]:
print(resp.text)
{
  "args": {
    "param1": "python", 
    "param2": "xyz"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "Ip-Address": "2402:8100:3021:6c34:c51:8194:c538:dcf3", 
    "User-Agent": "python-requests/2.14.2", 
    "X-Charging-Characteristics": "1024", 
    "X-Imei": "8635820349323301", 
    "X-Nokia-Imsi": "404223009977543", 
    "X-Nokia-Msisdn": "917776040198", 
    "X-Nokia-Prepaidind": "8", 
    "X-Rat": "GTP_RAT_TYPE_UTRAN"
  }, 
  "origin": "1.187.6.187", 
  "url": "http://httpbin.org/get?param1=python&param2=xyz"
}

In [298]:
resp = requests.post("http://httpbin.org/post", data={"name":"python", 
                                                      "email":"xyz@xyz.com"})
In [299]:
print(resp.text)
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "email": "xyz@xyz.com", 
    "name": "python"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "31", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "Ip-Address": "2402:8100:3021:6c34:c51:8194:c538:dcf3", 
    "User-Agent": "python-requests/2.14.2", 
    "X-Charging-Characteristics": "1024", 
    "X-Imei": "8635820349323301", 
    "X-Nokia-Imsi": "404223009977543", 
    "X-Nokia-Msisdn": "917776040198", 
    "X-Nokia-Prepaidind": "8", 
    "X-Rat": "GTP_RAT_TYPE_UTRAN"
  }, 
  "json": null, 
  "origin": "106.79.141.22", 
  "url": "http://httpbin.org/post"
}

In [300]:
resp = requests.post("http://httpbin.org/post", data=open("numbers.txt"))
In [301]:
print(resp.text)
{
  "args": {}, 
  "data": "ones\ntwo\nthree\n", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "15", 
    "Host": "httpbin.org", 
    "Ip-Address": "2402:8100:3021:6c34:c51:8194:c538:dcf3", 
    "User-Agent": "python-requests/2.14.2", 
    "X-Charging-Characteristics": "1024", 
    "X-Imei": "8635820349323301", 
    "X-Nokia-Imsi": "404223009977543", 
    "X-Nokia-Msisdn": "917776040198", 
    "X-Nokia-Prepaidind": "8", 
    "X-Rat": "GTP_RAT_TYPE_UTRAN"
  }, 
  "json": null, 
  "origin": "49.14.69.170", 
  "url": "http://httpbin.org/post"
}

In [302]:
resp.json()
Out[302]:
{'args': {},
 'data': 'ones\ntwo\nthree\n',
 'files': {},
 'form': {},
 'headers': {'Accept': '*/*',
  'Accept-Encoding': 'gzip, deflate',
  'Connection': 'close',
  'Content-Length': '15',
  'Host': 'httpbin.org',
  'Ip-Address': '2402:8100:3021:6c34:c51:8194:c538:dcf3',
  'User-Agent': 'python-requests/2.14.2',
  'X-Charging-Characteristics': '1024',
  'X-Imei': '8635820349323301',
  'X-Nokia-Imsi': '404223009977543',
  'X-Nokia-Msisdn': '917776040198',
  'X-Nokia-Prepaidind': '8',
  'X-Rat': 'GTP_RAT_TYPE_UTRAN'},
 'json': None,
 'origin': '49.14.69.170',
 'url': 'http://httpbin.org/post'}

Example

Find popular repositories of vmware on github

In [303]:
url = "https://api.github.com/orgs/vmware/repos"
repos = requests.get(url).json()
In [305]:
type(repos)
Out[305]:
list
In [306]:
len(repos)
Out[306]:
30
In [307]:
print(repos[0])
{'id': 1517508, 'name': 'pyvco', 'full_name': 'vmware/pyvco', 'owner': {'login': 'vmware', 'id': 473334, 'avatar_url': 'https://avatars0.githubusercontent.com/u/473334?v=4', 'gravatar_id': '', 'url': 'https://api.github.com/users/vmware', 'html_url': 'https://github.com/vmware', 'followers_url': 'https://api.github.com/users/vmware/followers', 'following_url': 'https://api.github.com/users/vmware/following{/other_user}', 'gists_url': 'https://api.github.com/users/vmware/gists{/gist_id}', 'starred_url': 'https://api.github.com/users/vmware/starred{/owner}{/repo}', 'subscriptions_url': 'https://api.github.com/users/vmware/subscriptions', 'organizations_url': 'https://api.github.com/users/vmware/orgs', 'repos_url': 'https://api.github.com/users/vmware/repos', 'events_url': 'https://api.github.com/users/vmware/events{/privacy}', 'received_events_url': 'https://api.github.com/users/vmware/received_events', 'type': 'Organization', 'site_admin': False}, 'private': False, 'html_url': 'https://github.com/vmware/pyvco', 'description': 'Python bindings for VMware Orchestrator', 'fork': True, 'url': 'https://api.github.com/repos/vmware/pyvco', 'forks_url': 'https://api.github.com/repos/vmware/pyvco/forks', 'keys_url': 'https://api.github.com/repos/vmware/pyvco/keys{/key_id}', 'collaborators_url': 'https://api.github.com/repos/vmware/pyvco/collaborators{/collaborator}', 'teams_url': 'https://api.github.com/repos/vmware/pyvco/teams', 'hooks_url': 'https://api.github.com/repos/vmware/pyvco/hooks', 'issue_events_url': 'https://api.github.com/repos/vmware/pyvco/issues/events{/number}', 'events_url': 'https://api.github.com/repos/vmware/pyvco/events', 'assignees_url': 'https://api.github.com/repos/vmware/pyvco/assignees{/user}', 'branches_url': 'https://api.github.com/repos/vmware/pyvco/branches{/branch}', 'tags_url': 'https://api.github.com/repos/vmware/pyvco/tags', 'blobs_url': 'https://api.github.com/repos/vmware/pyvco/git/blobs{/sha}', 'git_tags_url': 'https://api.github.com/repos/vmware/pyvco/git/tags{/sha}', 'git_refs_url': 'https://api.github.com/repos/vmware/pyvco/git/refs{/sha}', 'trees_url': 'https://api.github.com/repos/vmware/pyvco/git/trees{/sha}', 'statuses_url': 'https://api.github.com/repos/vmware/pyvco/statuses/{sha}', 'languages_url': 'https://api.github.com/repos/vmware/pyvco/languages', 'stargazers_url': 'https://api.github.com/repos/vmware/pyvco/stargazers', 'contributors_url': 'https://api.github.com/repos/vmware/pyvco/contributors', 'subscribers_url': 'https://api.github.com/repos/vmware/pyvco/subscribers', 'subscription_url': 'https://api.github.com/repos/vmware/pyvco/subscription', 'commits_url': 'https://api.github.com/repos/vmware/pyvco/commits{/sha}', 'git_commits_url': 'https://api.github.com/repos/vmware/pyvco/git/commits{/sha}', 'comments_url': 'https://api.github.com/repos/vmware/pyvco/comments{/number}', 'issue_comment_url': 'https://api.github.com/repos/vmware/pyvco/issues/comments{/number}', 'contents_url': 'https://api.github.com/repos/vmware/pyvco/contents/{+path}', 'compare_url': 'https://api.github.com/repos/vmware/pyvco/compare/{base}...{head}', 'merges_url': 'https://api.github.com/repos/vmware/pyvco/merges', 'archive_url': 'https://api.github.com/repos/vmware/pyvco/{archive_format}{/ref}', 'downloads_url': 'https://api.github.com/repos/vmware/pyvco/downloads', 'issues_url': 'https://api.github.com/repos/vmware/pyvco/issues{/number}', 'pulls_url': 'https://api.github.com/repos/vmware/pyvco/pulls{/number}', 'milestones_url': 'https://api.github.com/repos/vmware/pyvco/milestones{/number}', 'notifications_url': 'https://api.github.com/repos/vmware/pyvco/notifications{?since,all,participating}', 'labels_url': 'https://api.github.com/repos/vmware/pyvco/labels{/name}', 'releases_url': 'https://api.github.com/repos/vmware/pyvco/releases{/id}', 'deployments_url': 'https://api.github.com/repos/vmware/pyvco/deployments', 'created_at': '2011-03-23T18:18:26Z', 'updated_at': '2017-03-07T10:31:57Z', 'pushed_at': '2010-11-07T15:55:51Z', 'git_url': 'git://github.com/vmware/pyvco.git', 'ssh_url': 'git@github.com:vmware/pyvco.git', 'clone_url': 'https://github.com/vmware/pyvco.git', 'svn_url': 'https://github.com/vmware/pyvco', 'homepage': 'http://sigma.github.com/vmw.vco', 'size': 225, 'stargazers_count': 11, 'watchers_count': 11, 'language': 'Python', 'has_issues': False, 'has_projects': True, 'has_downloads': True, 'has_wiki': True, 'has_pages': True, 'forks_count': 4, 'mirror_url': None, 'archived': False, 'open_issues_count': 0, 'license': {'key': 'mit', 'name': 'MIT License', 'spdx_id': 'MIT', 'url': 'https://api.github.com/licenses/mit'}, 'forks': 4, 'open_issues': 0, 'watchers': 11, 'default_branch': 'master', 'permissions': {'admin': False, 'push': False, 'pull': True}}
In [308]:
def get_forks(r):
    return r['forks']

for r in sorted(repos, key=get_forks, reverse=True):
    print(r['name'], r['forks'])
pyvmomi 467
pyvmomi-community-samples 394
open-vm-tools 160
rbvmomi 154
vmware-vcenter 87
rvc 46
vcloud-rest 37
webcommander 32
vmware-vmware_lib 24
vmware-vcsa 18
pg_rewind 18
pyvmomi-tools 17
CloudFS 16
thinapp_factory 16
jenkins-reviewbot 12
upgrade-framework 11
vprobe-toolkit 9
vmware-vshield 6
lmock 5
pyvco 4
vmware-cassandra 4
data-driven-framework 3
vcd-nclient 2
FireBreath 2
vco-powershel-plugin 2
weasel 1
saml20serviceprovider 1
GemstoneWebTools 0
dbeekeeper 0
vmware-java 0
In [324]:
for r in sorted(repos, key=get_forks, reverse=True)[:5]:
    print(r['full_name'], r['forks'])
vmware/pyvmomi 467
vmware/pyvmomi-community-samples 394
vmware/open-vm-tools 160
vmware/rbvmomi 154
vmware/vmware-vcenter 87
In [321]:
def get_top_contributors(name):
    url = "https://api.github.com/repos/{}/stats/contributors".format(name)
    resp = requests.get(url)
    print(resp.status_code)
    contributors = resp.json()
    return contributors
In [325]:
contribs = get_top_contributors("vmware/pyvmomi")
200
In [326]:
type(contribs)
Out[326]:
list
In [333]:
c = contribs[0]['author']
print(c['login'], contribs[0]['total'])
magianxd 1
In [334]:
for c in sorted(contribs, key=lambda x:x['total'], reverse=True)[:5]:
    print(c['author']['login'], c['total'])
tianhao64 63
hartsock 54
michaelrice 10
wiggin15 7
mstagi 7

problem find distance between two cities using google api.

In [353]:
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
In [354]:
distance("pune", "mumbai")
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-354-af968abf7bd0> in <module>()
----> 1 distance("pune", "mumbai")

<ipython-input-353-5a6ed3775507> in distance(source, dest)
      4                                     "destinations":dest,
      5                                     "units":"metric"})
----> 6     data = resp.json()
      7     return data

/home/vikrant/usr/local/anaconda3/lib/python3.6/site-packages/requests/models.py in json(self, **kwargs)
    883                     # used.
    884                     pass
--> 885         return complexjson.loads(self.text, **kwargs)
    886 
    887     @property

/home/vikrant/usr/local/anaconda3/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder

/home/vikrant/usr/local/anaconda3/lib/python3.6/json/decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

/home/vikrant/usr/local/anaconda3/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)

References

Python practice book

Project Euler

Python docs

SICP

Suggested mini projects.

  • directorytree - print directory tree
  • testing
  • backup scripts
  • sockets , try to create your own simple httpserver
In [ ]: