Sep 26-28, 2018 Vikrant Patil
These notes are available online at http://notes.pipal.in/2018/vmware-practical-master-sep/
© Pipal Academy LLP
We will be using anaconda (python 3) distribution for this training. it can be downloaded from
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
class RedPoint(Point):
color = "red"
r1 = RedPoint(2,3)
r2 = RedPoint(3,4)
r1.color
r2.color
class ColoredPoint(Point):
def __init__(self, x, y, color):
super().__init__(x,y)
self.color = color
c1 = ColoredPoint(0,0, "white")
c1.color
class A:
def __init__(self):
print("A.__init__")
class B(A):
def __init__(self):
super().__init__()
print("B.__init__")
class C(B):
def __init__(self):
super().__init__()
print("C.__init__")
class D(C,B):
def __init__(self):
super().__init__()
print("D.__init__")
class A1:
def __init__(self):
print("A1.__init__")
class B1(A1):
def __init__(self):
A1.__init__(self)
print("B1.__init__")
class C1(B1):
def __init__(self):
B1.__init__(self)
print("C1.__init__")
class D1(C1,B1):
def __init__(self):
C1.__init__(self)
B1.__init__(self)
print("D1.__init__")
c = C()
c1 = C1()
d1 = D1()
d = D()
C.mro()
with open("data.csv") as csv:
print(csv.read())
csv.read()
if True:
x = 5
else:
y = 6
x
y
import time
class Timer:
def __init__(self):
self.starttime = 0
self.endtime = 0
def start(self):
self.starttime = time.time()
def stop(self):
self.endtime = time.time()
def get_time_taken(self):
return self.endtime - self.starttime
t = Timer()
t.start()
time.sleep(2)
t.stop()
t.get_time_taken()
import time
class Timer:
def __init__(self):
self.starttime = 0
self.endtime = 0
def start(self):
self.starttime = time.time()
def stop(self):
self.endtime = time.time()
def get_time_taken(self):
return self.endtime - self.starttime
def __enter__(self):
self.start()
return self
def __exit__(self, exception_type, exceptioin, traceback):
self.stop()
print("Time taken :", self.get_time_taken())
with Timer() as t:
s = 0.0
for i in range(10000):
for j in range(1000):
s += i*j*1.0
problem
s = "hello world!"
b = b"hello world!"
b
import socket
class LazyConnection:
def __init__(self, host, port):
self.host = host
self.port = port
self.sock = None
def connect(self):
if not self.sock:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.connect((self.host, self.port))
def __enter__(self):
print("Entering .... with block")
self.connect()
return self.sock
def __exit__(self, ecptyp, ecpv, tcb):
print("Exiting with block ...")
self.sock.close()
self.sock = None
with LazyConnection(host="127.0.0.1", port=3456) as sock:
sock.send(b"hello!")
reply = sock.recv(10)
print(reply)
%%file socketserver.py
## this is python2.7 code .. you need to port it to python 3
import SocketServer
import socket
import sys
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
data = self.request.recv(2048).strip()
print "%s wrote: " % self.client_address[0]
print data
if data=="close":
self.request.close()
self.server.shutdown()
else:
self.request.send(data.upper())
def start_server():
HOST, PORT = "127.0.0.1", 3456
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
def send_client_msg():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("127.0.0.1",3456))
s.send("This is junk data KJHJKASGHD jhG ASDGH AHSGDJASGH DFJKAGH:OP{IO "*10)
received = s.recv(1024)
s.close()
print "Received: " + received
if __name__ == "__main__":
start_server()
import datetime
datetime.datetime.now()
class Mydate(datetime.datetime):
pass
Mydate.now()
class Foo:
def instance():
return Foo()
class Bar(Foo):
pass
Foo.instance()
Bar.instance()
class Foo:
@classmethod
def instance(cls):
return cls()
def __repr__(self):
return "{}".format(type(self))
class Bar(Foo):
pass
Bar.instance()
sum([1,2,3,4,5])
sum(["1",2,3,3])
def incr(x):
try:
return x + 1
except TypeError as t:
print("Warning:", t)
return None
r = incr(3)
r
r = incr("3")
print(r)
def incr(x, y=3):
try:
x = int(x)
except TypeError as t:
print("Warning:", t)
return None
else:
return x + y ## this block gets executed if no exception occurs
finally:
pass # cleanup actions gets executed either case
def incr_(x, y=3):
try:
x = int(x)
except TypeError as t:
print("Warning:", t)
return None
else:
return x + y ## this block gets executed if no exception occurs
finally:
y = 5 # cleanup actions gets executed either case
print("finally",y)
incr_(2)
def append(x, y=None):
if not y:
y = []
try:
y.append(x)
except TypeError as t:
print("Warning:", t)
return None
else:
return y ## this block gets executed if no exception occurs
finally:
y.append(1) # cleanup actions gets executed either case
print("finally",y)
append(2)
def append(x, y=None):
if not y:
y = []
try:
y.append(x)
except TypeError as t:
print("Warning:", t)
return None
except NameError as n:
pass
else:
return y[:] ## this block gets executed if no exception occurs
finally:
y.append(1) # cleanup actions gets executed either case
print("finally",y)
append(2)
%%file csvparser.py
def csvparser(filename):
with open(filename,"r") as f:
return [line.strip().split(",") for line in f]
def test_parser():
assert csvparser("data.csv") == [[]]
!pytest -v csvparser.py
%%file csvparser1.py
def csvparser(filename):
with open(filename,"r") as f:
return [line.strip().split(",") for line in f]
def test_parser():
assert csvparser("data.csv") == [['A1', 'A2', 'A3', 'A4'],
['B1', 'B2', 'B3', 'B4'],
['C1', 'C2', 'C3', 'C4'],
['D1', 'D2', 'D3', 'D4']]
!pytest -v csvparser1.py
%%file functions.py
"""
This module for testing docstrings!
"""
def square(x):
"""
This is a function to compute square!
>>> square(2)
4
>>> square(-1)
1
"""
return x*x
!python -m doctest -v functions.py
!pydoc functions
%%file csvparser2.py
import pytest
def csvparser(filename):
with open(filename,"r") as f:
return [line.strip().split(",") for line in f]
def numeric_csvparser(filename):
with open(filename,"r") as f:
data = [line.strip().split(",") for line in f]
return [[float(s) for s in row] for row in data]
def test_parser():
assert csvparser("data.csv") == [['A1', 'A2', 'A3', 'A4'],
['B1', 'B2', 'B3', 'B4'],
['C1', 'C2', 'C3', 'C4'],
['D1', 'D2', 'D3', 'D4']]
assert csvparser("data1.csv") == [['A1', 'A2', 'A3', 'A4'],
['B1', '', 'B3', 'B4'],
['C1', 'C2', 'C3', 'C4'],
['D1', 'D2', '', 'D4']]
with pytest.raises(ValueError):
numeric_csvparser("data2.csv")
!cat data.csv
%%file data1.csv
A1,A2,A3,A4
B1,,B3,B4
C1,C2,C3,C4
D1,D2,,D4
%%file data2.csv
1,2,3,4
1,1,,1
,2,2,2
3,3,3,3
!pytest -v csvparser2.py
%%file csvparser3.py
import pytest
def csvparser(filename):
with open(filename,"r") as f:
return [line.strip().split(",") for line in f]
def numeric_csvparser(filename):
with open(filename,"r") as f:
data = [line.strip().split(",") for line in f]
return [[float(s) for s in row] for row in data]
def float_(x, value=0):
try:
return float(x)
except ValueError as v:
print("Detected unusual float!")
return 0
def numeric_csvparser_(filename):
with open(filename,"r") as f:
data = [line.strip().split(",") for line in f]
return [[float_(s) for s in row] for row in data]
def test_parser():
assert csvparser("data.csv") == [['A1', 'A2', 'A3', 'A4'],
['B1', 'B2', 'B3', 'B4'],
['C1', 'C2', 'C3', 'C4'],
['D1', 'D2', 'D3', 'D4']]
assert csvparser("data1.csv") == [['A1', 'A2', 'A3', 'A4'],
['B1', '', 'B3', 'B4'],
['C1', 'C2', 'C3', 'C4'],
['D1', 'D2', '', 'D4']]
def test_numeric_parser():
with pytest.raises(ValueError):
numeric_csvparser("data2.csv")
def test_parser_missing_values():
assert numeric_csvparser_("data2.csv") == [[1,2,3,4],[1,1,0,1],[0,2,2,2],[3,3,3,3]]
def numeric_csvparser(filename):
with open(filename,"r") as f:
data = [line.strip().split(",") for line in f]
return [[float(s) for s in row] for row in data]
numeric_csvparser("data2.csv")
!pytest -v csvparser3.py
def hello():
msg = yield "hello! how are you?"
yield "What the hell do you mean? " + msg
h = hello()
h
next(h)
h.send("hello, I am fine!")
next(h)
h = hello()
h.send("hello, how are you?")
h.send(None)
next(h)
h.send("hello, I am fine!")
%%file csvparser4.py
import pytest
import tempfile
def csvparser(filename):
with open(filename,"r") as f:
return [line.strip().split(",") for line in f]
def numeric_csvparser(filename):
with open(filename,"r") as f:
data = [line.strip().split(",") for line in f]
return [[float(s) for s in row] for row in data]
def float_(x, value=0):
try:
return float(x)
except ValueError as v:
print("Detected unusual float!")
return 0
def numeric_csvparser_(filename):
with open(filename,"r") as f:
data = [line.strip().split(",") for line in f]
return [[float_(s) for s in row] for row in data]
@pytest.fixture
def data():
x,filename = tempfile.mkstemp("csv")
with open(filename, "w") as f:
f.write('A1,A2,A3,A4\nB1,B2,B3,B4\nC1,C2,C3,C4\nD1,D2,D3,D4')
yield filename
@pytest.fixture
def data1():
x,filename = tempfile.mkstemp("csv")
with open(filename, "w") as f:
f.write('A1,A2,A3,A4\nB1,,B3,B4\nC1,C2,C3,C4\nD1,D2,,D4')
yield filename
@pytest.fixture
def data2():
x,filename = tempfile.mkstemp("csv")
with open(filename, "w") as f:
f.write('1,2,3,4\n1,1,,1\n,2,2,2\n3,3,3,3')
yield filename
def test_parser(data, data1):
assert csvparser(data) == [['A1', 'A2', 'A3', 'A4'],
['B1', 'B2', 'B3', 'B4'],
['C1', 'C2', 'C3', 'C4'],
['D1', 'D2', 'D3', 'D4']]
assert csvparser(data1) == [['A1', 'A2', 'A3', 'A4'],
['B1', '', 'B3', 'B4'],
['C1', 'C2', 'C3', 'C4'],
['D1', 'D2', '', 'D4']]
def test_numeric_parser(data2):
with pytest.raises(ValueError):
numeric_csvparser(data2)
def test_parser_missing_values(data2):
assert numeric_csvparser_(data2) == [[1,2,3,4],[1,1,0,1],[0,2,2,2],[3,3,3,3]]
import tempfile
tempfile.mkstemp("csv")
open("data2.csv").read()
!pytest -v csvparser4.py
%%file weekday.py
import datetime
def now():
return datetime.datetime.now()
def weekday():
n = now()
return n.strftime("%A")
import weekday
weekday.weekday()
%%file test_weekday.py
import weekday
import datetime
def test_weekday(monkeypatch):
def fakenow():
return datetime.datetime(*today)
monkeypatch.setattr(weekday, "now", fakenow)
today = (2018, 1, 1)
assert weekday.weekday() == "Monday"
today = (2018, 1, 2)
assert weekday.weekday() == "Tuesday"
def add(x,y):
return x+y
add(2,3)
params = (3,4)
add(params)
add(*params)
!pytest -v test_weekday.py
import cProfile
import time
def square(x):
time.sleep(1)
return x*x
def SSQ(numbers):
ssq = sum(square(i) for i in numbers)
s = sum(numbers)
e = sum(i for i in numbers if i%2==0)
return ssq + s +e
%%file ssq.py
import time
def square(x):
time.sleep(1)
return x*x
def SSQ(numbers):
ssq = sum(square(i) for i in numbers)
s = sum(numbers)
e = sum(i for i in numbers if i%2==0)
return ssq + s +e
if __name__ == "__main__":
SSQ(range(10))
SSQ(range(10))
cProfile.run("SSQ(range(10))")
!python -m cProfile -s cumtime ssq.py
import math
def fun(n):
s = 0
for i in range(n):
s += math.sqrt(i) ##math.sqrt has repeted hidden call for checking attribute and then accesing it!
return s
def morefun(n):
s = 0
sqrt = math.sqrt ### locals are faster than global variables
for i in range(n):
s += sqrt(i)
return s
n = 100000000
with Timer() as t:
fun(n)
with Timer() as t:
morefun(n)
python comes with gdb like debugger pdb. it works just like gdb, you can put break points, execute till break point, examine data, continue to next break point. delete /disable and add new breakpoints.
import os
os.getcwd() ### current working directory
os.listdir(".") ## returns list of all files and folders from given directory
len(os.listdir("."))
os.path.exists("./hello.py")
os.path.exists("day1.html")
os.path.join(os.getcwd(), "output.txt")
os.path.isfile("day1.html")
problem
!tree ~/trainings/2018/
def tree(path, level=0):
print("| "*level + "|--"+ path)
for item in os.listdir(path):
p = os.path.join(path, item)
if os.path.isfile(p):
print("| "*level + "|--" + item)
else:
tree(p, level+1)
tree(".")
os.mkdir("test")
os.path.isdir("test")
os.makedirs("test/hello/nested")
os.rmdir("test/hello/nested/")
tree("test/")
os.chdir("test/")
os.getcwd()
os.chdir("../")
for path, dirs, files in os.walk("."):
for f in files:
print(os.path.join(path, f))
os.path.basename("/home/vikrant/programming/explorations/python/example.py")
os.path.dirname("/home/vikrant/programming/explorations/python/example.py")
os.access("test/", os.R_OK) # check read permission
os.access("test/", os.X_OK) # check execute permission
os.access("test/", os.W_OK) #write
import stat
os.chmod("test/" , stat.S_IREAD|stat.S_IWRITE)
!ls -ld test/
os.lstat("test/")
stat.S_IMODE(os.lstat("test/").st_mode)
os.path.getsize("day1.html")
problem
def dirsize(dirname):
s = 0
for path, dirs , files in os.walk(dirname):
s += sum(os.path.getsize(os.path.join(path, f)) for f in files)
return s
dirsize(".")/1024/1024
!du -sh .
def find(dirpath, ext="log"):
for path, dirs, files in os.walk(dirpath):
for f in files:
if f.endswith("."+ext):
yield os.path.join(path, f)
def countdown(n):
print("start")
while n>0:
yield n
print("back")
n -= 1
print("end")
for i in countdown(10):
print(i, end=" ")
def lineemitter(files):
for file in files:
with open(file) as f:
yield f.readline()
logfiles = find("/var/log/")
lines = lineemitter(logfiles)
counter = countdown(5)
next(counter)
def grep(seq, pattern):
for item in seq:
if pattern in item:
yield item
searchresult = grep(lines, "X11/75dpi")
next(searchresult)
def take(n, seq):
return [next(seq) for i in range(n)]
take(5, logfiles)
for file in logfiles:
print(file)
os.path.getmtime(".")
import time
time.ctime(os.path.getmtime("."))
problem
import time
def modifiedwithin(files, seconds):
now = time.time()
for f in files:
t = os.path.getmtime(f)
if now - t <= seconds:
yield f
logfiles = find("/var/log")
latest = modifiedwithin(logfiles, 1000)
list(latest)
import shutil
shutil.copy("./test_weekday.py", "/tmp/") #copies files
os.path.exists("/tmp/test_weekday.py")
shutil.copy2("./test_weekday.py", "/tmp/")# copies files with metadata
shutil.copytree(".", "/tmp/training")
shutil.copytree(".", "/tmp/training1")
tree("/tmp/training1/")
import filecmp
filecmp.cmp("./test_weekday.py", "/tmp/training1/test_weekday.py")
problem