Oct 11-15, 2021 Vikrant Patil
These notes are available online at https://notes.pipal.in/2021/arcesium_finop_batch2/
© Pipal Academy LLP
We will be using jupyter hub from https://lab2.pipal.in for this training.
create a notebook with name module2-day4
Q. What is difference between repr and str
x = "hello" + " " + "worlds"
x # this is not print, this is signal to interpreter (interactive) that I want to just see debug information of 'x'
'hello worlds'
print(x) # this shows what __str__ of x returns
hello worlds
x # shows whatever __repr__ returns
'hello worlds'
y
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-6-9063a9f0e032> in <module> ----> 1 y NameError: name 'y' is not defined
class OutOfStock(Exception):
pass
class VendingMachine:
def __init__(self, capacity):
self.capacity = capacity
self.currentvolume = 0
def refill(self, volume):
if volume > self.capacity:
raise ValueError("Can not refill beyond capacity..not refilling!")
self.currentvolume = volume
def dispense(self):
"""dispenses 200ml at a time
"""
if self.currentvolume>=200:
self.currentvolume -= 200
else:
raise OutOfStock("Vending machine is empty, please refill to use")
def __repr__(self):
return f"VendingMachine<{self.currentvolume}>"
def __str__(self):
return f"VendingMachine<{self.capacity} : {self.currentvolume}>"
vm = VendingMachine(5000)
vm # __repr__ will show up
VendingMachine<0>
print(vm) # calls __str__
VendingMachine<5000 : 0>
str(vm) # again calls __str__
'VendingMachine<5000 : 0>'
class Dummy:
def __repr__(self):
return "Dummy-repr"
d = Dummy()
d
Dummy-repr
print(d)
Dummy-repr
class DummyFoo:
def __str__(self):
return "DummyFoo-str"
df = DummyFoo()
df # default debug information..because we did not implement __repr__!
<__main__.DummyFoo at 0x7fe0145bcc10>
print(df) ## __str__
DummyFoo-str
class OutOfStock(Exception):
pass
class VendingMachine:
def __init__(self, capacity):
self.capacity = capacity
self.currentvolume = 0
def refill(self, volume):
if volume > self.capacity:
raise ValueError("Can not refill beyond capacity..not refilling!")
self.currentvolume = volume
def dispense(self):
"""dispenses 200ml at a time
"""
if self.currentvolume>=200:
self.currentvolume -= 200
print("Dispensed 200ml of beverage!")
else:
raise OutOfStock("Vending machine is empty, please refill to use")
def __repr__(self):
return f"VendingMachine<{self.currentvolume}>"
def __str__(self):
return f"VendingMachine<{self.capacity} : {self.currentvolume}>"
vm = VendingMachine(1000)
vm.refill(800) # volume ->800 ..it is parameter..so local variable to method refill!
vm.dispense()
Dispensed 200ml of beverage!
class Foo:
def __init__(self, x):
self.x = x
def resetx(self, new_x):
self.x = new_x
f = Foo(5)
f.x
5
f.resetx(10)
f.x
10
Lets make a class called Timer. you can start timer, then execute some code and then stop timer. Then you can ask this timer, how much time was taken between start of a timer and stoping the timer.
timer = Timer()
timer.start()
s = 0
for i in range(10000):
for j in range(10000):
s += i*j + 1.1
timer.stop()
print(timer.get_ealapsed_time())
import time
class Timer:
def __init__(self):
self.starttime = -1
self.endtime = -1
def start(self):
self.starttime = time.time() # returns time in seconds from 1 jan 1972
def stop(self):
self.endtime = time.time()
def get_elapsed_time(self):
if self.starttime!=-1 and self.endtime!=-1:
return self.endtime - self.starttime
else:
raise Exception("Was timer started and stopped?..something went wrong")
timer = Timer()
timer.start()
s = 0
for i in range(10000):
for j in range(1000):
s += i*j + 1.1
timer.stop()
print(timer.get_elapsed_time())
2.0697851181030273
class Foo:
def assignx(self, value):
self.x = value
def assigny(self, value):
self.y = value
f = Foo()
f.x
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-60-566eb98641fe> in <module> ----> 1 f.x AttributeError: 'Foo' object has no attribute 'x'
f.y
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-61-be7deaa04b90> in <module> ----> 1 f.y AttributeError: 'Foo' object has no attribute 'y'
f.assignx(5)
f.x
5
bank0
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-65-99deb1bae816> in <module> ----> 1 bank0 NameError: name 'bank0' is not defined
import bank0
bank0.balance += 100
bank0.dailylimit += 1000
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-68-c78082b6ee6f> in <module> ----> 1 bank0.dailylimit += 1000 AttributeError: module 'bank0' has no attribute 'dailylimit'
bank0.dailylimit = 1000
bank0.dailylimit
1000
!python3 cat.py bank1.py
def create_account(balance):
return {"balance": balance} # a dictionary which has balance inside it
def get_balance(account):
return account['balance']
def deposite(account, amount):
account['balance'] +=amount
def withdraw(account, amount):
account['balance'] -= amount
timer.__dict__
{'starttime': 1634188024.7149982, 'endtime': 1634188026.7847834}
timer.__dict__['x'] = 10
time.x = 10
timer.__dict__
{'starttime': 1634188024.7149982, 'endtime': 1634188026.7847834, 'x': 10}
%%file words.txt
one
one two
one two three
one two three four
one two three four five
one two three four five six
one two three four seven six
one two three eight seven six
one two nine eight seven six
one ten nine eight seven six
one ten nine eight seven
one ten nine eight
one ten nine
one ten
one
Writing words.txt
freq = {"one":15, "two":12}
steps
find freq of every word from given text file
wordlist = ["one", "two", "one", "three", "four", "one"]
wordlist
['one', 'two', 'one', 'three', 'four', 'one']
one 1+1
two 1
one
two
one
freq = {}
for word in wordlist:
if word in freq:
freq[word] += 1
else:
freq[word] = 1
freq
{'one': 3, 'two': 1, 'three': 1, 'four': 1}
wordlist
['one', 'two', 'one', 'three', 'four', 'one']
wordlist.count('one')
3
freq = {}
for word in set(wordlist): # set creates unique collection
freq[word] = wordlist.count(word)
freq
{'four': 1, 'two': 1, 'one': 3, 'three': 1}
def get_words(filename):
with open(filename) as f:
return f.read().strip().split()
words = get_words("words.txt")
def wordfreq(words):
freq = {}
for word in words:
if word in freq:
freq[word] += 1
else:
freq[word] = 1
return freq
wordfreq(words)
{'one': 15,
'two': 8,
'three': 6,
'four': 4,
'five': 2,
'six': 5,
'seven': 5,
'eight': 5,
'nine': 5,
'ten': 5}
d = {"x":5, "y":10}
d['x']
5
d['y']
10
d.get('x')
5
d.get("z", 0) # if z is not present in the dictionary then return 0
0
d
{'x': 5, 'y': 10}
d.get("m", 100)
100
d
{'x': 5, 'y': 10}
def wordfreq(words):
freq = {}
for word in words:
freq[word] = freq.get(word, 0) + 1
return freq
wordfreq(words)
{'one': 15,
'two': 8,
'three': 6,
'four': 4,
'five': 2,
'six': 5,
'seven': 5,
'eight': 5,
'nine': 5,
'ten': 5}
def wordfreq1(words):
freq = {}
for word in set(words):
freq[word] = words.count(word)
return freq
{word:words.count(word) for word in set(words)}
{'ten': 5,
'four': 4,
'two': 8,
'seven': 5,
'one': 15,
'three': 6,
'eight': 5,
'five': 2,
'nine': 5,
'six': 5}
{key:value for key,value in zip([1, 2, 3], ['x','y','z'])}
{1: 'x', 2: 'y', 3: 'z'}
def wordfreq2(filename):
words = get_words(filename)
return {word:words.count(word) for word in set(words)}
wordfreq2("words.txt")
{'ten': 5,
'four': 4,
'two': 8,
'seven': 5,
'one': 15,
'three': 6,
'eight': 5,
'five': 2,
'nine': 5,
'six': 5}
freq = wordfreq2("words.txt")
freq
{'ten': 5,
'four': 4,
'two': 8,
'seven': 5,
'one': 15,
'three': 6,
'eight': 5,
'five': 2,
'nine': 5,
'six': 5}
for word in freq: # for loop on dict goes over only keys
print("{word} {f}".format(word=word, f=freq[word]))
ten 5 four 4 two 8 seven 5 one 15 three 6 eight 5 five 2 nine 5 six 5
for word in freq: # for loop on dict goes over only keys
print("{word:5} {f:2d}".format(word=word, f=freq[word]))
ten 5 four 4 two 8 seven 5 one 15 three 6 eight 5 five 2 nine 5 six 5
for word in freq: # for loop on dict goes over only keys
print("{word:5} {f:2d} {bar}".format(word=word, f=freq[word], bar="*"*freq[word]))
ten 5 ***** four 4 **** two 8 ******** seven 5 ***** one 15 *************** three 6 ****** eight 5 ***** five 2 ** nine 5 ***** six 5 *****
for w, f in freq.items(): # .items() method allows going over key and value together
print(w.rjust(5), str(f).ljust(2), "*"*f)
ten 5 ***** four 4 **** two 8 ******** seven 5 ***** one 15 *************** three 6 ****** eight 5 ***** five 2 ** nine 5 ***** six 5 *****
def get_freq(key_value):
return key_value[1]
for w, f in sorted(freq.items(), key=get_freq):
print(w.rjust(5), str(f).ljust(2), "*"*f)
five 2 ** four 4 **** ten 5 ***** seven 5 ***** eight 5 ***** nine 5 ***** six 5 ***** three 6 ****** two 8 ******** one 15 ***************
def get_freq(key_value):
return key_value[1]
for w, f in sorted(freq.items(), key=get_freq, reverse=True):
print(w.rjust(5), str(f).ljust(2), "*"*f)
one 15 *************** two 8 ******** three 6 ****** ten 5 ***** seven 5 ***** eight 5 ***** nine 5 ***** six 5 ***** four 4 **** five 2 **
"{a} {b},{c}".format(a=23, b=232,c="hello")
'23 232,hello'
for i in range(1, 11):
print(i, i*i, i**3)
1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
for i in range(1, 11):
print("{c1} {c2} {c3}".format(c1=i, c2=i*i, c3=i**3))
1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
for i in range(1, 11):
print("{u:2d} {s:3d} {c:4d}".format(u=i, s=i*i, c=i**3))
1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000
pairs = {"one":"ONE",
"three":"THREE",
"four":"FOUR"
}
for k,v in pairs.items():
print(f"{k}:{v}") ## "{k} {v}".format(k=k, v=v)
one:ONE three:THREE four:FOUR
for k,v in pairs.items():
print(f"{k:5}:{v:5}")
one :ONE three:THREE four :FOUR
for k,v in pairs.items():
print(f"{k:5d}:{v:5}") # d is for numbers..digits
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-135-d4db5a2246cc> in <module> 1 for k,v in pairs.items(): ----> 2 print(f"{k:5d}:{v:5}") # d is for numbers..digits ValueError: Unknown format code 'd' for object of type 'str'
import operator as op
conds = [">", ">=", "<", "<=", "", "<>"] ## ">40",">=40", "<>40", "40"
functions = [op.gt, op.ge, op.lt, op.le, op.eq, op.ne]
op.gt(4, 5)
False
funcmap = {}
for cond, func in zip(conds, functions):
funcmap[cond] = func
funcmap
{'>': <function _operator.gt(a, b, /)>,
'>=': <function _operator.ge(a, b, /)>,
'<': <function _operator.lt(a, b, /)>,
'<=': <function _operator.le(a, b, /)>,
'': <function _operator.eq(a, b, /)>,
'<>': <function _operator.ne(a, b, /)>}
funcmap[">"](3, 4)
False
{c:f for c,f in zip(conds, functions)} # dictionary comprehension
{'>': <function _operator.gt(a, b, /)>,
'>=': <function _operator.ge(a, b, /)>,
'<': <function _operator.lt(a, b, /)>,
'<=': <function _operator.le(a, b, /)>,
'': <function _operator.eq(a, b, /)>,
'<>': <function _operator.ne(a, b, /)>}
dict(zip(conds, functions)) # keys from conds and values from functions
{'>': <function _operator.gt(a, b, /)>,
'>=': <function _operator.ge(a, b, /)>,
'<': <function _operator.lt(a, b, /)>,
'<=': <function _operator.le(a, b, /)>,
'': <function _operator.eq(a, b, /)>,
'<>': <function _operator.ne(a, b, /)>}
names = ["IBM","APPLE","AGILENT"]
values = [123.5, 145.6, 344.7]
prices = dict(zip(names, values))
prices
{'IBM': 123.5, 'APPLE': 145.6, 'AGILENT': 344.7}
import operator as op
def greaterthan(x,y):
return x>y
gt = lambda x,y: x>y
conds = [">", ">=", "<", "<=", "", "<>"] ## ">40",">=40", "<>40", "40"
functions = [op.gt, op.ge, op.lt, op.le, op.eq, op.ne]
funcmap = dict(zip(conds, functions))
def split_value_cond(condstr):
value = int("".join([c for c in condstr if c.isdigit()]))
cond = "".join([c for c in condstr if not c.isdigit()])
return value, cond
def COUNTIFS(criterio_list, condstr):
value, cond = split_value_cond(condstr)
return len([item for item in criterio_list if funcmap[cond](item, value)])
COUNTIFS([10,20,10,20,30,40,50,50,60,60], "<>60")
8
COUNTIFS([10,20,10,20,30,40,50,50,60,60], ">60")
0
COUNTIFS([10,20,10,20,30,40,50,50,60,60], "<60")
8
COUNTIFS([10,20,10,20,30,40,50,50,60,60], "<=30")
5