Python Training at Arcesium - Day 3

Oct 25-27, 2017 Vikrant Patil

These notes are available online at http://notes.pipal.in/2017/arcesium-oct-python/day3.html

© Pipal Academy LLP

Day 1 | Day 2 | Day 3

EXAMPLE

Implement unix command wc

In [1]:
%%file wc.py
"""
implements unix command wc
"""
import sys

def line_count(f):
    return len(open(f).readlines())

def word_count(f):
    return len(open(f).read().split())

def char_count(f):
    return len(open(f).read())

if __name__ == "__main__":
    file = sys.argv[1]
    print(line_count(file), word_count(file), char_count(file), file)
Writing wc.py
In [2]:
!python wc.py data.txt
21 144 856 data.txt
In [3]:
!wc data.txt
 20 144 856 data.txt
  • Find file with max number of lines in current directory
  • How about finding a file with maximum number of words?
In [4]:
%%file wc1.py
import sys

def line_count(f):
    return len(open(f).readlines())

def word_count(f):
    return len(open(f).read().split())

def char_count(f):
    return len(open(f).read())


file = sys.argv[1]
print(line_count(file), word_count(file), char_count(file), file)
Writing wc1.py
In [5]:
!python wc1.py data.txt
21 144 856 data.txt
In [6]:
import wc1
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-6-33150c973d98> in <module>()
----> 1 import wc1

/home/vikrant/trainings/2017/arcesium-oct-python/wc1.py in <module>()
     12 
     13 file = sys.argv[1]
---> 14 print(line_count(file), word_count(file), char_count(file), file)

/home/vikrant/trainings/2017/arcesium-oct-python/wc1.py in line_count(f)
      2 
      3 def line_count(f):
----> 4     return len(open(f).readlines())
      5 
      6 def word_count(f):

FileNotFoundError: [Errno 2] No such file or directory: '-f'
In [8]:
import os
files = [f for f in os.listdir(os.getcwd()) if os.path.isfile(f)]
In [9]:
import wc
In [10]:
max(files, key=wc.line_count)
Out[10]:
'day1.html'
In [12]:
max(files, key=wc.word_count)
Out[12]:
'day1.html'

Writing files

In [14]:
f = open("primes.txt", "w")
f.write("two\n")
f.write("three\n")
f.write("five\n")
f.close() #unless file handle is closed contents will not be flushed to disk
In [15]:
!python cat.py primes.txt
two
three
five
In [17]:
f = open("primes.txt", "a")
f.write("seven\n")
f.write("eleven\n")
f.close()
In [18]:
!python cat.py primes.txt
two
three
five
seven
eleven

Similarly there are different modes for reading and writing as given below

  1. rb => read in binary mode
  2. wb => write in binary mode
  3. ab => append in binary mode
In [19]:
open("primes.txt", "r").read() # text mode read
Out[19]:
'two\nthree\nfive\nseven\neleven\n'
In [20]:
open("primes.txt", "rb").read() # read in binary mode
Out[20]:
b'two\nthree\nfive\nseven\neleven\n'
In [21]:
f = open("binarydata.bin", "wb")
f.write(b"\x025x082")
f.close()
In [22]:
open("binarydata.bin", "rb").read()
Out[22]:
b'\x025x082'
In [23]:
f = open("binarydata.bin", "ab")
f.write(b'hello')
f.close()
In [24]:
open("binarydata.bin", "rb").read()
Out[24]:
b'\x025x082hello'

with block

In [25]:
with open("primes.txt", "a") as f:
    f.write("thirteen\n")

open("primes.txt").read()
Out[25]:
'two\nthree\nfive\nseven\neleven\nthirteen\n'
In [29]:
with open("regional.txt", "w", encoding="utf-16") as reginal:
    reginal.write("मआखक")
    
open("regional.txt", encoding="utf-16").read()
Out[29]:
'मआखक'

problems

  • Write a function to write multiplication tables upto 5 in csv fromat as given below
    1,2,3,4,5
    2,4,6,8,10
    .
    .
    .
    10,20,30,40,50
  • Write python module tabulate.py which reads csv file data and writes it in another text file in prety printed table as shown below
python tabulate.py tables.csv tabulated.txt
cat tabulated.txt
1  2  3
2  4  6
3  6  9
4  8  12
.
.
10 20 30
In [35]:
def tables(n):
    return [[str(i*j) for i in range(1,n+1)] for j in range(1,11)]

def write_csv(data, filename):
    
    with open(filename, "w") as file:
        for row in data:
            line = ",".join(row)
            file.write(line + "\n")

    
