Apr 02-04, 2018 Vikrant Patil
These notes are available online at http://notes.pipal.in/2018/vmware-advanced-apr
© Pipal Academy LLP
We will be using anaconda (python 3) distribution for this training. it can be downloaded from
Markdown cell
### Header ###
## Header ##
lists
l = []
numbers = [1,2,3,4]
digits = list(range(0,9,1)) # range(start, end, step)
digits
list(range(0,20,2))
digits[0]
digits[-1]
digits[1:] # start at 1 end at end of list... drop one
digits[:2] # start at 0th index and end at 2nd (exclude) ... take first 2
digits[2:5:1] # just like range..start:end:step
digits[:]
digits[:-1]
digits[:3]
digits[::-1]
digits.pop()
digits
digits.insert(0, 2)
digits
digits.pop(0)
strings
book = "Alice in Wonderland"
book[0]
book[:2]
book.split()
book.split(maxsplit=1)
book.upper()
book.lower()
book.center(50)
book.ljust(50)
book.rjust(50)
dictionaries
person = {"name":"Alice","email":"alice@example.com"}
person['name']
person.get("country", "UK")
person
boolean = {True:"True", False:"False"}
data = {(1,2):[1,2], (2,3):[2,3]}
data
True
boolean
List comprehensions
numbers = range(10)
[n*n for n in numbers]
[n*n for n in numbers if n%2==0]
t = [[i*j for i in range(1,11)] for j in range(1,6)]
t
[str(item) for item in t[0]]
Files
f = open("data.csv","w")
for row in t:
f.write(",".join([str(i) for i in row]))
f.write("\n")
f.close()
!cat data.csv
def fun(filename):
pass
%%file cat.py
import sys
if __name__ == "__main__":
print(open(sys.argv[1]).read())
!python cat.py data.csv
def csvparse(filename):
t = []
with open(filename) as f:
for line in f:
tokens = line.strip().split(",")
items = [int(t) for t in tokens]
t.append(items)
return t
csvparse("data.csv")
def csvparse_(filename):
return [[int(i) for i in line.strip().split(",")] for line in open(filename)]
csvparse_("data.csv")
problem
# If you change this file, run 'update-grub' afterwards to update
# /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"
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"
%%file grub.conf
# If you change this file, run 'update-grub' afterwards to update
# /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"
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"
list(range(5))
tuple([2,3])
dict([(1,2),(3,4)])
def grubconfparser(filename):
data = {}
for line in open(filename):
if not line.startswith("#") and line.strip():
k,v = line.strip().split("=", maxsplit=1)
data[k] = v
return data
grubconfparser("grub.conf")
def grubconfparser_(filename):
return dict([line.strip().split("=", maxsplit=1) for line in open(filename)
if not line.startswith("#") and line.strip()])
grubconfparser_("grub.conf")
zip(["one","two","three"],[1,2,3])
for item in zip(["one","two","three"],[1,2,3]):
print(item)
dict(zip(["one","two","three"],[1,2,3]))
Positional Arguments
import math as m
def cyl_volume(radius, height):
return m.pi*radius**2*height
cyl_volume(2, 1)
cyl_volume(1,2)
cyl_volume(radius=1, height=2)
Default arguments
import math as m
def cyl_volume(radius=1.0, height=1.0):
return m.pi*radius**2*height
cyl_volume()
cyl_volume(radius=2)
cyl_volume(height=2)
cyl_volume(1.5, height=2)
cyl_volume(height=2, 1.5)
Only Named arguments
import os
def binlocation(base="/home/vikrant",*, package="jupyter"):
return os.path.sep.join([base, "usr","local",package])
binlocation()
binlocation(base="/home/root",package="virtulenv")
binlocation("/home/root",package="virtulenv")
binlocation("/home/root","virtulenv")
max([1,2,3,4,5])
variable number of arguments
def func(*args):
for arg in args:
print(arg)
func(1,2,3)
func(1)
func(2,3,4,5,67,5)
problem
mysum which returns sum of all the arguments passed def mysum(*args):
s = 0
for i in args:
s += i
return s
mysum(1,2,3,4,5)
"one" + "two"
mysum("one","two","three")
def mysum(*args, start=0):
s = start
for i in args:
s += i
return s
mysum("one","two","three",start="")
mysum(1,2,3,4,5)
mysum(start="", "one","tweo","ada")
variable number of named arguments
def make_person(**properties):
person = {}
print(properties)
for k,v in properties.items():
person[k] = v
return person
make_person(name="Alice",email="alice@wonder.land")
person
for item in person:
print(item)
for k,v in person.items():
print(k,v)
def mixed(*args, **kwargs):
print(args)
print(kwargs)
mixed(1,2,3,name="x",last="y")
mixed(1,name="hello")
x,y = [1,2]
x
y
def mysum(*args, start=0):
print(args)
s = start
for i in args:
s += i
return s
def make_plot_data(*data, **annotaions):
"""
takes numeric data as variable number of positional arguments
also takes annotations as variable number of named arguments
it computes sum of data...
"""
print(type(data))
ploting_data = {}
plotig_data["sum"] = mysum(data)
return ploting_data
make_plot_data(1,2,3,4,5,name="x",exp="y",abc="123")
def mysum(*args, start=0):
print(args)
s = start
for i in args:
s += i
return s
def make_plot_data(*data, **annotaions):
"""
takes numeric data as variable number of positional arguments
also takes annotations as variable number of named arguments
it computes sum of data...
"""
print(type(data))
ploting_data = {}
ploting_data["sum"] = mysum(*data)
return ploting_data
make_plot_data(1,2,3,4,5,name="x",exp="y",abc="123")
numbers
numbers = list(numbers)
numbers
mysum(numbers)
mysum(*numbers)
def add(x,y):
return x+y
add(2,3)
add(*[2,3])
mysum(*{"start":0, "a":1,"b":2})
mysum(**{"start":0})
mysum("start","a","b")
mysum(*{1:"one",2:"two",3:"three"})
mysum(**{1:"one",2:"two",3:"three"})
def fun_():
print("Fun!")
fun_
x = 2
y = x
fun_
x
type(fun_)
aliasfun = fun_
fun_()
aliasfun()
def square(x):
return x*x
def sumofsquares(start,end):
s = 0
for i in range(start, end):
s += square(i)
return s
def cube(x):
return x*x*x
def sumofcubes(start, end):
s = 0
for i in range(start, end):
s += cube(i)
return s
def sumof(f, start, end):
s = 0
for i in range(start, end):
s += f(i)
return s
sumofsquares(1,20)
sumof(square, 1, 20)
def f(x,y):
return x*y
f = lambda x,y: x*y
f(2,3)
sumof(lambda x:x*x, 1, 20)
sumof(lambda x:x**3, 1, 20)
with this small abstraction of sumation we can do lots of magic in one line. for example The series 8/1*3 + 8/5*7 + 8/9*11 + ....converges slowly to pi. we can compute pi!
sumof(lambda n:8/((4*n+1)*(4*n+3)), 0, 10000)
max([1,2,3,4,5])
nums = ["one","two","three","four","five","six"]
max(nums)
max(nums, key=len)
min(nums, key=len)
sorted(nums)
sorted(nums, key=len)
records = [("A",6.7, 100),
("B", 7.8, 110),
("C",8.0, 90),
("D",7.0, 99),
("E", 7.7, 105)
]
max(records)
max(records, key=lambda r:r[1])
max(records, key=lambda r:r[2])
def make_logger(loggertype):
def logger(msg):
print("[{type}]: {msg}".format(type=loggertype, msg=msg))
return logger
info = make_logger("INFO")
warn = make_logger("WARNING")
error = make_logger("ERROR")
info
warn
error
info("This is just for your information")
warn("Something might be wrong...")
error("Something is wrong..crash")
def column(index):
return lambda r:r[index]
max(records, key=column(0))
max(records, key=column(1))
max(records, key=column(2))
records
problems
reduce which reduces given sequence in single item based on combiner function
>>> reduce([1,2,3,4], lambda x,y:x*y, default=1)
24
zip_with which zips two different list in to single list based on combiner functions
>>> zip_with([1,2,3],[2,3,4], lambda x,y:x+y)
[3, 5, 7]
compose which takes two functions as argument and returns a function which will apply two functions in sequecnce.
>>> fg = compose(f,g)
>>> fg(2) ===> f(g(2))
def max_(seq, key=lambda x:x):
m = seq[0]
for item in seq[1:]:
if key(item) > key(m):
m = item
return m
max(person)
def reduce(seq, func, default):
r = default
for item in seq:
r = func(r, item)
return r
reduce(range(10),lambda x,y:x+y, 0) #sum
reduce(range(1,10), lambda x,y:x*y, 1) # mutliply items from list
reduce(range(100), lambda x,y: x if x>y else y, default=0) #max
def factorial(n):
return reduce(range(1,n+1), lambda x,y:x*y, 1)
factorial(5)
import functools
functools.reduce
def zip_with(combiner, seq1, seq2):
return [combiner(i, j) for i,j in zip(seq1, seq2)]
zip_with(lambda x,y:x+y, range(10), range(10))
def compose(f, g):
def fg(x):
return f(g(x))
return fg
fg = compose(square, lambda x:2*x)
fg(3)
compose(lambda x:2*x, square)(3)
def square(x):
return x**2
def square(x):
print("Begining .. square ({x})".format(x=x))
return x**2
def debug(f):
def wrapper(*args, **kwargs):
print("Beginning ", f.__name__, args, kwargs)
r = f(*args, **kwargs)
print("Returning ", r)
return r
return wrapper
def add(x,y):
return x+y
add(3,4)
add_ = debug(add)
add_(3,4)
add(3,4)
add = debug(add)
def sumofsquares(x,y):
return square(x) + square(y)
def square(x):
return x*x
square = debug(square)
sumofsquares(3,4)
def sumofcubes(x,y):
return cube(x) + cube(y)
@debug
def cube(x):
return x*x*x
sumofcubes(3,4)
problem
timeit which times the functions. make use of time.time() function to get timestamp at given time.@timeit
def saxpy(n=10000):
s = 1.0
for i in range(1,n):
s = s + i*i*1.0
return s
>>> saxpy()
time taken by saxy = 2.0 sec
@login_required(role="admin")
def add_user(name):
pass
add
add(2,3)
def with_retries(tries=5):
def decor(f):
def wrapper(*args, **kwargs):
for i in range(tries):
try:
return f(*args, **kwargs)
except Exception as e:
print("Retrying ...")
print("Giving up...")
return wrapper
return decor
import requests
@with_retries(tries=3)
def download(url):
return requests.get(url)
download("http://googl.nosuchurl/junk")
def fib(n):
if n in [1,2]:
return 1
else:
return fib(n-1) + fib(n-2)
for i in range(1,15):
print(fib(i), end=",")
fib(10)
%%file trace.py
level = 0
def trace(f):
def wrapper(*args, **kwargs):
global level
print("| " * level + "|-- " + f.__qualname__, args)
level += 1
value = f(*args)
level -= 1
print("| " * level + "|-- " + "return", value)
return value
return wrapper
import imp
import trace
imp.reload(trace)
def square(x):
return x*x
sumofsquares(3,4)
from trace import trace
@trace
def square(x):
return x*x
@trace
def sumofsquares(x,y):
return square(x) + square(y)
sumofsquares(3,4)
print(*[1,2,3,4])
print(1,2,3,4)
@trace
def fib(n):
if n in [1,2]:
return 1
else:
return fib(n-1) + fib(n-2)
fib(5)
fib(7)
def square(s):
"""
returns square of a number
"""
return s*s
help(square)
@debug
def square(s):
"""
returns square of a number
"""
return s*s
help(square)
from functools import wraps
def debug(f):
@wraps(f)
def wrapper(*args, **kwargs):
print("Beginning ", f.__name__, args, kwargs)
r = f(*args, **kwargs)
print("Returning ", r)
return r
return wrapper
@debug
def square(s):
"""
returns square of a number
"""
return s*s
help(square)
for i in range(5):
print(i)
r = iter(range(5))
next(r)
next(r)
next(r)
next(r)
next(r)
next(r)
def squares(n):
for i in range(n):
yield i
s = squares(5)
s
next(s)
next(s)
def squares(n):
print("Begining...")
for i in range(n):
print("yielding ...")
yield i
print("control is back")
print("Exiting....")
s = squares(3)
next(s)
next(s)
next(s)
next(s)
for i in squares(10):
print(i, end=",")
problem
>>> for i in countdown(5):
print(i)
5
4
3
2
1
for i in [1,2,3,4,5]:
print(i)
l = [1,2,3,4,5]
l1 = range(1, 6)
sum([i for i in range(1000)])
l = [i for i in range(1000)]
g = (i for i in range(1000))
g
g = (i for i in range(100000))
sum(g)
def ones():
while True:
yield 1
def take(seq, n):
return [next(seq) for i in range(n)]
take(iter(range(1000)), 10)
o = ones()
o
next(o)
take(o, 10)
def gen():
for i in range(4):
yield
g = gen()
next(g)