Oct 16-18, 2017 Vikrant Patil
These notes are available online at http://notes.pipal.in/2017/vmware-oct-python
© Pipal Academy LLP
%%file three.txt
one
two
three
filehandle = open("three.txt")
filehandle.read()
filehandle.close()
!python -c "import this" > data.txt
filehandle = open("data.txt")
filehandle.readline()
filehandle.readline()
filehandle.readline()
filehandle.readlines()
filehandle.readline()
filehandle.close()
for line in open("data.txt").readlines():
print(len(line.strip().split()))# number of words per line
problem:
Write a program cat.py equivalent to unix command cat. It prints contents of file to standard output
python cat.py three.txt
one
two
three
Write a program head.py equivalent to unix command head. it should take first commandline argument as number of lines and second argument as filename. It should print first n lines of file on screen
python head.py 5 data.txt
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
python wc.py data.txt
20 144 856
%%file cat.py
"""
module cat is rough implementation of unix command cat
"""
import sys
def cat(file):
"""
prints file to standard output
"""
f = open(file)
print(f.read())
f.close()
if __name__ == "__main__":
cat(sys.argv[1])
!python cat.py three.txt
%%file head.py
"""
module head implements unix head command
"""
import sys
def head(filename, n):
f = open(filename)
for i in range(n):
print(f.readline(), end="")
f.close()
if __name__ == "__main__":
linecount = int(sys.argv[1])
filename = sys.argv[2]
head(filename, linecount)
!python head.py 5 data.txt
%%file wc.py
"""
module wc implements unix command wc
"""
import sys
def line_count(f):
return len(open(f).readlines())
def test_line_count():
assert line_count("three.txt") == 3
def word_count(f):
return len(open(f).read().split())
def test_word_count():
assert word_count("three.txt") == 3
def char_count(f):
return len(open(f).read())
def test_char_count():
assert char_count("three.txt") == 13
if __name__ == "__main__":
f = sys.argv[1]
print(line_count(f), word_count(f), char_count(f))
!python wc.py data.txt
problem:
!py.test -v wc.py
import os
files = [f for f in os.listdir(os.getcwd()) if os.path.isfile(f)]
max([1,2,5,6,8])
max(["one", "two", "three", "four"])
max(["one", "two", "three", "four"], key=len)
from wc import line_count, word_count, char_count
max(files, key=line_count)
max(files, key=word_count)
max(files, key=char_count)
For writing you need to open file with write mode
f = open("primes.txt", "w")
f.write("two\n")
f.write("three\n")
f.write("five\n")
f.write("seven")
f.close()
!python cat.py primes.txt
f = open("primes.txt", "a")
f.write("eleven\n")
f.write("thirteen")
f.close()
!python cat.py primes.txt
similarly we can read or write files in differnt modes
open("three.txt", "r").read() # read in text mode
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"binary")
f.close()
open("binarydata.bin", "rb").read()
with open("primes.txt", "a") as f:
f.write("seventeen\n")
!python cat.py primes.txt
with open("regional.txt", "w", encoding="utf-8") as f:
f.write("\u0c05\u0c06")
open("regional.txt", encoding="utf-8").read()
problem:
write_cvs to write multiplication tables into a csv file. write_csv should take filename and two dimentional list as argument and write data from list to a given file in csv format
1,2,3,4.....11
2,4,6,8.....22
.
.
.
def tables(n):
return [[i*j for i in range(1, n+1)] for j in range(1, 11)]
tables(10)
w = ["one", "two", "three", "four"]
",".join(w)
t = tables(11)
t[0]
",".join([str(i) for i in t[0]])
def write_csv(filename, data):
with open(filename, "w") as f:
for row in data:
line = ",".join([str(i) for i in row])
f.write(line + "\n")
write_csv("tables.csv", tables(11))
!python cat.py tables.csv
[line.strip().split(",") for line in open("tables.csv")]
import sys
sys.stdout.write("Hello pyhton")
sys.stderr.write("Error: some ..exception..!")
author = {"name":"lewis carrol",
"books":["Alice in wonderland", "Looking through the glass"],
"language":"English"
}
author['name']
author['books']
print(author)
del author['books']
print(author)
"name" in author
"lewis" in author
author['language']
author.get("language")
"books" in author
author['books']
author.get("books", [])
d = {"one":1, "two":2, "three":3}
Iterating over keys
for key in d:
print(key)
[k for k in d]
for key in d.keys():
print(key, d[key])
for k in d:
print(k)
Iterating over values directly
for value in d.values():
print(value)
def f(name):
print(name)
f(name="python")
Iterating with keys and values
for k, v in d.items():
print(k, v)
print(d.items())
list(d.items())
numbers = [("one",1),("two", 2),("three", 3)]
dict(numbers)
names = ['a','b','c']
values = [1,2,3]
dict(zip(names, values))
items = ("Pen", "Pencil", "Colorbox")
prices = (25, 10, 50)
cart = dict(zip(items, prices))
cart
for item, price in cart.items():
print(item.rjust(8), price)
print("-"*12)
print("Total".rjust(8), sum(cart.values()))
problem:
def unzip(d):
keys = d.keys()
values = [d[k] for k in keys]
return list(keys), values
unzip(d)
unzip(cart)
Write a program to count word frequency in a file
%%file words.txt
five
five four
five four three
five four three two
five four three two one
six seven eight nine
six seven eight
six seven
six
"hello hello python".count("hello")
s = "hello hello python"
s.count("hello")
words = s.split()
words
for w in words:
print(w, s.count(w))
%%file wordfeq.py
"""
computes word frequency of every word in a file
"""
import sys
def read_words(filename):
return open(filename).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__":
filename = sys.argv[1]
words = read_words(filename)
freq = wordfreq(words)
print(freq)
!python wordfeq.py words.txt
%%file wordfreq.py
"""
computes word frequency of every word in a file
"""
import sys
def read_words(filename):
return open(filename).read().split()
def wordfreq(words):
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
return freq
if __name__ == "__main__":
filename = sys.argv[1]
words = read_words(filename)
freq = wordfreq(words)
print(freq)
!python wordfreq.py words.txt
def wordfreq1(words):
freq = {}
uniquewords = set(words)
for w in uniquewords:
freq[w] = words.count(w)
return freq
import wordfreq
words = wordfreq.read_words("words.txt")
freq = wordfreq1(words)
print(freq)
for w, f in freq.items():
print(w.rjust(5), f)
for w, f in sorted(freq.items()):
print(w.rjust(5), f)
for w, f in sorted(freq.items(), key=lambda x: x[1]):
print(w.rjust(5), f)
for w, f in sorted(freq.items(), key=lambda x: x[1], reverse=True):
print(w.rjust(5), f)
for w, f in sorted(freq.items(), key=lambda x: x[1], reverse=True):
print(w.rjust(5), f, "*"*f)
Grouping all keys with given values
team = {"david":"USA", "anand":"india","linus":"USA", "naufal":"india", "alice":"uk"}
[name for name in team.keys() if team[name]=="india"]
[name for name in team.keys() if team[name]=="USA"]
x = [1,2,3,4]
y = x
y.append(5)
print(x)
x = [1,2,3]
y = [3,4,5]
x, y = y, x
def f(x):
x = x +1
v = 5
f(v)
print(v)
def appendone(l):
l.append(1)
x = [1,2,3]
appendone(x)
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, real, imaginary):
self._real = real
self._imag = imaginary
def get_real(self):
return self._real
def get_imaginary(self):
return self._imag
p = Complex(3, 4)
p
type(p)
isinstance(p, Complex)
isinstance([], Complex)
p.get_real()
p.get_imaginary()
class EmptyClass:
pass
e = EmptyClass()
type(e)
isinstance(e, EmptyClass)
class Complex:
def __init__(self, real, imaginary):
self._real = real
self._imag = imaginary
def get_real(self):
return self._real
def get_imaginary(self):
return self._imag
def display(self):
print(self._real,"+", str(self._imag) + "j")
def add(self, c):
r = self._real + c.get_real()
i = self._imag + c.get_imaginary()
return Complex(r, i)
p = Complex(4,5)
p.display()
p2 = Complex(3,7)
p3 = p.add(p2)
p3.display()
%%file bank0.py
balance = 0
def deposit(amount):
global balance
balance += amount
def withdraw(amount):
global balance
balance -= amount
def get_balance():
return balance
if __name__ == "__main__":
deposit(100)
withdraw(40)
print(get_balance())
!python bank0.py
%%file bank1.py
def make_account():
return {"balance":0}
def deposit(account, amount):
account['balance'] += amount
def withdraw(account, amount):
account['balance'] -= amount
def get_balance(account):
return account['balance']
if __name__ == "__main__":
a1 = make_account()
a2 = make_account()
deposit(a1, 500)
deposit(a2, 300)
withdraw(a1, 100)
print("a1", get_balance(a1))
print("a2", get_balance(a2))
!python bank1.py
print(p)
p.__dict__
dir(p)
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
p = Point(3,4)
p.x
p.y
p.z
p.z = 5
p.z
class ColoredPoint(Point):
color = (0,0,0)
def get_color(self):
return self.color
cp = ColoredPoint(10, 5)
print(cp.x)
print(cp.y)
print(cp.get_color())
problem:
t = Timer()
t.start()
do_some_stuff()
t.stop()
print("Time taken by task:", t.get_time_taken())
dfdfdsf
int("hello")
"2" * "3"
b = "hello"
#b = "2"
c = "3"
try:
a = int(b)
#a = b * c
except TypeError as e:
a = 1
print("Handled TypeError", e)
except ValueError as e:
b = 0
print("Handled ValueError", e)
b
d = {"a":1, "b":2}
d['c']
import sys
def get_value(data, key, default):
try:
return data[key]
except KeyError as e:
print("Value not found, returning default", e, file=sys.stderr)
return default
get_value(d, "c", 5)
def read_with_missing(filename):
with open(filename) as f:
return [int(s.strip()) for s in f.readlines()]
read_with_missing("missing.txt")
import sys
def parseint(strnum):
try:
return int(strnum)
except ValueError as e:
print("Invalid integer", strnum, e, file=sys.stderr)
return 0
def read_with_missing(filename):
with open(filename) as f:
return [parseint(s.strip()) for s 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])
Third party library requests is very handy for downloading stuff from web
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 = {"param1":"hello", "param2":"python"})
print(response.text)
response = requests.post("http://httpbin.org/post", data={"name":"python", "email":"abc@remail.com"})
print(response.text)
import requests
url = "https://api.github.com/orgs/vmware/repos"
repos = requests.get(url).json()
type(repos)
for rep in repos:
print(rep['full_name'], rep['forks'])
repos = sorted(repos, key=lambda r:r['forks'], reverse=True)[:5]
for r in repos:
print(r['full_name'], r['forks'])
problem: Find distance between two cities using google api
origins
destinations
units (metric)
import requests
def distance(origin, destination):
url = "https://maps.googleapis.com/maps/api/distancematrix/json"
response = requests.get(url, params={"units":"metric",
"origins":origin,
"destinations":destination
})
return response.json()['rows'][0]['elements'][0]['distance']['text']
distance("pune", "bangalore")
Consider following unix command
!ls
!ls /home/vikrant/
!cp data.txt /tmp/
These are called positional arguments. They can be optional or compulsory.
Lets make our own small command fib.py which should work like as given below
Usage: python fib.py n
computes nth fibonacci number
%%file fib.py
import argparse
def fib(n):
prev = 1
current = 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 fiboinacci 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
Lets extend out command to print sequence of fibinacci numbers till nth fibonacci. Lets add one optional argument to out command -s, if this is given our command should print sequence and not just a number.
%%file fib.py
import argparse
def fib(n):
prev = 1
current = 1
for i in range(2,n):
current, prev = prev+current, current
return current
def printfiblist(n):
prev, current = 1, 1
print(prev, current, end=" ")
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 fiboinacci number",
type=int)
p.add_argument("-s", "--sequence",
help="Print sequence of fibonaci",
action="store_true")
return p.parse_args()
def main():
args = parse_args()
print(args)
if args.sequence:
printfiblist(args.n)
else:
print(fib(args.n))
if __name__ == "__main__":
main()
!python fib.py -h
!python fib.py --sequence 10
!python fib.py -s 5
!python fib.py 5
google for