In [36]:
d = tables(5)
In [37]:
d
Out[37]:
[['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 [38]:
write_csv(d, "tables.csv")
In [39]:
!cat tables.csv
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 [43]:
def csv_parser(filename):
    lines = open(filename).read().strip().split("\n")
    #lines = open(filename).readlines()
    data = [line.split(",") for line in lines]
    return data
In [44]:
csv_parser("tables.csv")
Out[44]:
[['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 [48]:
%%file tabulate.py
import sys

def csv_parser(filename):
    lines = open(filename).read().strip().split("\n")
    #lines = open(filename).readlines()
    data = [line.split(",") for line in lines]
    return data

def tabulate(csvdata, filename):
    with open(filename, "w") as f:
        longestword = max([max(row, key=len) for row in csvdata], key=len)
        maxlength = len(longestword)
        for row in data: 
            f.write(" ".join([word.rjust(maxlength) for word in row]))
            f.write("\n")
        
if __name__ == "__main__":
    data = csv_parser(sys.argv[1])
    outfile = sys.argv[2]
    tabulate(data, outfile)
Overwriting tabulate.py
In [49]:
!python tabulate.py tables.csv tabulated.txt
In [50]:
!cat tabulated.txt
 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

Writing to standard output and standard error

In [51]:
import sys
In [52]:
sys.stdout.write("Hello pyhon")
Hello pyhon
In [53]:
sys.stderr.write("Errr...some Exception!")
Errr...some Exception!

working with dictionaries

In [55]:
author = {"name":"lewis carrol",
          "books":["alice in wonderland", "looking through the glass"],
          "language":"english"
         }
In [56]:
author['name']
Out[56]:
'lewis carrol'
In [57]:
print(author)
{'name': 'lewis carrol', 'books': ['alice in wonderland', 'looking through the glass'], 'language': 'english'}
In [58]:
author['name'] = "lewis"
In [59]:
print(author)
{'name': 'lewis', 'books': ['alice in wonderland', 'looking through the glass'], 'language': 'english'}
In [60]:
del author['books']
In [61]:
print(author)
{'name': 'lewis', 'language': 'english'}
In [62]:
"name" in author
Out[62]:
True
In [63]:
"language" in author
Out[63]:
True
In [64]:
"books" in author
Out[64]:
False
In [67]:
author['language']
Out[67]:
'english'
In [68]:
author['books']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-68-7b693b1298ba> in <module>()
----> 1 author['books']

KeyError: 'books'
In [69]:
author.get("books", [])
Out[69]:
[]

Iterating over dictionaries

In [70]:
d = {"one":1, "two":2, "three":3}
In [71]:
len(d)
Out[71]:
3
In [74]:
for key in d.keys():#iterating over keys
    print(key, d[key])
one 1
two 2
three 3
In [75]:
for values in d.values():#iterating over values
    print(values)
1
2
3
In [76]:
for key, value in d.items(): # iterating with keys and values together
    print(key, value)
one 1
two 2
three 3
In [78]:
for item in d: # iterating over dictionary gives only keys
    print(item)
one
two
three
In [79]:
numbers = [("one", 1), ("two", 2), ("three",3)]
In [80]:
dict(numbers)
Out[80]:
{'one': 1, 'three': 3, 'two': 2}
In [82]:
dict([reversed(item) for item in numbers])
Out[82]:
{1: 'one', 2: 'two', 3: 'three'}
In [83]:
dict(zip(["one", "two", "three"], [1,2,3]))
Out[83]:
{'one': 1, 'three': 3, 'two': 2}
In [85]:
items = ("Pen", "Pencil", "Colorbox")
prices = (25, 10, 50)
cart = dict(zip(items, prices))
In [86]:
for item, price in cart.items():
    print(item.rjust(8), price)
print("-"*12)
print("Total".rjust(8), sum(cart.values()))
     Pen 25
  Pencil 10
Colorbox 50
------------
   Total 85
In [91]:
keys = [key for key in cart.keys()]
In [92]:
[cart[key] for key in cart.keys() ]
Out[92]:
[25, 10, 50]

More Examples

write a program to calculate frequencies of words in file given below

In [93]:
%%file words.txt
five
five four
five four three
five four threes two
five four three two one
five six seven eight
five six seven
five six
five
Writing words.txt
In [96]:
%%file wordfreq.py
import sys

def get_words(file):
    return open(file).read().split()


def wordfreq(words):
    freq = {}
    for word in words:
        if word in freq:
            freq[word] += 1
        else:
            freq[word] = 1
    return freq


if __name__ == "__main__":
    words = get_words(sys.argv[1])
    freq = wordfreq(words)
    print(freq)
Overwriting wordfreq.py
In [97]:
!python wordfreq.py words.txt
{'five': 9, 'four': 4, 'three': 2, 'threes': 1, 'two': 2, 'one': 1, 'six': 3, 'seven': 2, 'eight': 1}
In [98]:
%%file wordfreq1.py
import sys

def get_words(file):
    return open(file).read().split()


def wordfreq(words):
    freq = {}
    for word in words:
        freq[word] = freq.get(word, 0) + 1
    return freq


if __name__ == "__main__":
    words = get_words(sys.argv[1])
    freq = wordfreq(words)
    print(freq)
Writing wordfreq1.py
In [99]:
!python wordfreq1.py words.txt
{'five': 9, 'four': 4, 'three': 2, 'threes': 1, 'two': 2, 'one': 1, 'six': 3, 'seven': 2, 'eight': 1}
In [100]:
def wordfreq2(words):
    freq = {}
    uniqwords = set(words)
    for word in uniqwords:
        freq[word] = words.count(word)
        
    return freq
    
In [101]:
import wordfreq
In [102]:
words = wordfreq.get_words("words.txt")
In [103]:
freq = wordfreq2(words)
In [106]:
for w, f in freq.items():
    print(w.rjust(6), f)
  four 4
 three 2
threes 1
 eight 1
  five 9
   six 3
 seven 2
   two 2
   one 1
In [108]:
for k,v in sorted(freq.items()):
    print(k.rjust(6), v)
 eight 1
  five 9
  four 4
   one 1
 seven 2
   six 3
 three 2
threes 1
   two 2
In [109]:
for k,v in sorted(freq.items(), key=lambda x :x[1]):
    print(k.rjust(6), v)
threes 1
 eight 1
   one 1
 three 2
 seven 2
   two 2
   six 3
  four 4
  five 9
In [110]:
for k,v in sorted(freq.items(),reverse=True, key=lambda x :x[1]):
    print(k.rjust(6), v)
  five 9
  four 4
   six 3
 three 2
 seven 2
   two 2
threes 1
 eight 1
   one 1
In [111]:
for k,v in sorted(freq.items(),reverse=True, key=lambda x :x[1]):
    print(k.rjust(6), "*"*v)
  five *********
  four ****
   six ***
 three **
 seven **
   two **
threes *
 eight *
   one *
In [112]:
team = {"david":"USA", "anand":"India", "linus":"USA", "Noufal":"India", "alice":"UK"}
In [114]:
[name for name in team.keys() if team[name]=="USA"]
Out[114]:
['david', 'linus']
In [115]:
[name for name in team.keys() if team[name]=="India"]
Out[115]:
['anand', 'Noufal']

Pitfalls

In [116]:
x = [1,2,3,4]
y = x
y.append(5)
print(x)
[1, 2, 3, 4, 5]
In [117]:
x = [1,2,3,4]
y = x
y =[1,2,3]
print(x)
[1, 2, 3, 4]
In [118]:
x = 1
y = x
y = 2
print(x)
1

Classes

In [119]:
class Complex:
    def __init__(self, r, i):
        self.real = r
        self.imaginary = i
        
    def get_real(self):
        return self.real

    def get_imaginary(self):
        return self.imaginary
    
In [120]:
c = Complex(10,5)
In [122]:
print(c)
<__main__.Complex object at 0x7f2a91b75e10>
In [123]:
type(c)
Out[123]:
__main__.Complex
In [125]:
isinstance(c, Complex)
Out[125]:
True
In [126]:
c.get_real()
Out[126]:
10
In [127]:
c.real
Out[127]:
10
In [128]:
c.imaginary
Out[128]:
5
In [135]:
class Complex:
    def __init__(self, r, i):
        self.real = r
        self.imaginary = i
        
    def get_real(self):
        return self.real

    def get_imaginary(self):
        return self.imaginary
    
    def display(self):
        print(self.real, " + ", self.imaginary , "j")
        
    def add(self, c):
        _real = self.real + c.get_real()
        _imag = self.imaginary + c.get_imaginary()
        return Complex(_real, _imag)
In [130]:
c1 = Complex(1,2) 
In [131]:
c1.display()
1  +  2 j
In [132]:
c2 = Complex(3,4)
In [133]:
c3 = c1.add(c2)
In [134]:
c3.display()
4  +  6 j

Why classes ?

In [137]:
%%file bank0.py

balance = 0

def deposit(amount):
    global balance
    balance = balance + amount
    
    
def withdraw(amount):
    global balance
    balance = balance - amount
    
def get_balance():
    return balance


def main():
    deposit(100)
    withdraw(20)
    print(get_balance())
    deposit(50)
    print(get_balance())
    
if __name__ == "__main__":
    main()
Overwriting bank0.py
In [138]:
!python bank0.py 
80
130
In [141]:
%%file bank1.py

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

def deposite(account, amount):
    account['balance'] += amount
    
def withdraw(account, amount):
    account['balance'] -= amount
    
def get_balance(account):
    return account['balance']

def main():
    a1 = make_account()
    a2 = make_account()
    deposite(a1, 100)
    withdraw(a1, 30)
    deposite(a2, 200)
    withdraw(a2, 20)
    print("a1 ", get_balance(a1))
    print("a2 ", get_balance(a2))
    
if __name__ == "__main__":
    main()
Overwriting bank1.py
In [142]:
!python bank1.py
a1  70
a2  180
In [147]:
%%file bank2.py

class BankAccount:
    def __init__(self):
        self._balance = 0 
        
    def deposit(self, amount):
        self._balance += amount
        
    def withdraw(self, amount):
        self._balance -= amount
        
    def get_balance(self):
        return self._balance
    
def main():
    a1 = BankAccount()
    a2 = BankAccount()
    a1.deposit(100)
    a1.withdraw(30)
    a2.deposit(200)
    a2.withdraw(20)
    print("a1 ", a1.get_balance())
    print("a2 ", a2.get_balance())
    
if __name__ == "__main__":
    main()
Overwriting bank2.py
In [148]:
!python bank2.py
a1  70
a2  180
In [153]:
class A:
    def func():
        print("Print from A->func")
        
    def func2(self):
        print("Print from A->func2", self)
In [154]:
a = A()
In [155]:
a.func()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-155-4ac12b9f93e7> in <module>()
----> 1 a.func()

TypeError: func() takes 0 positional arguments but 1 was given
In [156]:
A.func()
Print from A->func
In [158]:
A.func2(a)
Print from A->func2 <__main__.A object at 0x7f2a91b24c88>
In [159]:
class Foo:
    pass
In [160]:
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
        
In [169]:
class ColoredPoint(Point):
    color = (0,0,0) #rgb
    
    def get_color(self):
        return self.color
In [170]:
c = ColoredPoint(10, 5)
In [171]:
print(c.x, c.y)
10 5
In [172]:
c.get_color()
Out[172]:
(0, 0, 0)

problem

  • Write a class Timer to measure time taken in a task. The class should have start and stop methods and it should be able to find time taken between call of start and call of end. **Hint: time.time()**
t = Timer()
t.start()
do_some_stuff()
t.stop()
print("Time taken by task", t.get_time_taken())
In [173]:
import time
In [174]:
time.time()
Out[174]:
1509097826.210927

Exceptions

In [176]:
h
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-176-01fbdc44ef81> in <module>()
----> 1 h

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

ValueError: invalid literal for int() with base 10: 'hello'
In [178]:
"2" * "3"
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-178-ff03144c1b70> in <module>()
----> 1 "2" * "3"

TypeError: can't multiply sequence by non-int of type 'str'
In [183]:
try:
    int("hello")
except TypeError as e:
    print("Handled TypeError", e)
except NameError as e:
    print("Handled NameError", e)
except ValueError as e:
    print("Handled ValueError", e)
except Exception as e:
    print ("Else .. ", e)
Handled ValueError invalid literal for int() with base 10: 'hello'
In [184]:
def get_value(dictionary, key, default):
    try:
        return dictionary[key]
    except KeyError as k:
        print("Key not found returning default")
        return default
In [185]:
d = {"a":1, "b":2}
In [186]:
get_value(d, "c", 0)
Key not found returning default
Out[186]:
0
In [187]:
%%file missing.txt
1
2
3
4
N/A
6
7
8
12
Nan
10
Writing missing.txt
In [188]:
def parseint(strnum):
    try:
        return int(strnum)
    except ValueError as e:
        return 0
In [193]:
def read_with_missing(file):
    with open(file) as f:
        return [parseint(line.strip()) for line in f.readlines()]
In [194]:
read_with_missing("missing.txt")
Out[194]:
[1, 2, 3, 4, 0, 6, 7, 8, 12, 0, 10]

Downloading stuff from internet

In [195]:
from urllib.request import urlopen
In [196]:
response = urlopen("http://httpbin.org/html")
In [197]:
response
Out[197]:
<http.client.HTTPResponse at 0x7f2a91b75860>
In [198]:
contents = response.read()
In [199]:
contents[:100]
Out[199]:
b'<!DOCTYPE html>\n<html>\n  <head>\n  </head>\n  <body>\n      <h1>Herman Melville - Moby-Dick</h1>\n\n     '
In [200]:
html = contents.decode("utf-8")
In [202]:
print(html[:400])
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
      <h1>Herman Melville - Moby-Dick</h1>

      <div>
        <p>
          Availing himself of the mild, summer-cool weather that now reigned in these latitudes, and in preparation for the peculiarly active pursuits shortly to be anticipated, Perth, the begrimed, blistered old blacksmith, had not removed his portable forge to the hold again, af
In [203]:
response.status
Out[203]:
200

Third party library requests makes it very easy to work with http reuests install as given below

pip3 install requests
In [204]:
import requests
In [205]:
response = requests.get("http://httpbin.org/html")
In [206]:
print(response.text[:400])
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
      <h1>Herman Melville - Moby-Dick</h1>

      <div>
        <p>
          Availing himself of the mild, summer-cool weather that now reigned in these latitudes, and in preparation for the peculiarly active pursuits shortly to be anticipated, Perth, the begrimed, blistered old blacksmith, had not removed his portable forge to the hold again, af
In [207]:
response.headers
Out[207]:
{'Connection': 'keep-alive', 'Server': 'meinheld/0.6.1', 'Date': 'Fri, 27 Oct 2017 10:46:39 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Content-Length': '3741', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': 'true', 'X-Powered-By': 'Flask', 'X-Processed-Time': '0.000417947769165', 'Via': '1.1 vegur'}
In [208]:
response.status_code
Out[208]:
200
In [209]:
response = requests.get("http://httpbin.org/get", params={'query':"python demo", "page":"2"})
In [210]:
print(response.text)
{
  "args": {
    "page": "2", 
    "query": "python demo"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.14.2"
  }, 
  "origin": "115.112.81.240", 
  "url": "http://httpbin.org/get?query=python+demo&page=2"
}

In [211]:
response = requests.post("http://httpbin.org/post", data="Plain text")
print(response.text)
{
  "args": {}, 
  "data": "Plain text", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip, deflate", 
    "Connection": "close", 
    "Content-Length": "10", 
    "Host": "httpbin.org", 
    "User-Agent": "python-requests/2.14.2"
  }, 
  "json": null, 
  "origin": "115.112.81.240", 
  "url": "http://httpbin.org/post"
}

In [212]:
response = requests.post("http://httpbin.org/post", data={"name":"python", "version":"3.6"})
print(response.text)
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "name": "python", 
    "version": "3.6"
  }, 
  "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": null, 
  "origin": "115.112.81.240", 
  "url": "http://httpbin.org/post"
}

In [213]:
import requests
url = "https://api.github.com/orgs/google/repos"
repos = requests.get(url).json()
In [214]:
type(repos)
Out[214]:
list
In [215]:
for repo in repos:
    print(repo['full_name'], repo['forks'])
google/upb 109
google/truth 141
google/ruby-openid-apps-discovery 14
google/signet 116
google/autoparse 30
google/gitkit-ruby 0
google/google-api-ruby-client 492
google/anvil-build 12
google/googletv-android-samples 71
google/ChannelPlate 6
google/GL-Shader-Validator 11
google/qpp 4
google/CSP-Validator 10
google/embed-dart-vm 13
google/module-server 36
google/cxx-std-draft 10
google/filesystem-proposal 9
google/libcxx 26
google/tracing-framework 192
google/namebench 64
google/devtoolsExtended 15
google/sirius 0
google/testRunner 6
google/crx2app 8
google/episodes.dart 18
google/cpp-netlib 44
google/dagger 1046
google/ios-webkit-debug-proxy 278
google/google.github.io 33
google/kratu 30
In [216]:
def get_forks(repo):
    return repo['forks']
In [217]:
toprepos = sorted(repos, key= get_forks, reverse=True)[:5]
In [219]:
for repo in toprepos:
    print(repo['full_name'], repo['forks'])
google/dagger 1046
google/google-api-ruby-client 492
google/ios-webkit-debug-proxy 278
google/tracing-framework 192
google/truth 141
In [233]:
def get_top_contributor(reponame):
    url = "https://api.github.com/repos/{}/stats/contributors".format(reponame)
    print(url)
    contributors = requests.get(url).json()
    contributors = sorted(contributors, key=lambda x:x['total'], reverse=True)
    for c in contributors[:5]:
        print(c['author']['login'], c['total'])
In [234]:
get_top_contributor("google/dagger")
https://api.github.com/repos/google/dagger/stats/contributors
cgruber 354
ronshapiro 255
netdpb 191
gk5885 190
jbeder 104
In [226]:
import json
In [228]:
s = json.dumps({"test":"x","seq":[1,2,3,4]})
In [229]:
s
Out[229]:
'{"test": "x", "seq": [1, 2, 3, 4]}'
In [230]:
json.loads(s)
Out[230]:
{'seq': [1, 2, 3, 4], 'test': 'x'}

problem Find distance between two cities using google maps api

In [235]:
def distance(origin, destination):
    url = "https://maps.googleapis.com/maps/api/distancematrix/json"
    response = requests.get(url, params={"origins":origin, 
                                         "destinations":destination,
                                         "units":"metric"})
    data = response.json()
    return data['rows'][0]['elements'][0]['distance']['text']
In [237]:
distance("hyderabad", "pune")
Out[237]:
'561 km'
In [238]:
distance("hyderabad", "delhi")
Out[238]:
'1,578 km'
In [240]:
distance("hyderabad", "mumbai")
Out[240]:
'707 km'

Writing commandline applications

In [241]:
!ls
add.py		day2.html      ls.py	     push	    Untitled.ipynb
bank0.py	day2.ipynb     main.py	     __pycache__    wc1.py
bank1.py	day3.html      Makefile      rectangle.py   wc.py
bank2.py	day3.ipynb     missing.txt   regional.txt   wordfreq1.py
binarydata.bin	echo.py        mymodule1.py  square.py	    wordfreq.py
cat.py		feedback.txt   mymodule2.py  tables.csv     words.txt
circle.py	feedback.txt~  mymodule3.py  tabulated.txt
data.txt	function1.py   mymodule.py   tabulate.py
day1.html	functions.py   primes.txt    three.txt
day1.ipynb	head.py        prime.txt     Untitled.html
In [242]:
!ls /home/
lost+found  vikrant
In [243]:
!cp day1.html /tmp/
In [244]:
!ls -l
total 1912
-rw-rw-r-- 1 vikrant vikrant     99 Oct 25 16:11 add.py
-rw-rw-r-- 1 vikrant vikrant    366 Oct 27 14:37 bank0.py
-rw-rw-r-- 1 vikrant vikrant    498 Oct 27 14:44 bank1.py
-rw-rw-r-- 1 vikrant vikrant    536 Oct 27 14:49 bank2.py
-rw-rw-r-- 1 vikrant vikrant     11 Oct 27 10:46 binarydata.bin
-rw-rw-r-- 1 vikrant vikrant    139 Oct 26 17:20 cat.py
-rw-rw-r-- 1 vikrant vikrant    219 Oct 25 15:27 circle.py
-rw-rw-r-- 1 vikrant vikrant    856 Oct 26 16:54 data.txt
-rw-rw-r-- 1 vikrant vikrant 486233 Oct 25 17:14 day1.html
-rw-rw-r-- 1 vikrant vikrant 117257 Oct 25 17:14 day1.ipynb
-rw-rw-r-- 1 vikrant vikrant 394412 Oct 26 17:22 day2.html
-rw-rw-r-- 1 vikrant vikrant  71535 Oct 26 17:22 day2.ipynb
-rw-rw-r-- 1 vikrant vikrant 398707 Oct 27 17:02 day3.html
-rw-rw-r-- 1 vikrant vikrant  69681 Oct 27 17:01 day3.ipynb
-rw-rw-r-- 1 vikrant vikrant     41 Oct 25 16:12 echo.py
-rw-rw-r-- 1 vikrant vikrant     45 Oct 26 22:34 feedback.txt
-rw-rw-r-- 1 vikrant vikrant     44 Oct 26 22:33 feedback.txt~
-rw-rw-r-- 1 vikrant vikrant   1107 Oct 26 15:18 function1.py
-rw-rw-r-- 1 vikrant vikrant    684 Oct 26 15:04 functions.py
-rw-rw-r-- 1 vikrant vikrant    210 Oct 26 17:22 head.py
-rw-rw-r-- 1 vikrant vikrant    147 Oct 26 10:46 ls.py
-rw-rw-r-- 1 vikrant vikrant     76 Oct 25 15:33 main.py
-rw-r--r-- 1 vikrant vikrant    617 Oct 25 09:23 Makefile
-rw-rw-r-- 1 vikrant vikrant     27 Oct 27 16:03 missing.txt
-rw-rw-r-- 1 vikrant vikrant     27 Oct 25 15:17 mymodule1.py
-rw-rw-r-- 1 vikrant vikrant    106 Oct 25 15:20 mymodule2.py
-rw-rw-r-- 1 vikrant vikrant     85 Oct 25 15:22 mymodule3.py
-rw-rw-r-- 1 vikrant vikrant     57 Oct 25 15:15 mymodule.py
-rw-rw-r-- 1 vikrant vikrant     37 Oct 27 10:48 primes.txt
-rw-rw-r-- 1 vikrant vikrant     13 Oct 27 10:41 prime.txt
-rw-rw-r-- 1 vikrant vikrant      0 Oct 27 17:02 push
drwxrwxr-x 2 vikrant vikrant   4096 Oct 27 12:49 __pycache__
-rw-rw-r-- 1 vikrant vikrant    250 Oct 25 15:29 rectangle.py
-rw-rw-r-- 1 vikrant vikrant     10 Oct 27 10:53 regional.txt
-rw-rw-r-- 1 vikrant vikrant     80 Oct 25 16:10 square.py
-rw-rw-r-- 1 vikrant vikrant    131 Oct 27 11:33 tables.csv
-rw-rw-r-- 1 vikrant vikrant    150 Oct 27 11:45 tabulated.txt
-rw-rw-r-- 1 vikrant vikrant    623 Oct 27 11:45 tabulate.py
-rw-rw-r-- 1 vikrant vikrant     13 Oct 26 16:53 three.txt
-rw-rw-r-- 1 vikrant vikrant 248809 Oct 25 13:53 Untitled.html
-rw-rw-r-- 1 vikrant vikrant     72 Oct 25 10:14 Untitled.ipynb
-rw-rw-r-- 1 vikrant vikrant    263 Oct 27 10:33 wc1.py
-rw-rw-r-- 1 vikrant vikrant    332 Oct 27 10:29 wc.py
-rw-rw-r-- 1 vikrant vikrant    295 Oct 27 12:47 wordfreq1.py
-rw-rw-r-- 1 vikrant vikrant    346 Oct 27 12:45 wordfreq.py
-rw-rw-r-- 1 vikrant vikrant    125 Oct 27 12:26 words.txt
In [245]:
!ls --help
Usage: ls [OPTION]... [FILE]...
List information about the FILEs (the current directory by default).
Sort entries alphabetically if none of -cftuvSUX nor --sort is specified.

Mandatory arguments to long options are mandatory for short options too.
  -a, --all                  do not ignore entries starting with .
  -A, --almost-all           do not list implied . and ..
      --author               with -l, print the author of each file
  -b, --escape               print C-style escapes for nongraphic characters
      --block-size=SIZE      scale sizes by SIZE before printing them; e.g.,
                               '--block-size=M' prints sizes in units of
                               1,048,576 bytes; see SIZE format below
  -B, --ignore-backups       do not list implied entries ending with ~
  -c                         with -lt: sort by, and show, ctime (time of last
                               modification of file status information);
                               with -l: show ctime and sort by name;
                               otherwise: sort by ctime, newest first
  -C                         list entries by columns
      --color[=WHEN]         colorize the output; WHEN can be 'always' (default
                               if omitted), 'auto', or 'never'; more info below
  -d, --directory            list directories themselves, not their contents
  -D, --dired                generate output designed for Emacs' dired mode
  -f                         do not sort, enable -aU, disable -ls --color
  -F, --classify             append indicator (one of */=>@|) to entries
      --file-type            likewise, except do not append '*'
      --format=WORD          across -x, commas -m, horizontal -x, long -l,
                               single-column -1, verbose -l, vertical -C
      --full-time            like -l --time-style=full-iso
  -g                         like -l, but do not list owner
      --group-directories-first
                             group directories before files;
                               can be augmented with a --sort option, but any
                               use of --sort=none (-U) disables grouping
  -G, --no-group             in a long listing, don't print group names
  -h, --human-readable       with -l and/or -s, print human readable sizes
                               (e.g., 1K 234M 2G)
      --si                   likewise, but use powers of 1000 not 1024
  -H, --dereference-command-line
                             follow symbolic links listed on the command line
      --dereference-command-line-symlink-to-dir
                             follow each command line symbolic link
                               that points to a directory
      --hide=PATTERN         do not list implied entries matching shell PATTERN
                               (overridden by -a or -A)
      --indicator-style=WORD  append indicator with style WORD to entry names:
                               none (default), slash (-p),
                               file-type (--file-type), classify (-F)
  -i, --inode                print the index number of each file
  -I, --ignore=PATTERN       do not list implied entries matching shell PATTERN
  -k, --kibibytes            default to 1024-byte blocks for disk usage
  -l                         use a long listing format
  -L, --dereference          when showing file information for a symbolic
                               link, show information for the file the link
                               references rather than for the link itself
  -m                         fill width with a comma separated list of entries
  -n, --numeric-uid-gid      like -l, but list numeric user and group IDs
  -N, --literal              print raw entry names (don't treat e.g. control
                               characters specially)
  -o                         like -l, but do not list group information
  -p, --indicator-style=slash
                             append / indicator to directories
  -q, --hide-control-chars   print ? instead of nongraphic characters
      --show-control-chars   show nongraphic characters as-is (the default,
                               unless program is 'ls' and output is a terminal)
  -Q, --quote-name           enclose entry names in double quotes
      --quoting-style=WORD   use quoting style WORD for entry names:
                               literal, locale, shell, shell-always,
                               shell-escape, shell-escape-always, c, escape
  -r, --reverse              reverse order while sorting
  -R, --recursive            list subdirectories recursively
  -s, --size                 print the allocated size of each file, in blocks
  -S                         sort by file size, largest first
      --sort=WORD            sort by WORD instead of name: none (-U), size (-S),
                               time (-t), version (-v), extension (-X)
      --time=WORD            with -l, show time as WORD instead of default
                               modification time: atime or access or use (-u);
                               ctime or status (-c); also use specified time
                               as sort key if --sort=time (newest first)
      --time-style=STYLE     with -l, show times using style STYLE:
                               full-iso, long-iso, iso, locale, or +FORMAT;
                               FORMAT is interpreted like in 'date'; if FORMAT
                               is FORMAT1<newline>FORMAT2, then FORMAT1 applies
                               to non-recent files and FORMAT2 to recent files;
                               if STYLE is prefixed with 'posix-', STYLE
                               takes effect only outside the POSIX locale
  -t                         sort by modification time, newest first
  -T, --tabsize=COLS         assume tab stops at each COLS instead of 8
  -u                         with -lt: sort by, and show, access time;
                               with -l: show access time and sort by name;
                               otherwise: sort by access time, newest first
  -U                         do not sort; list entries in directory order
  -v                         natural sort of (version) numbers within text
  -w, --width=COLS           set output width to COLS.  0 means no limit
  -x                         list entries by lines instead of by columns
  -X                         sort alphabetically by entry extension
  -Z, --context              print any security context of each file
  -1                         list one file per line.  Avoid '\n' with -q or -b
      --help     display this help and exit
      --version  output version information and exit

The SIZE argument is an integer and optional unit (example: 10K is 10*1024).
Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000).

Using color to distinguish file types is disabled both by default and
with --color=never.  With --color=auto, ls emits color codes only when
standard output is connected to a terminal.  The LS_COLORS environment
variable can change the settings.  Use the dircolors command to set it.

Exit status:
 0  if OK,
 1  if minor problems (e.g., cannot access subdirectory),
 2  if serious trouble (e.g., cannot access command-line argument).

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/ls>
or available locally via: info '(coreutils) ls invocation'
In [249]:
%%file fib.py
import argparse

def fib(n):
    prev, current = 1,1
    for i in range(2, n):
        current, prev = prev+current, current
        
    return current

def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument("n", help="n for computing nth fibonacci number",
                  type=int)
    return p.parse_args()

def main():
    args = parse_args()
    print(args)
    print(fib(args.n))
    
if __name__ == "__main__":
    main()
Overwriting fib.py
In [250]:
!python fib.py
usage: fib.py [-h] n
fib.py: error: the following arguments are required: n
In [251]:
!python fib.py -h
usage: fib.py [-h] n

positional arguments:
  n           n for computing nth fibonacci number

optional arguments:
  -h, --help  show this help message and exit
In [252]:
!python fib.py 10
Namespace(n=10)
55
In [253]:
%%file fib.py
import argparse

def fib(n):
    prev, current = 1,1
    for i in range(2, n):
        current, prev = prev+current, current
        
    return current

def printfinlist(n):
    prev, current = 1, 1
    
    for i in range(2, n):
        current, prev = prev+current, current
        print(current, end=" ")

def parse_args():
    p = argparse.ArgumentParser()
    p.add_argument("n", help="n for computing nth fibonacci number",
                  type=int)
    p.add_argument("-s","--sequence",
                  help = "Print sequence",
                  action="store_true")
    return p.parse_args()

def main():
    args = parse_args()
    print(args)
    if args.sequence:
        printfinlist(args.n)
    else:
        print(fib(args.n))
    
if __name__ == "__main__":
    main()
Overwriting fib.py
In [254]:
!python fib.py
usage: fib.py [-h] [-s] n
fib.py: error: the following arguments are required: n
In [255]:
!python fib.py -h
usage: fib.py [-h] [-s] n

positional arguments:
  n               n for computing nth fibonacci number

optional arguments:
  -h, --help      show this help message and exit
  -s, --sequence  Print sequence
In [256]:
!python fib.py -s 10
Namespace(n=10, sequence=True)
2 3 5 8 13 21 34 55 
In [257]:
!python fib.py 10
Namespace(n=10, sequence=False)
55

regular expressions

In [258]:
import re
pattern = re.compile("^def .*$")
s  = "def hello(name):"
s2 = "print('hello', name)"
In [259]:
pattern.match(s)
Out[259]:
<_sre.SRE_Match object; span=(0, 16), match='def hello(name):'>
In [260]:
pattern.match(s2)
In [ ]:
if pattern.match(s):

Further References

  1. Structure and Intepretation of computer programming - search google for SICP
  2. Python Practice Book
  3. google for python 3 docs
  4. python cookbook 3rd edition. by david beazley and others
In [ ]: