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
Implement unix command wc
%%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)
!python wc.py data.txt
!wc data.txt
%%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)
!python wc1.py data.txt
import wc1
import os
files = [f for f in os.listdir(os.getcwd()) if os.path.isfile(f)]
import wc
max(files, key=wc.line_count)
max(files, key=wc.word_count)
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
!python cat.py primes.txt
f = open("primes.txt", "a")
f.write("seven\n")
f.write("eleven\n")
f.close()
!python cat.py primes.txt
Similarly there are different modes for reading and writing as given below
open("primes.txt", "r").read() # text mode read
open("primes.txt", "rb").read() # read in binary mode
f = open("binarydata.bin", "wb")
f.write(b"\x025x082")
f.close()
open("binarydata.bin", "rb").read()
f = open("binarydata.bin", "ab")
f.write(b'hello')
f.close()
open("binarydata.bin", "rb").read()
with open("primes.txt", "a") as f:
f.write("thirteen\n")
open("primes.txt").read()
with open("regional.txt", "w", encoding="utf-16") as reginal:
reginal.write("मआखक")
open("regional.txt", encoding="utf-16").read()
problems
1,2,3,4,5
2,4,6,8,10
.
.
.
10,20,30,40,50
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
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")
d = tables(5)
d
write_csv(d, "tables.csv")
!cat tables.csv
def csv_parser(filename):
lines = open(filename).read().strip().split("\n")
#lines = open(filename).readlines()
data = [line.split(",") for line in lines]
return data
csv_parser("tables.csv")
%%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)
!python tabulate.py tables.csv tabulated.txt
!cat tabulated.txt
import sys
sys.stdout.write("Hello pyhon")
sys.stderr.write("Errr...some Exception!")
author = {"name":"lewis carrol",
"books":["alice in wonderland", "looking through the glass"],
"language":"english"
}
author['name']
print(author)
author['name'] = "lewis"
print(author)
del author['books']
print(author)
"name" in author
"language" in author
"books" in author
author['language']
author['books']
author.get("books", [])
d = {"one":1, "two":2, "three":3}
len(d)
for key in d.keys():#iterating over keys
print(key, d[key])
for values in d.values():#iterating over values
print(values)
for key, value in d.items(): # iterating with keys and values together
print(key, value)
for item in d: # iterating over dictionary gives only keys
print(item)
numbers = [("one", 1), ("two", 2), ("three",3)]
dict(numbers)
dict([reversed(item) for item in numbers])
dict(zip(["one", "two", "three"], [1,2,3]))
items = ("Pen", "Pencil", "Colorbox")
prices = (25, 10, 50)
cart = dict(zip(items, prices))
for item, price in cart.items():
print(item.rjust(8), price)
print("-"*12)
print("Total".rjust(8), sum(cart.values()))
keys = [key for key in cart.keys()]
[cart[key] for key in cart.keys() ]
write a program to calculate frequencies of words in file given below
%%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
%%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)
!python wordfreq.py words.txt
%%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)
!python wordfreq1.py words.txt
def wordfreq2(words):
freq = {}
uniqwords = set(words)
for word in uniqwords:
freq[word] = words.count(word)
return freq
import wordfreq
words = wordfreq.get_words("words.txt")
freq = wordfreq2(words)
for w, f in freq.items():
print(w.rjust(6), f)
for k,v in sorted(freq.items()):
print(k.rjust(6), v)
for k,v in sorted(freq.items(), key=lambda x :x[1]):
print(k.rjust(6), v)
for k,v in sorted(freq.items(),reverse=True, key=lambda x :x[1]):
print(k.rjust(6), v)
for k,v in sorted(freq.items(),reverse=True, key=lambda x :x[1]):
print(k.rjust(6), "*"*v)
team = {"david":"USA", "anand":"India", "linus":"USA", "Noufal":"India", "alice":"UK"}
[name for name in team.keys() if team[name]=="USA"]
[name for name in team.keys() if team[name]=="India"]
x = [1,2,3,4]
y = x
y.append(5)
print(x)
x = [1,2,3,4]
y = x
y =[1,2,3]
print(x)
x = 1
y = x
y = 2
print(x)
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
c = Complex(10,5)
print(c)
type(c)
isinstance(c, Complex)
c.get_real()
c.real
c.imaginary
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)
c1 = Complex(1,2)
c1.display()
c2 = Complex(3,4)
c3 = c1.add(c2)
c3.display()
%%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()
!python bank0.py
%%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()
!python bank1.py
%%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()
!python bank2.py
class A:
def func():
print("Print from A->func")
def func2(self):
print("Print from A->func2", self)
a = A()
a.func()
A.func()
A.func2(a)
class Foo:
pass
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class ColoredPoint(Point):
color = (0,0,0) #rgb
def get_color(self):
return self.color
c = ColoredPoint(10, 5)
print(c.x, c.y)
c.get_color()
problem
t = Timer()
t.start()
do_some_stuff()
t.stop()
print("Time taken by task", t.get_time_taken())
import time
time.time()
h
int("hello")
"2" * "3"
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)
def get_value(dictionary, key, default):
try:
return dictionary[key]
except KeyError as k:
print("Key not found returning default")
return default
d = {"a":1, "b":2}
get_value(d, "c", 0)
%%file missing.txt
1
2
3
4
N/A
6
7
8
12
Nan
10
def parseint(strnum):
try:
return int(strnum)
except ValueError as e:
return 0
def read_with_missing(file):
with open(file) as f:
return [parseint(line.strip()) for line in f.readlines()]
read_with_missing("missing.txt")
from urllib.request import urlopen
response = urlopen("http://httpbin.org/html")
response
contents = response.read()
contents[:100]
html = contents.decode("utf-8")
print(html[:400])
response.status
Third party library requests makes it very easy to work with http reuests
install as given below
pip3 install requests
import requests
response = requests.get("http://httpbin.org/html")
print(response.text[:400])
response.headers
response.status_code
response = requests.get("http://httpbin.org/get", params={'query':"python demo", "page":"2"})
print(response.text)
response = requests.post("http://httpbin.org/post", data="Plain text")
print(response.text)
response = requests.post("http://httpbin.org/post", data={"name":"python", "version":"3.6"})
print(response.text)
import requests
url = "https://api.github.com/orgs/google/repos"
repos = requests.get(url).json()
type(repos)
for repo in repos:
print(repo['full_name'], repo['forks'])
def get_forks(repo):
return repo['forks']
toprepos = sorted(repos, key= get_forks, reverse=True)[:5]
for repo in toprepos:
print(repo['full_name'], repo['forks'])
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'])
get_top_contributor("google/dagger")
import json
s = json.dumps({"test":"x","seq":[1,2,3,4]})
s
json.loads(s)
problem Find distance between two cities using google maps api
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']
distance("hyderabad", "pune")
distance("hyderabad", "delhi")
distance("hyderabad", "mumbai")
!ls
!ls /home/
!cp day1.html /tmp/
!ls -l
!ls --help
%%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()
!python fib.py
!python fib.py -h
!python fib.py 10
%%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()
!python fib.py
!python fib.py -h
!python fib.py -s 10
!python fib.py 10
import re
pattern = re.compile("^def .*$")
s = "def hello(name):"
s2 = "print('hello', name)"
pattern.match(s)
pattern.match(s2)
if pattern.match(s):