Python Virtual Training For Arcesium - Module II - Day 4¶

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

In [1]:
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
In [51]:
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
In [3]:
class GreenLight(Light):
    color = "GREEN"
    
In [4]:
g1 = GreenLight()
In [5]:
l1 = Light()
In [6]:
l1
Out[6]:
<__main__.Light at 0x7f7f5eb77640>
In [7]:
l1.color
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[7], line 1
----> 1 l1.color

AttributeError: 'Light' object has no attribute 'color'
In [9]:
g1.color # color is class variable
Out[9]:
'GREEN'
In [10]:
g2 = GreenLight()
In [11]:
g2.color
Out[11]:
'GREEN'
In [12]:
GreenLight.color = "BottleGreen"
In [13]:
g1.color
Out[13]:
'BottleGreen'
In [14]:
g2.color
Out[14]:
'BottleGreen'
In [15]:
GreenLight.state
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[15], line 1
----> 1 GreenLight.state

AttributeError: type object 'GreenLight' has no attribute 'state'
In [17]:
g1.state # this is instance variable ..
Out[17]:
False
In [18]:
g1.switch_on()
In [19]:
g1.state
Out[19]:
True
In [20]:
g2.state
Out[20]:
False
In [21]:
g2.switch_on() # this method is defined in class
In [22]:
g2.color # is not with instance... so it will enquire to the class from this instance is created
Out[22]:
'BottleGreen'
In [23]:
g2.color
Out[23]:
'BottleGreen'
In [24]:
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
In [26]:
u = User("vikrant")
In [27]:
u.username # it will first check with instance... username is available with instance... so it will not make use of class variable
Out[27]:
'vikrant'
In [28]:
User.username
Out[28]:
''
In [45]:
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
In [46]:
u = User("varun")
In [ ]:
 
In [47]:
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'
In [50]:
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

Working with dictionaries¶

In [52]:
stock = {"ticker": "IBM", 
         "value": 130,
         "high": 135,
         "low": 125}
In [53]:
stock
Out[53]:
{'ticker': 'IBM', 'value': 130, 'high': 135, 'low': 125}
In [54]:
stock['ticker']
Out[54]:
'IBM'
In [55]:
stock['volume']
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[55], line 1
----> 1 stock['volume']

KeyError: 'volume'
In [56]:
stock.get('volume', 0) # if volume is not in keys, then return 0
Out[56]:
0
In [57]:
stock
Out[57]:
{'ticker': 'IBM', 'value': 130, 'high': 135, 'low': 125}
In [58]:
if "volume" in stock:
    v = stock['volume']
else:
    v = 0
In [59]:
stock
Out[59]:
{'ticker': 'IBM', 'value': 130, 'high': 135, 'low': 125}
In [61]:
stock.get("volume") # it returns None
In [62]:
print(stock.get("volume"))
None
In [64]:
!ls *.csv
tables5.csv  tables6.csv  tables8.csv
In [67]:
!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

Example cases fr use case of dictionary¶

In [69]:
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)]
In [70]:
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])
In [71]:
weekly_average(prices, "IBM")
Out[71]:
128.62503125576717
In [74]:
symbols = set([name for  name, day, price in prices])
In [75]:
symbols
Out[75]:
{'APPLE', 'IBM', 'MICROSOFT'}
In [77]:
averages = dict() # empty dictionary
for s in symbols:
    averages[s] = weekly_average(prices, s)
In [78]:
averages
Out[78]:
{'APPLE': 324.51215324332736,
 'IBM': 128.62503125576717,
 'MICROSOFT': 222.47066665915946}
In [ ]:
empty = []
for item in oldlist:
    empty.append(do_something(item))
    
[do_something(item) for item in oldlist]
In [79]:
{s:weekly_average(prices, s)  for s in symbols}
Out[79]:
{'APPLE': 324.51215324332736,
 'IBM': 128.62503125576717,
 'MICROSOFT': 222.47066665915946}
