Feb 13-17, 2023 Vikrant Patil
All notes are available online at https://notes.pipal.in/2023/arcesium_finop_jan/
Please login to https://engage.pipal.in/ and launch jupyter lab
For today create a notebook with name module2-day4
notebook names are case sensitive. Make sure you give correct name
© Pipal Academy LLP
class User:
username=""
password=""
def __init__(self,user): # constructor
self.username = user
def set_password(self, pwd):
self.password = pwd
def check_password(self,pwd):
if self.password==pwd:
return True
else:
return False
class Light:
def __init__(self):
self.state = False
def is_on(self):
return self.state == True
def switch_off(self):
if self.state: # if it is on
self.state = False
def switch_on(self):
if not self.state: #if it is off
self.state = True
def toggle(self):
pass # above check is required only if we have some method like this
class GreenLight(Light):
color = "GREEN"
g1 = GreenLight()
l1 = Light()
l1
<__main__.Light at 0x7f7f5eb77640>
l1.color
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[7], line 1 ----> 1 l1.color AttributeError: 'Light' object has no attribute 'color'
g1.color # color is class variable
'GREEN'
g2 = GreenLight()
g2.color
'GREEN'
GreenLight.color = "BottleGreen"
g1.color
'BottleGreen'
g2.color
'BottleGreen'
GreenLight.state
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[15], line 1 ----> 1 GreenLight.state AttributeError: type object 'GreenLight' has no attribute 'state'
g1.state # this is instance variable ..
False
g1.switch_on()
g1.state
True
g2.state
False
g2.switch_on() # this method is defined in class
g2.color # is not with instance... so it will enquire to the class from this instance is created
'BottleGreen'
g2.color
'BottleGreen'
class User:
username="" # this is not good example to make use of class variable
password=""
def __init__(self,user): # constructor
self.username = user # this is right!
def set_password(self, pwd):
self.password = pwd
def check_password(self,pwd):
if self.password==pwd:
return True
else:
return False
u = User("vikrant")
u.username # it will first check with instance... username is available with instance... so it will not make use of class variable
'vikrant'
User.username
''
class User:
def __init__(self, username):
self.username = username
def set_password(self, password):
self.userpwd = password
def check_password(self, password):
return self.userpwd == password
u = User("varun")
u.check_password("varun124")
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[47], line 1 ----> 1 u.check_password("varun124") Cell In[45], line 10, in User.check_password(self, password) 9 def check_password(self, password): ---> 10 return self.userpwd == password AttributeError: 'User' object has no attribute 'userpwd'
class User:
def __init__(self, username):
self.username = username
self.userpwd = None
def set_password(self, password):
self.userpwd = password
def check_password(self, password):
return self.userpwd == password
stock = {"ticker": "IBM",
"value": 130,
"high": 135,
"low": 125}
stock
{'ticker': 'IBM', 'value': 130, 'high': 135, 'low': 125}
stock['ticker']
'IBM'
stock['volume']
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) Cell In[55], line 1 ----> 1 stock['volume'] KeyError: 'volume'
stock.get('volume', 0) # if volume is not in keys, then return 0
0
stock
{'ticker': 'IBM', 'value': 130, 'high': 135, 'low': 125}
if "volume" in stock:
v = stock['volume']
else:
v = 0
stock
{'ticker': 'IBM', 'value': 130, 'high': 135, 'low': 125}
stock.get("volume") # it returns None
print(stock.get("volume"))
None
!ls *.csv
tables5.csv tables6.csv tables8.csv
!python head.py 5 tables8.csv
multiflier-1,multiflier-2,multiflier-3,multiflier-4,multiflier-5,multiflier-6,multiflier-7,multiflier-8,multiflier-9,multiflier-10 1,2,3,4,5,6,7,8,9,10 2,4,6,8,10,12,14,16,18,20 3,6,9,12,15,18,21,24,27,30 4,8,12,16,20,24,28,32,36,40 5,10,15,20,25,30,35,40,45,50
prices = [('IBM', 'Monday', 111.71436961893693),
('IBM', 'Tuesday', 141.21220022208635),
('IBM', 'Wednesday', 112.40571010053796),
('IBM', 'Thursday', 137.54133351926248),
('IBM', 'Friday', 140.25154281801224),
('MICROSOFT', 'Monday', 235.0403622499107),
('MICROSOFT', 'Tuesday', 225.0206535036475),
('MICROSOFT', 'Wednesday', 216.10342426936444),
('MICROSOFT', 'Thursday', 200.38038844494193),
('MICROSOFT', 'Friday', 235.80850482793264),
('APPLE', 'Monday', 321.49182055844256),
('APPLE', 'Tuesday', 340.63612771662815),
('APPLE', 'Wednesday', 303.9065277507285),
('APPLE', 'Thursday', 338.1350605764038),
('APPLE', 'Friday', 318.3912296144338)]
def mean(nums):
return sum(nums)/len(nums)
def weekly_average(prices, symbol):
return mean([price for name, day, price in prices if name==symbol])
weekly_average(prices, "IBM")
128.62503125576717
symbols = set([name for name, day, price in prices])
symbols
{'APPLE', 'IBM', 'MICROSOFT'}
averages = dict() # empty dictionary
for s in symbols:
averages[s] = weekly_average(prices, s)
averages
{'APPLE': 324.51215324332736,
'IBM': 128.62503125576717,
'MICROSOFT': 222.47066665915946}
empty = []
for item in oldlist:
empty.append(do_something(item))
[do_something(item) for item in oldlist]
{s:weekly_average(prices, s) for s in symbols}
{'APPLE': 324.51215324332736,
'IBM': 128.62503125576717,
'MICROSOFT': 222.47066665915946}
!python head.py 5 tables8.csv
multiflier-1,multiflier-2,multiflier-3,multiflier-4,multiflier-5,multiflier-6,multiflier-7,multiflier-8,multiflier-9,multiflier-10 1,2,3,4,5,6,7,8,9,10 2,4,6,8,10,12,14,16,18,20 3,6,9,12,15,18,21,24,27,30 4,8,12,16,20,24,28,32,36,40 5,10,15,20,25,30,35,40,45,50
def makeints(strnums):
return [int(item) for item in strnums]
def column(data2d, col):
return [row[col] for row in data2d]
def mean(nums):
return sum(nums)/len(nums)
def average_columns(filename):
with open(filename) as f:
columnnames = f.readline().strip().split(",")
data2d = [line.strip().split(",") for line in f] # text data..not ints!
data2d = [makeints(row) for row in data2d]
return {col:mean(column(data2d, i)) for i, col in enumerate(columnnames)}
average_columns("tables8.csv")
{'multiflier-1': 4.5,
'multiflier-2': 9.0,
'multiflier-3': 13.5,
'multiflier-4': 18.0,
'multiflier-5': 22.5,
'multiflier-6': 27.0,
'multiflier-7': 31.5,
'multiflier-8': 36.0,
'multiflier-9': 40.5,
'multiflier-10': 45.0}
problem
words.txt
one
one two
one two three
Here are steps
with open("poem.txt") as f:
for line in f:
proces line to get words
f.readline() #
f.read()
import this
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!
"""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!""".split()
['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!']
Here are steps
def get_words(filename):
with open(filename) as f:
return f.read().split()
def wordfreq(filename):
words = get_words(filename)
unique_words = set(words)
freq = {}
for word in unique_words:
freq[word] = words.count(word)
return freq
wordfreq("poem.txt")
{'not': 1,
'at': 1,
'than': 8,
'Explicit': 1,
'Tim': 1,
'Unless': 1,
'Flat': 1,
'only': 1,
'it.': 1,
'ugly.': 1,
'Errors': 1,
'practicality': 1,
'by': 1,
'Peters': 1,
'counts.': 1,
'idea.': 2,
'complicated.': 1,
'--': 1,
'are': 1,
'Readability': 1,
'be': 3,
'cases': 1,
"let's": 1,
'There': 1,
'Python,': 1,
'refuse': 1,
'unless': 1,
'that': 1,
'enough': 1,
'complex.': 1,
'silently.': 1,
'rules.': 1,
'special': 1,
'Complex': 1,
'break': 1,
'--obvious': 1,
'one--': 1,
'obvious': 1,
'first': 1,
'of': 3,
"it's": 1,
'If': 2,
'those!': 1,
'should': 2,
'silenced.': 1,
'Dutch.': 1,
'temptation': 1,
'it': 1,
"aren't": 1,
'now.': 1,
'bad': 1,
'implicit.': 1,
'more': 1,
'a': 2,
'beats': 1,
'the': 5,
'way': 2,
'pass': 1,
'Sparse': 1,
'explicitly': 1,
"you're": 1,
'one': 2,
'The': 1,
'preferably': 1,
'Simple': 1,
'to': 5,
'explain,': 2,
'In': 1,
'is': 10,
'Namespaces': 1,
'guess.': 1,
'honking': 1,
'nested.': 1,
'often': 1,
'purity.': 1,
'Beautiful': 1,
'never.': 1,
'dense.': 1,
'never': 2,
'easy': 1,
'may': 2,
'implementation': 2,
'good': 1,
'Zen': 1,
'Although': 3,
'better': 8,
'face': 1,
'hard': 1,
'*right*': 1,
'idea': 1,
'great': 1,
'do': 2,
'ambiguity,': 1,
'and': 1,
'Now': 1,
'Special': 1}
import random
words = "one two three four five six seven eight nine ten ten ten".split()
def create_test_file(filename):
with open(filename, "w") as f:
for i in range(10):
line = " ".join([random.choice(words) for i in range(5)])
f.write(line)
f.write("\n")
create_test_file("words.txt")
!cat words.txt
six five two two six ten ten one five six ten nine two eight ten five four ten three three four two eight six ten seven four six three one four eight nine nine ten ten seven six four ten eight two eight two four ten ten two one ten
wordfreq("words.txt")
{'nine': 3,
'one': 3,
'five': 3,
'four': 6,
'two': 7,
'seven': 2,
'eight': 5,
'ten': 12,
'six': 6,
'three': 3}
def get_words(filename):
with open(filename) as f:
return f.read().split()
def wordfreq(filename):
words = get_words(filename)
unique_words = set(words)
freq = {}
for word in unique_words:
freq[word] = words.count(word)
return freq
set(get_words("words.txt"))
{'eight', 'five', 'four', 'nine', 'one', 'seven', 'six', 'ten', 'three', 'two'}
set([1, 1, 2, 3, 4, 5, 6, 5, 5])
{1, 2, 3, 4, 5, 6}
def wordfreq(filename):
words = get_words()
freq = {}
for w in words:
freq[w] = freq.get(w, 0) + 1
return freq
def wordfreq(filename):
with open(filename) as f:
freq = {}
for line in f:
words = line.strip().split()
for w in words:
freq[w] = freq.get(w, 0) + 1
return freq
freq = wordfreq("words.txt")
for word, f in freq.items():
print(word, f)
six 6 five 3 two 7 ten 12 one 3 nine 3 eight 5 four 6 three 3 seven 2
for word, f in freq.items():
print(word.rjust(5),":", f)
six : 6 five : 3 two : 7 ten : 12 one : 3 nine : 3 eight : 5 four : 6 three : 3 seven : 2
for word, f in freq.items():
print(word.rjust(5),":", f , "*"*f)
six : 6 ****** five : 3 *** two : 7 ******* ten : 12 ************ one : 3 *** nine : 3 *** eight : 5 ***** four : 6 ****** three : 3 *** seven : 2 **
for word, f in sorted(freq.items()):
print(word.rjust(5),":", f , "*"*f)
eight : 5 ***** five : 3 *** four : 6 ****** nine : 3 *** one : 3 *** seven : 2 ** six : 6 ****** ten : 12 ************ three : 3 *** two : 7 *******
def get_value(record):
return record[1]
for word, f in sorted(freq.items(), key=get_value):
print(word.rjust(5),":", f , "*"*f)
seven : 2 ** five : 3 *** one : 3 *** nine : 3 *** three : 3 *** eight : 5 ***** six : 6 ****** four : 6 ****** two : 7 ******* ten : 12 ************
for word, f in sorted(freq.items(), key=get_value):
print(word.rjust(5),":", str(f).rjust(2) , "*"*f)
seven : 2 ** five : 3 *** one : 3 *** nine : 3 *** three : 3 *** eight : 5 ***** six : 6 ****** four : 6 ****** two : 7 ******* ten : 12 ************
matrix = [[11, 12, 13],
[21, 22, 23],
[31, 32, 33]]
[row for row in matrix]
[col
def transpose(data2d):
colcount = len(data2d[0])
return [column(data2d, i) for i in range(colcount)]
transpose(matrix)
[[11, 21, 31], [12, 22, 32], [13, 23, 33]]
matrix
[[11, 12, 13], [21, 22, 23], [31, 32, 33]]
list(zip(matrix[0], matrix[1], matrix[2]))
[(11, 21, 31), (12, 22, 32), (13, 23, 33)]
def add(a, b):
return a+b
nums = [2, 5]
add(nums[0], nums[1])
7
add(nums)
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[121], line 1 ----> 1 add(nums) TypeError: add() missing 1 required positional argument: 'b'
add(*nums)# this expand the list items and passes them as individual arg
7
list(zip(*matrix))
[(11, 21, 31), (12, 22, 32), (13, 23, 33)]
list(zip(["A","B","C"], [1, 2, 3]))
[('A', 1), ('B', 2), ('C', 3)]
d = {"one":1,
"two":2}
d1 = {"two":2,
"three":3,
"four": 4}
d.keys() & d1.keys() # intersection
{'two'}
d.keys() | d1.keys() # union of keys
{'four', 'one', 'three', 'two'}
d['one']= 1
d['two'] = -2
d['five'] = 5
d
{'one': 1, 'two': -2, 'five': 5}
d1
{'two': 2, 'three': 3, 'four': 4}
d1.update(d)
d1
{'two': -2, 'three': 3, 'four': 4, 'one': 1, 'five': 5}