May 28-30, 2018 Vikrant Patil
These notes are available online at http://notes.pipal.in/2018/vmware-advanced-may
© Pipal Academy LLP
We will be using jupyter which comes with anaconda (python 3) distribution for this training. it can be downloaded from
2 + 3
strigs , lists, dictionary, tuple
name = "first" "second"
name
"first" in name
if "first" in name:
print("yes it is!")
"first" + "second"
a = "first"
b = "second"
c = a b
book = "Alice in wonderland"
book.split(" ")
book.split()
"Alice in \t wonderland".split()
s = "BOOT_LOADER=/boot/...."
s.split("=")
s = "options= 'wifi=off blootoon=on'"
s.split("=")
s.split(maxsplit=1)
s.split("=", maxsplit=1)
import os
os.path.sep
path = ["","home","vikrant","training","2018","vmware-advanced-may"]
curretdir = os.path.sep.join(path)
curretdir
" hello python \n".strip()
lists
l = [1,"a",[1,2,3],4]
l[0]
len(l)
l[-1]
numbers = list(range(0,10,1))
numbers
numbers[1:4] # start at index 1, stop at index 4 (excluding)
numbers[1:7:2] # start at index 1, stop at index 7 (excluding) at interval of 2
numbers[:3]
numbers[2:]
numbers[::-1]
def is_palindrome(w):
return w == w[::-1]
is_palindrome("madam")
for n in numbers:
print(n*n, end=",")
squares = [n*n for n in numbers]
squares
cubes = [n**3 for n in numbers]
cubes
[[i*j for j in range(1,11)] for i in range(1,6)]
t = [[i*j for j in range(1,11)] for i in range(1,6)]
t[0]
t[1]
t[0] # 0th row
t[0][0]
len(t)
def column(data, n):
return [data[r][n] for r in range(len(data))]
column(t, 0)
column(t, 2)
def transpose(data):
numcols = len(data[0])
return [column(data, i) for i in range(numcols)]
transpose(t)
dictionary
person = {"name":"Alice", "age":9, "email":"alice@wonder.land"}
person["name"]
person["age"]
person["hello"]
if "name" in person:
print(person["name"])
person.get("country", "India")
person
for key in person:
print(key)
for key, value in person.items():
print(key, value)
d = {[1,2,3]:"list"}
d = {"list":[1,2,32,4]}
d
d = {(1,2,3):"tuple"}
You can put only hashable items as a key in dictionary
files
with open("numbers.txt","w") as f:
f.write("one\n")
f.write("two\n")
f.write("three\n")
for line in open("numbers.txt"):
print(line, end="")
problem
t
with open("tables.csv", "w") as f:
for row in t:
line = ",".join([str(n) for n in row])
f.write(line)
f.write("\n")
!cat tables.csv
def cat(file):
for line in open(file):
print(line, end="")
cat("tables.csv")
def csvpaser1(file):
return [line for line in open(file)]
csvpaser1("tables.csv")
def csvpaser2(file):
return [line.strip().split(",") for line in open(file)]
csvpaser2("tables.csv")
def csvpaser(file):
return [[int(i) for i in line.strip().split(",")] for line in open(file)]
csvpaser("tables.csv")
def csvpaser(file):
with open(file) as f:
d = [[int(i) for i in line.strip().split(",")] for line in f]
return d
csvpaser("tables.csv")
def twice(x) :
return 2*x
def twice1(x):
print(2*x)
twice(3)
twice1(3)
x = twice(4)
y = twice1(4)
x
y
twice(twice(2))
twice1(twice1(3))
Positional arguments
import math
def cyl_volume(radius, height):
return math.pi*radius**2*height
cyl_volume(1.0, 2.0)
cyl_volume(2.0, 1.0)
cyl_volume(radius=1.0, height=2.0)
cyl_volume(1.0, height=2.0)
cyl_volume(radius=1.0, 2.0)
cyl_volume(height=2.0, radius=1.0)
def cyl_volume(radius=1.0, height=10):
return math.pi*radius**2*height
cyl_volume()
cyl_volume(height=2)
cyl_volume(2)
def append(value, data=[]):
data.append(value)
return data
l = ["A"]
l = append("B", l)
l
l2= append("A")
l2
l2= append("A")
l2
def append(value, data=None):
if not data:
data = []
data.append(value)
return data
t = (1,2,3,4)
t[0] = 3
t = ([],2,3,4,5)
t[0]
t[0].append(1)
t
t[0] = [1,3]
type(t[0])
(1,2,3,4,5)
Named only argument
def cyl_volume(radius=1.0, *, height=10):
return math.pi*radius**2*height
cyl_volume(2.0)
cyl_volume(2.0, 20)
def cyl_volume(*, radius=1.0, height=10):
return math.pi*radius**2*height
cyl_volume(1.0)
cyl_volume(radius=1.0)
cyl_volume(radius=1.0, height=10)
Variable number of arguments
def mysum(*args):
print(args)
mysum(1,2,3,4,5)
mysum(1,1,1)
def mysum(*args):
s = 0
for n in args:
s += n
return s
mysum(1,2,3,4)
problem
myjoin("_", "we","are","going", "functional","way")
'we_are_going_functional_way'
items = (1,2,3,"3","A")
[str(item) for item in items]
def myjoin(joiner, *args):
s = ""
for item in args[:-1]:
s += str(item) + joiner
return s + args[-1]
myjoin("/","","home","vikrant","training")
mysum(1,2,3,4)
def mean(*args):
s = mysum(*args) # this is called as unpacking of arguments
return s/len(args)
mean(1,2,3,4,5)
def foo(*args):
print(len(args))
print(args)
def bar(*args):
foo(args)
def bar1(*args):
foo(*args)
bar(1,2,3,4,5,6)
bar1(1,2,3,4,5,6)
def make_person(**kwargs):
d = {}
for key, value in kwargs.items():
d[key] = value
return d
make_person(name="Alenzo Church", email="alenzo@lambda.in",language="lisp")
def fun(p1, p2, *args, **kwargs):
pass
def square(x):
return x*x
a = 2
a
print(a)
type(a)
square
type(square)
x = a
print(x)
fun = square
square(2)
fun(3)
def sum_natural(start=1, end=100):
s = 0
for i in range(start, end+1):
s += i
return s
def sum_of_squares(start=1, end=100):
s = 0
for i in range(start, end+1):
s += square(i)
return s
def cube(x):
return x*x*x
def sum_of_cubes(start=1, end=100):
s = 0
for i in range(start, end+1):
s += cube(i)
return s
def sum_(func, start, end):
s = 0
for i in range(start, end+1):
s += func(i)
return s
sum_natural(1, 50)
def identity(x):
return x
sum_(identity, 1, 50)
sum_of_squares(1,50)
sum_(square, 1, 50)
sum_of_cubes(1,50)
sum_(cube, 1, 50)
max(["one", "two","three","four","five"])
words = ["one", "two","three","four","five"]
max(words, key=len)
records = [
("A", 9.0),
("B", 8.0),
("C", 5.0),
("D", 8.8),
("E", 8.7),
("F", 5.5)
]
max(records)
def get_score(r):
return r[1]
max(records, key=get_score)
sorted(records)
sorted(records, key=get_score)
sorted(records, key=get_score, reverse=True)
min(records, key=get_score)
8/3*5 + 8/7*9 + 8/11*13 ..... this converges to pi
def pi_func(n):
return 8/((4*n-1)*(4*n+1))
sum_(pi_func, 1, 10000)*4
def make_adder(x):
def adder(y):
return x+y
return adder
aader5 = make_adder(5)
aader5
aader5(4)
aader5(11)
adder4 = make_adder(4)
adder4(3)
adder4(7)
f = lambda x:x*x
f(3)
m = lambda x,y: x*y
m(3,4)
sum_(lambda x:x*x, 1, 50)
records = [
("A", 9.0, 90),
("B", 8.0, 110),
("C", 5.0, 115),
("D", 8.8, 120),
("E", 8.7, 95),
("F", 5.5, 107)
]
def column(i):
return lambda r:r[i]
max(records, key=column(1))
max(records, key=column(2))
problem
repeat which takes a single argument function as first parameter and an integer, n as second parameter. it returns a new function which repeats original function n times.
s3 = repeat(square, 3)
s3(2) => square(square(square(2)))
make_logger which can create loggers of various level..for example warning, info, error...warn = make_logger("[WARNING]:")
warn("Something might be wrong")
[WARNING]: Somthing might be wrong
info = make_logger("[INFO]:")
info("Just for information...process started all well")
[INFO]: Just for information...process started all well
Bonus problem
compose which takes two functions f anf g as parameters and returns a function which is composite of f and g as given belowfg = compose(f, g)
fg(x) => f(g(x))
square(square(square(square(square(11)))))
s = square(11)
for i in range(4):
s = square(s)
s
bonus problem
zip_with which zips two lists with a function that you specify
zip([1,2,3][a,b,c]) => [(1,a), (2,b), (3,c)]
zip_with(zipper, [1,2,3,4], [1,2,3,4]) => it should combine two lists using zipper function
fixedpoint to find fixed point with given accuracy.
fixedpoint(cos, guess=1.0, accuracy = 0.00001)
def make_logger(logger_type):
def logger(msg):
print("[{0}]:{1}".format(logger_type, msg))
return logger
info = make_logger("INFO")
info("process started normally...")
warn = make_logger("WARN")
warn("something went wrong!")
def zip_with(zipper):
def zipper_(seq1, seq2):
return [zipper(i,j) for i,j in zip(seq1, seq2)]
return zipper_
vector_adder = zip_with(lambda x,y:x+y)
vector_adder([1,2,3,4],[1,1,1,1])
def compose(f, g):
def fg(x):
return f(g(x))
return fg
ssin = compose(square, math.sin)
ssin(math.pi/4)
def add(x,y):
print("calling func add ", x, y)
v = x +y
print("Returning add ", v)
return v
def square(x):
return x*x
square
def modified_square(x):
print("calling square", x)
v = square(x)
print("returning square", v)
return v
def debugger(func):
def modified_func(x):
print("calling ",func.__name__, x)
v = func(x)
print("returning ",func.__name__, v)
return v
return modified_func
square_debug = debugger(square)
square_debug(3)
square = debugger(square)
square(4)
import math
from math import sin
sin(math.pi/4)
sin = debugger(sin)
sin(math.pi/4)
@debugger
def double(x): ## same as double = debugger(double)
return 2*x
double(3)
def sub(x,y):
"""
some help for sub
"""
return x - y
help(sub)
help(square)
def foo():
pass
foo.__name__
foo.__qualname__
def decorator(func):
def wrapper(*args):
print("preproccing")
r = func(*args)
print("post processing")
return r
wrapper
problem
deprecated which prints warning message that given function is deprecateddef deprecated(func):
def wrapper(*args):
print("Function {} is deprecated!".format(func.__qualname__))
return func(*args)
return wrapper
def fib(n):
if n in [0,1]:
return 1
else:
return fib(n-1) + fib(n-2)
%%file fib.py
import sys
def fib(n):
if n in [0,1]:
return 1
else:
return fib(n-1) + fib(n-2)
if __name__ == "__main__":
print(fib(int(sys.argv[1])))
!python fib.py 7
%%file trace.py
import os
level = 0
def trace(func):
def log(*args):
if os.getenv("DEBUG")=="TRUE":
print(*args)
def wrapper(*args):
global level
level += 1
log("| "*level + "|--" + func.__name__ , args)
r = func(*args)
log("| "*level + "|-- return " , r)
level -= 1
return r
return wrapper
%%file fib.py
import sys
import trace
@trace.trace
def fib(n):
if n in [0,1]:
return 1
else:
return fib(n-1) + fib(n-2)
if __name__ == "__main__":
print(fib(int(sys.argv[1])))
import trace
import imp
imp.reload(trace)
@trace.trace
def square(x):
return x*x
@trace.trace
def sum_of_squares(x,y):
return square(x) + square(y)
sum_of_squares(2,3)
!time -p python fib.py 30
%%file memoize.py
cache = {}
def memoize(func):
def wrapper(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return wrapper
import memoize
import imp
imp.reload(memoize)
@memoize.memoize
def square(x):
print("computing square({})".format(x))
return x*x
square(2)
square(2)
%%file fib1.py
import sys
import trace
import memoize
@memoize.memoize
@trace.trace
def fib(n):
if n in [0,1]:
return 1
else:
return fib(n-1) + fib(n-2)
if __name__ == "__main__":
print(fib(int(sys.argv[1])))
!time -p python fib1.py 30
!DEBUG=TRUE python fib1.py 10
!DEBUG=TRUE python fib.py 10