Jan 17-19, 2018 Vikrant Patil
These notes are available online at http://notes.pipal.in/2018/vmware-jan-python
© Pipal Academy LLP
def column(data, c):
rowcount = len(data)
return [data[i][c] for i in range(rowcount)]
def reverse(items):
return list(reversed(items))
def rotate90clockewise(data):
colcount = len(data[0])
return [reverse(column(data, i)) for i in range(colcount)]
def transpose(data):
colcount = len(data[0])
return [column(data, i) for i in range(colcount)]
def rotate90anticlockwise(data):
return reverse(transpose(data))
data = [["A1","B1","C1"],
["A2","B2","C2"],
["A3","B3","C3"]]
transpose(data)
rotate90clockewise(data)
rotate90anticlockwise(data)
import this
%%file data.txt
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!
filehandle = open("data.txt")
filehandle.read() #this will read complete file as a string
filehandle.read()
filehandle.close()
filehandle = open("data.txt")
lines = filehandle.readlines()
for line in lines:
print(line, end="")
for index,line in enumerate(lines):
print(index+1, line, end="")
filehandle = open("data.txt")
filehandle.readline()
filehandle.readline()
filehandle.readline()
count = 1
line = filehandle.readline()
while line!="":
print(count, line, end="")
line = filehandle.readline()
count += 1
problems
%%file cat.py
"""
cat module implements unix command cat, approximately
"""
import sys
def cat(file):
"""
print the file to standard output
"""
for line in open(file):
print(line, end="")
def catfiles(files):
"""
print multiple files to standard output
"""
for file in files:
cat(file)
if __name__ == "__main__":
catfiles(sys.argv[1:])
!python cat.py /home/vikrant/programming/explorations/python/argv.py data.txt
%%file head.py
"""
head module implements unix command head approximately
"""
import sys
def head(file, n):
filehandle = open(file)
line = filehandle.readline()
count = 1
while line and count<=n:
print(line, end="")
count += 1
line = filehandle.readline()
if __name__ == "__main__":
head(sys.argv[1], int(sys.argv[2]))
!python head.py data.txt 5
%%file wc.py
"""
wc module implements unix command wc approximately
"""
import sys
def word_count(file):
words = open(file).read().split()
return len(words)
def char_count(file):
return len(open(file).read())
def line_count(file):
return len(open(file).readlines())
if __name__ == "__main__":
file = sys.argv[1]
print(line_count(file), word_count(file), char_count(file), file)
!python wc.py data.txt
What about finding file with largest number of line count in current directory?
file with largest number of words?
import os
files = [file for file in os.listdir(os.getcwd()) if os.path.isfile(file)]
files
import wc
count = 0
f = files[0]
for file in files:
lcount = wc.line_count(file)
if lcount > count:
count = lcount
f = file
print(f, count)
max(files, key=wc.line_count)
max(files, key=wc.word_count)
file = open("numbers.txt", "w")
file.write("one\n")
file.write("two\n")
file.write("three\n")
file.close()
!python cat.py numbers.txt
file = open("numbers.txt", "r+") #read and write at a time, but file has to pre-exist.
file.write("1\n")
file.write("2\n")
file.flush()
file.readline()
file.readline()
file.readline()
file.close()
!python cat.py numbers.txt
file = open("numbers.txt","a")
file.write("end\n")
file.close()
!python cat.py numbers.txt
file = open("binary.bin", "wb")
file.write(b"\x65\x69")
file.close()
file = open("binary.bin", "rb")
file.read()
b = b"binary"
type(b)
b.decode()
"string".encode()
file = open("regional.txt", "w", encoding="utf-8")
file.write("हाआ")
file.close()
file = open("regional.txt", encoding="utf-8")
file.read()
tables = [[str(i*j) for i in range(1,6)] for j in range(1,11) ]
tables
def csvwriter(data, filename):
file = open(filename, "w")
for row in data:
file.write(",".join(row))
file.write("\n")
file.close()
def columndatawriter(data, delimiter, filename):
with open(filename, "w") as f:
for row in data:
f.write(delimiter.join(row))
f.write("\n")
csvwriter = lambda data, file: columndatawriter(data, ",", file)
def csvwriter(data, file):
return columndatawriter(data, ",", file)
tsvwriter = lambda data, file: columndatawriter(data, "\t", file)
csvwriter(tables, "tables.csv")
!python cat.py tables.csv
csvwriter(tables, "tables.csv")
!python cat.py tables.csv
tsvwriter(tables, "tables.tsv")
!python cat.py tables.tsv
import sys
sys.stderr.write("Error: Something went wrong")
sys.stdout.write("This is just for information..")
author = {"name":"Lewis Carrol",
"books":["Alice in wonderland", "Looking through the glass"],
"language":"english"}
author
author["name"]
author["books"]
author["country"] = "UK"
author
del author['country']
author
d = {True:1, False:0}
d
[[d[i==j] for i in range(5)] for j in range(5)]
author['country']
author.get("country", "UK")
author
del author['books']
author
author.get("books", [])
"name" in author
"books" in author
"language" in author
"english" in author
author
"english" in author.values()
author.values()
author.keys()
author.items()
my grub conf looks like this ...
%%file grub.cnf
# /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 modlbs=off wifi=off"
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"
def parsegrubconf(conffile):
grubconf = {}
with open(conffile) as conf:
for line in conf:
if line.startswith("#") or line.strip()=="":
continue
else:
tokens = line.strip().split("=")
grubconf[tokens[0]] = "=".join(tokens[1:])
return grubconf
conf = parsegrubconf("grub.cnf")
conf['GRUB_CMDLINE_LINUX']
conf['GRUB_TIMEOUT']
conf
s = "hello world few more words"
help(s.split)
s.split(maxsplit=1)
%%file words.txt
one
one two
one two three
one two three four
one two three four five
one two three five six
one two five six seven
one five six
five six
five
%%file wordfreq.py
import sys
def getwords(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
def wordfreq1(words):
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
return freq
def wordfreq2(words):
uniq = set(words)
freq = {}
for w in uniq:
freq[w] = words.count(w)
return freq
if __name__ == "__main__":
words = getwords(sys.argv[1])
print(wordfreq(words))
!python wordfreq.py words.txt
set([1,1,1,2,3,2,3,4]) # set is unique collection of objects
import wordfreq
words = wordfreq.getwords("words.txt")
wordfreq.wordfreq(words)
wordfreq.wordfreq1(words)
wordfreq.wordfreq2(words)
freq = wordfreq.wordfreq(words)
for key in freq:
print(key, freq[key])
for key in freq.keys():
print(key, freq[key])
for value in freq.values():
print(value)
for key, value in freq.items():
print(key, value)
for key, value in sorted(freq.items()):
print(key, value)
def getvalue(pair):
return pair[1]
for key, value in sorted(freq.items(), key=getvalue):
print(key, value)
for key, value in sorted(freq.items(), key=getvalue, reverse=True):
print(key, value)
for key, value in sorted(freq.items(), key=getvalue, reverse=True):
print(key.rjust(5), value)
for key, value in sorted(freq.items(), key=getvalue, reverse=True):
print(key.rjust(5), value, "*"*value)
names = ["Anand","Naufal", "Vikrant", "David", "Alice", "Isac"]
countires = ["India", "India", "India", "USA", "UK", "USA"]
dict(zip(names, countires))
d = {[]:"empty",
[1,1,1]:"ones"}
d = {(1,):1,
(1,2):2}
d
Lets try to model bank account
%%file bank0.py
balance = 0
def get_balance():
return balance
def deposite(amount):
global balance
balance += amount
def withdraw(amount):
global balance
balance -= amount
if __name__ == "__main__":
deposite(100)
print(get_balance())
withdraw(20)
print(get_balance())
%%file m.py
a = 2
b = 3
def func():
return a+b
import m
m.a
m.b
m.func()
%%file bank1.py
def create_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__":
a1 = create_account()
a2 = create_account()
deposite(a1, 100)
deposite(a2, 200)
print("a1 ", get_balance(a1))
print("a2 ", get_balance(a2))
withdraw(a1, 10)
withdraw(a2, 20)
print("a1 ", get_balance(a1))
print("a2 ", get_balance(a2))
!python bank1.py
%%file bank2.py
class BankAccount:
def __init__(self):
self.balance = 0
def get_balance(self):
return self.balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
if __name__ == "__main__":
a1 = BankAccount()
a2 = BankAccount()
a1.deposit(100)
a2.deposit(200)
print("a1 ", a1.get_balance())
print("a2 ", a2.get_balance())
a1.withdraw(20)
a2.withdraw(30)
print("a1 ", a1.get_balance())
print("a2 ", a2.get_balance())
!python bank2.py
class Foo:
pass
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class Point:
def __init__(obj, x, y): # this will also work, but by convention we use self
obj.x = x
obj.y = y
p = Point(1,2)
type(p)
def f():
pass
f
class Foo:
pass
Foo
p.x
p.y
p.z = 3
p.z
p.x = -1
p.x
p.__dict__
problem
t = Timer()
t.start()
do some work
t.stop()
print(t.get_time_taken())
import time
time.time()
import time
class Timer:
def __init__(self):
self._start = 0
self._end = 0
def start(self):
self._start = time.time()
def stop(self):
self._end = time.time()
def get_time_taken(self):
return self._end - self._start
def reset(self):
self._start = 0
self._end = 0
t = Timer()
t.start()
time.sleep(2)
t.stop()
print(t.get_time_taken())
t.start()
t.get_time_taken()
t.reset()
names = ["Anand","Naufal", "Vikrant", "David", "Alice", "Isac"]
countires = ["India", "India", "India", "USA", "UK", "USA"]
dict([(1,"one"), (2,"two")])
teams = dict(zip(names, countires))
teams
teams['Asimov']
doom
2 + "3"
open("sdkjhfkdsjhfkjds")
def parseint(strvalue):
return int(strvalue)
parseint("42")
parseint("Nan")
def parseint(strvalue):
try:
return int(strvalue)
except ValueError as e:
print("Handled :", e)
return 0
parseint("Nan")
def csvparser(file):
return [[int(item) for item in line.strip().split(",")] for line in open(file)]
def csvparser_missing(file):
return [[parseint(item) for item in line.strip().split(",")] for line in open(file)]
%%file nums.csv
1,2,3
4,,5
6,7,Nan
csvparser("nums.csv")
csvparser_missing("nums.csv")
%%file head.py
"""
head module implements unix command head approximately
"""
import argparse
def head(file, n):
filehandle = open(file)
line = filehandle.readline()
count = 1
while line and count<=n:
print(line, end="")
count += 1
line = filehandle.readline()
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("file", type=str,
help="File in which to look for")
parser.add_argument("-n", "--lines",
type=int,
help="Number of lines to be seen as head")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
if args.lines:
head(args.file, args.lines)
else:
head(args.file, 5)
!python head.py data.txt
%%file args.py
"""
head module implements unix command head approximately
"""
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("files",type=str,
nargs="+",
help="Files in which to look for")
parser.add_argument("-n", "--lines",
type=int,
help="Number of lines to be seen as head")
parser.add_argument("-f",
help="optional flag",
action="store_true")
return parser.parse_args()
if __name__ == "__main__":
args = parse_args()
print(args)
!python args.py -n 5 data.txt jkjsdhdf jksddj
problem
import re
pattern = re.compile("^def\s+[a-zA-Z]+\(.*\):")
m = pattern.match("def hello():")
m
pattern = re.compile("^def\s+([a-zA-Z]+)\(.*\):")
m = pattern.match("def hello():")
m
m.groups()
def functions(pyfiles):
funcp = re.compile("^def\s+([a-zA-Z]+)\(.*\):")
funcs = []
for file in pyfiles:
for line in open(file):
m = funcp.match(line)
if m:
funcs.append(m.groups()[0])
return funcs
import os
pyfiles = [file for file in os.listdir(os.getcwd()) if file.endswith(".py")]
functions(pyfiles)
text = "some junk lkdljsfl 1hell3 kjfklfgj-090943unlkjrkltgj90485"
text2 = "kjsdhhfkjds kjhdsfkhsdkjh 987jhkfdskjh 1ghfh9 lkdfjlkds90m.?;ljfsdh"
p = re.compile("(.*)([0-9]{1,1}[a-z]{4,4}[0-9]{1,1})(.*)")
m1 = p.match(text)
m2 = p.match(text2)
m1
m2
m1.groups()
m2.groups()
import requests
response = requests.get("http://httpbin.org/html")
print(response.text[:400])
response.headers
response = requests.get("http://httpbin.org/get", params={"parameters":"dummy", "language":"python"})
print(response.text)
response = requests.post("http://httpbin.org/post", data={"name":"alice","email":"asa@kj.com"})
print(response.text[:100])
import requests
url = "https://api.github.com/orgs/vmware/repos"
repos = requests.get(url).json()
type(repos)
for repo in repos:
print(repo['full_name'], repo['forks'])
for repo in sorted(repos, key=lambda rep:rep["forks"], reverse=True)[:5]:
print(repo['full_name'], repo['forks'])
problem
url="https://maps.googleapis.com/maps/api/distancematrix/json"
paramaeter reqauired :
origins
destinations
units (metric)
def distance(source, dest):
url = "https://maps.googleapis.com/maps/api/distancematrix/json"
r = requests.get(url, params={"origins":source,
"destinations":dest,
"units":"metric"})
#return r.json()
return r.json()['rows'][0]['elements'][0]['distance']['text']
data = distance("bangalore", "mumbai")
data
data['rows'][0]['elements'][0]['distance']['text']
distance("chennai", "bangalore")
google for bangpypers
Suggested mini projects.