In [80]:
!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
In [83]:
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)}
In [84]:
average_columns("tables8.csv")
Out[84]:
{'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

  • There is file with some text in it. We want to find word frequency of each word.
words.txt
one
one two
one two three

Here are steps

  • read the file first -> inputs - filename
  • unique words -> inputs - all words - output -> set
  • count words -> input unique_words and all_words, -> dict word:freq
In [ ]:
with open("poem.txt") as f:
    for line in f:
        proces line to get words
        
    f.readline() # 
    f.read()
In [85]:
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!
In [86]:
"""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()
Out[86]:
['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

  • read the file first -> inputs - filename
  • unique words -> inputs - all words - output -> set
  • count words -> input unique_words and all_words, -> dict word:freq
In [87]:
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
In [88]:
wordfreq("poem.txt")
Out[88]:
{'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}
In [91]:
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")
In [92]:
create_test_file("words.txt")
In [93]:
!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
In [94]:
wordfreq("words.txt")
Out[94]:
{'nine': 3,
 'one': 3,
 'five': 3,
 'four': 6,
 'two': 7,
 'seven': 2,
 'eight': 5,
 'ten': 12,
 'six': 6,
 'three': 3}
In [95]:
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
In [97]:
set(get_words("words.txt"))
Out[97]:
{'eight', 'five', 'four', 'nine', 'one', 'seven', 'six', 'ten', 'three', 'two'}
In [98]:
set([1, 1, 2, 3, 4, 5, 6, 5, 5])
Out[98]:
{1, 2, 3, 4, 5, 6}
In [101]:
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
In [104]:
freq = wordfreq("words.txt")
In [105]:
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
In [107]:
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
In [108]:
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 **
In [109]:
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 *******
In [110]:
def get_value(record):
    return record[1]
In [111]:
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 ************
In [112]:
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 ************

some simple hacks¶

In [113]:
matrix = [[11, 12, 13],
         [21, 22, 23],
          [31, 32, 33]]

[row for row in matrix]
[col
In [114]:
def transpose(data2d):
    colcount = len(data2d[0])
    return [column(data2d, i) for i in range(colcount)]
In [115]:
transpose(matrix)
Out[115]:
[[11, 21, 31], [12, 22, 32], [13, 23, 33]]
In [116]:
matrix
Out[116]:
[[11, 12, 13], [21, 22, 23], [31, 32, 33]]
In [117]:
list(zip(matrix[0], matrix[1], matrix[2]))
Out[117]:
[(11, 21, 31), (12, 22, 32), (13, 23, 33)]
In [118]:
def add(a, b):
    return a+b
In [119]:
nums  = [2, 5]
In [120]:
add(nums[0], nums[1])
Out[120]:
7
In [121]:
add(nums)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[121], line 1
----> 1 add(nums)

TypeError: add() missing 1 required positional argument: 'b'
In [122]:
add(*nums)# this expand the list items and passes them as individual arg
Out[122]:
7
In [123]:
list(zip(*matrix))
Out[123]:
[(11, 21, 31), (12, 22, 32), (13, 23, 33)]
In [125]:
list(zip(["A","B","C"], [1, 2, 3]))
Out[125]:
[('A', 1), ('B', 2), ('C', 3)]

Keys common among two dictionaries¶

In [128]:
d = {"one":1,
     "two":2}

d1 = {"two":2,
      "three":3,
      "four": 4}
In [131]:
d.keys() & d1.keys() #  intersection
Out[131]:
{'two'}
In [132]:
d.keys() | d1.keys() # union of keys
Out[132]:
{'four', 'one', 'three', 'two'}
In [136]:
d['one']= 1
d['two'] = -2
d['five'] = 5
In [137]:
d
Out[137]:
{'one': 1, 'two': -2, 'five': 5}
In [135]:
d1
Out[135]:
{'two': 2, 'three': 3, 'four': 4}
In [138]:
d1.update(d)
In [139]:
d1
Out[139]:
{'two': -2, 'three': 3, 'four': 4, 'one': 1, 'five': 5}
In [ ]: