Python Virtual Training For Arcesium - Module I - Day 5¶

Jun 20-24, 2022 Vikrant Patil

All notes are available online at https://notes.pipal.in/2022/arcesium_finop_batch1/

Please accept the invitation that you have received in your email and login to

https://engage.pipal.in/

© Pipal Academy LLP

lets look back for max and key¶

In [6]:
def foo(): # defination
    pass
In [14]:
foo # referrering function as varaible...referring name foo
Out[14]:
<function __main__.foo()>
In [8]:
x = 10
In [9]:
def func(variable):
    print(variable)
In [10]:
func(x)
10
In [11]:
func(foo)
<function foo at 0x7f9ae5b0e560>
In [15]:
def sumof(x, y, func): # third argument is neither defination of function nor it is calling the function!
    return func(x) + func(y)
In [13]:
def square(x):
    return x*x

def cube(x):
    return x**3
In [16]:
square # this is just referring name square
Out[16]:
<function __main__.square(x)>
In [17]:
def add(a, b):
    ## assumption that user will pass a and b such that they can be added
    return a + b
In [18]:
def testfunc(x, y, z): # 
    #z.split() # if developer assumes that it is a string
    #z ** 2 # if he assumes that it is an integer!
    #z(x) # there is assumption that z is a function so I am using it!
    return z(x) + z(y)
In [19]:
sumof(3, 4, square)
Out[19]:
25
In [20]:
sumof(3, 4, cube)
Out[20]:
91
In [22]:
max([1, 3, 43, 2, 4, 5, 45]) # by default it assumes that if list has numeric data then order by value 
# if it has text data then order by dictionary order
Out[22]:
45

1, 2, 3, 4, 5, 43, 45 # first you will order it in ascending manner..then take last item

In [23]:
nums = [232, 34545, 54, 45646456, 434, 454546, -4545454545454]

I am looking for a number with maximum digits!

In [35]:
def digits(num):
    
    strnum = str(abs(num))
    print("processing", num, "->", type(strnum), strnum)
    return len(strnum)
In [36]:
digits(34343434)
processing 34343434 -> <class 'str'> 34343434
Out[36]:
8
In [37]:
max(nums, key=digits)
processing 232 -> <class 'str'> 232
processing 34545 -> <class 'str'> 34545
processing 54 -> <class 'str'> 54
processing 45646456 -> <class 'str'> 45646456
processing 434 -> <class 'str'> 434
processing 454546 -> <class 'str'> 454546
processing -4545454545454 -> <class 'str'> 4545454545454
Out[37]:
-4545454545454
In [31]:
"hello"
Out[31]:
'hello'
In [32]:
print("hello")
hello
In [33]:
34
Out[33]:
34
In [34]:
print("34")
34
In [42]:
def mymax(items, key):
    m = items[0]
    for item in items:
        if key(item) > key(m):
            m = item
            
    return m
In [41]:
mymax(nums, key=digits)
processing 232 -> <class 'str'> 232
processing 232 -> <class 'str'> 232
processing 34545 -> <class 'str'> 34545
processing 232 -> <class 'str'> 232
processing 54 -> <class 'str'> 54
processing 34545 -> <class 'str'> 34545
processing 45646456 -> <class 'str'> 45646456
processing 34545 -> <class 'str'> 34545
processing 434 -> <class 'str'> 434
processing 45646456 -> <class 'str'> 45646456
processing 454546 -> <class 'str'> 454546
processing 45646456 -> <class 'str'> 45646456
processing -4545454545454 -> <class 'str'> 4545454545454
processing 45646456 -> <class 'str'> 45646456
Out[41]:
-4545454545454
In [43]:
5 > None
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [43], in <cell line: 1>()
----> 1 5 > None

TypeError: '>' not supported between instances of 'int' and 'NoneType'
In [44]:
def mymax(items, key=None):
    m = items[0]
    for item in items:
        if key !=None:
            if key(item) > key(m):
                m = item
        else:
            if item > m:
                m = item
            
    return m
In [45]:
mymax([23,234324, 56, 6, 7,2])
Out[45]:
234324
In [46]:
max([5, 5, 3, 2, -6, 3, 5, 3, 2, 1])
Out[46]:
5
In [47]:
5 > -6
Out[47]:
True
In [48]:
max([5, 5, 3, 2, -6, 3, 5, 3, 2, 1], key=abs)
Out[48]:
-6
In [49]:
def key(x):
    return abs(x)
In [51]:
square
Out[51]:
<function __main__.square(x)>
In [52]:
aliassqr = square
In [53]:
square(5)
Out[53]:
25
In [54]:
aliassqr(5)
Out[54]:
25
In [56]:
def fib(n):
    if n==0 or n ==1:
        return n # because there is return here ..function will not go ahead
    #else: if there would not have been return in if block then else would be needed
    curr, prev = 1, 0
    count = 1
    while count < n: # this has to be carefully
        curr, prev = prev+curr, curr
        # tmp = prev
        # prev = curr
        # curr = curr + tmp
        count += 1 # count = count + 1
        
    return curr
        
In [57]:
fib(10)
Out[57]:
55
In [59]:
for i in range(11):
    print(fib(i))
0
1
1
2
3
5
8
13
21
34
55
In [60]:
def fibr(n):
    if n in [0, 1]:
        return n
    else:
        return fibr(n-1) + fibr(n-2)
In [61]:
fibr(5)
Out[61]:
5
In [63]:
for i in range(11):
    print(fibr(i))
0
1
1
2
3
5
8
13
21
34
55
In [64]:
def unique(items):
    seen = []
    for item in items:
        if item not in seen:
            seen.append(item)
    return seen
In [65]:
unique([1, 2, 2, 3, 1, 2, 3, 2, 4, 1])
Out[65]:
[1, 2, 3, 4]
In [66]:
["1K","2K","3K"]
Out[66]:
['1K', '2K', '3K']
In [67]:
def numeric(value):
    return int(value.replace("K","000"))
In [73]:
def numeric(value):
    if value.endswith("K"):
        return int(value.replace("K","000"))
    elif value[-1] == "M":
        return int(value.replace("M","000000"))
    elif value[-1] == "B":
        return int(value.replace("B","000000000"))
    else:
        return int(value)
In [71]:
def numeric(value):
    return int(value.replace("K","000").replace("M", "000000").replace("B", "000000000"))
In [72]:
max(["1K","2K","3K"], key=numeric)
Out[72]:
'3K'
In [76]:
words = ["one", "two", "three", "four", "five", "six", "seven"]
In [77]:
max(words, key=len)
Out[77]:
'three'
In [78]:
def find_word_with_len(words, length):
    words_ = []
    for word in words:
        if len(word) == length:
            words_.append(word)
    return words_
In [80]:
find_word_with_len(words, 5)
Out[80]:
['three', 'seven']
In [82]:
len(max(words, key=len))
Out[82]:
5
In [83]:
find_word_with_len(words, len(max(words, key=len)))
Out[83]:
['three', 'seven']

Built in modules¶

In [162]:
import os ## os is built in module in python ..and we are importing it here
In [85]:
os.getcwd() # this is function inside os module...returns current working
Out[85]:
'/home/vikrant/trainings/2022/arcesium_finop_batch1'

"/home/username"

In [86]:
os.path # is submodule inside os module
Out[86]:
<module 'posixpath' from '/home/vikrant/usr/local/python3.10/lib/python3.10/posixpath.py'>
In [87]:
filename = "hello.txt"
In [90]:
"/".join(["","dsfdf", "sds","asasd"]) # this code will not work on windows because path sepearator is different on windows
Out[90]:
'/dsfdf/sds/asasd'
In [91]:
os.path.join("xys", "sdsd", "sds", "dsfdsf") 
Out[91]:
'xys/sdsd/sds/dsfdsf'
In [92]:
filepath = os.path.join(os.getcwd(), filename)
In [93]:
print(filepath)
/home/vikrant/trainings/2022/arcesium_finop_batch1/hello.txt
In [99]:
"day1.txt" ## this is relative path ..path with respect to current working directory
Out[99]:
'day1.txt'
In [95]:
!ls # this is unix/mac/linux command to list files in current directory
day1.txt    index.ipynb		module1-day2.html   module1-day4.html	push
day2.txt    Makefile		module1-day2.ipynb  module1-day4.ipynb
day3.txt    module1-day1.html	module1-day3.html   module1-day5.html
index.html  module1-day1.ipynb	module1-day3.ipynb  module1-day5.ipynb
In [96]:
filepath = "module1-day1.ipynb" # relative path
In [98]:
os.path.getsize(filepath) # gives size in bytes
Out[98]:
67735
In [102]:
!ls .. # you can see data.txt is in parent folder
arcesium_finop_batch1  data.txt
In [104]:
os.path.getsize("data.txt") #with relative path this will fail
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
Input In [104], in <cell line: 1>()
----> 1 os.path.getsize("data.txt")

File ~/usr/local/python3.10/lib/python3.10/genericpath.py:50, in getsize(filename)
     48 def getsize(filename):
     49     """Return the size of a file, reported by os.stat()."""
---> 50     return os.stat(filename).st_size

FileNotFoundError: [Errno 2] No such file or directory: 'data.txt'
In [107]:
os.path.getsize(os.path.join("..", "data.txt"))  # correct relative path
Out[107]:
0
In [108]:
absoutepath =  os.path.join(os.getcwd(), "module1-day1.ipynb")
In [109]:
absoutepath
Out[109]:
'/home/vikrant/trainings/2022/arcesium_finop_batch1/module1-day1.ipynb'
In [110]:
os.path.getsize(absoutepath)
Out[110]:
67735
In [111]:
os.path.isfile(absoutepath)
Out[111]:
True
In [114]:
cwd = os.getcwd()
In [115]:
parent = os.path.join(cwd, "..")
In [117]:
os.path.getsize(os.path.join(parent, "data.txt"))
Out[117]:
0
In [118]:
os.path.join(parent, "data.txt")
Out[118]:
'/home/vikrant/trainings/2022/arcesium_finop_batch1/../data.txt'
In [119]:
os.path.abspath(os.path.join(parent, "data.txt"))
Out[119]:
'/home/vikrant/trainings/2022/data.txt'
In [120]:
filepath = "/home/vikrant/Downloads/000000001.pdf"
In [121]:
os.path.getsize(filepath)
Out[121]:
28370500
In [122]:
os.listdir() # lists all the files in current working directory
Out[122]:
['index.html',
 'module1-day5.html',
 'day2.txt',
 'push',
 'module1-day5.ipynb',
 'index.ipynb',
 'module1-day1.html',
 'module1-day1.ipynb',
 'module1-day2.ipynb',
 'module1-day3.html',
 'module1-day2.html',
 'day3.txt',
 'day1.txt',
 'module1-day3.ipynb',
 'module1-day4.html',
 'Makefile',
 '.ipynb_checkpoints',
 'module1-day4.ipynb']
In [123]:
def print_listdir():
    for file in os.listdir():
        print(file)
    
In [124]:
print_listdir()
index.html
module1-day5.html
day2.txt
push
module1-day5.ipynb
index.ipynb
module1-day1.html
module1-day1.ipynb
module1-day2.ipynb
module1-day3.html
module1-day2.html
day3.txt
day1.txt
module1-day3.ipynb
module1-day4.html
Makefile
.ipynb_checkpoints
module1-day4.ipynb
In [125]:
def print_listdir():
    for file in os.listdir():
        if os.path.isfile(file):
            print("f", file) # print f for file
        else:
            print("d", file) # print d for directory
In [126]:
print_listdir()
f index.html
f module1-day5.html
f day2.txt
f push
f module1-day5.ipynb
f index.ipynb
f module1-day1.html
f module1-day1.ipynb
f module1-day2.ipynb
f module1-day3.html
f module1-day2.html
f day3.txt
f day1.txt
f module1-day3.ipynb
f module1-day4.html
f Makefile
d .ipynb_checkpoints
f module1-day4.ipynb
In [127]:
def print_listdir():
    for file in os.listdir():
        if os.path.isfile(file):
            print("f", file, os.path.getsize(file)) # print f for file
        else:
            print("d", file, os.path.getsize(file)) # print d for directory
In [129]:
print_listdir()
f index.html 577223
f module1-day5.html 684080
f day2.txt 1602
f push 0
f module1-day5.ipynb 37517
f index.ipynb 2231
f module1-day1.html 756305
f module1-day1.ipynb 67735
f module1-day2.ipynb 56791
f module1-day3.html 733409
f module1-day2.html 737138
f day3.txt 1424
f day1.txt 1690
f module1-day3.ipynb 57634
f module1-day4.html 825133
f Makefile 627
d .ipynb_checkpoints 4096
f module1-day4.ipynb 87198
In [ ]:
 
In [133]:
def print_listdir():
    for file in os.listdir():
        if os.path.isfile(file):
            print("f", file.ljust(20), os.path.getsize(file)) # print f for file
        else:
            print("d", file.ljust(20), os.path.getsize(file)) # print d for directory
In [134]:
print_listdir()
f index.html           577223
f module1-day5.html    684080
f day2.txt             1602
f push                 0
f module1-day5.ipynb   37517
f index.ipynb          2231
f module1-day1.html    756305
f module1-day1.ipynb   67735
f module1-day2.ipynb   56791
f module1-day3.html    733409
f module1-day2.html    737138
f day3.txt             1424
f day1.txt             1690
f module1-day3.ipynb   57634
f module1-day4.html    825133
f Makefile             627
d .ipynb_checkpoints   4096
f module1-day4.ipynb   87198
In [135]:
"helo".ljust(20)
Out[135]:
'helo                '
In [136]:
files = os.listdir()
In [137]:
max(files)
Out[137]:
'push'
In [138]:
files
Out[138]:
['index.html',
 'module1-day5.html',
 'day2.txt',
 'push',
 'module1-day5.ipynb',
 'index.ipynb',
 'module1-day1.html',
 'module1-day1.ipynb',
 'module1-day2.ipynb',
 'module1-day3.html',
 'module1-day2.html',
 'day3.txt',
 'day1.txt',
 'module1-day3.ipynb',
 'module1-day4.html',
 'Makefile',
 '.ipynb_checkpoints',
 'module1-day4.ipynb']
In [139]:
max(files, key=os.path.getsize)
Out[139]:
'module1-day4.html'
In [148]:
os.listdir("/home/vikrant/trainings/") # files and folder inside directory /home/vikrant/trainings/
Out[148]:
['2018',
 'nakul',
 'day5.org',
 '2022',
 'hello.py',
 'day5.org~',
 '2020',
 'trainingvenv',
 '2021',
 '2019',
 '2017',
 'indexdata.xlsx']
In [154]:
def print_listdir(folder=""):
    if folder:
        files = os.listdir(folder)
    else:
        files = os.listdir()
    for file in files:
        path = os.path.join(folder, file)
        if os.path.isfile(path):
            print("f", file.ljust(20), os.path.getsize(path)) # print f for file
        else:
            print("d", file.ljust(20), os.path.getsize(path)) # print d for directory
In [155]:
print_listdir()
f index.html           577223
f module1-day5.html    705642
f day2.txt             1602
f push                 0
f module1-day5.ipynb   47203
f index.ipynb          2231
f module1-day1.html    756305
f module1-day1.ipynb   67735
f module1-day2.ipynb   56791
f module1-day3.html    733409
f module1-day2.html    737138
f day3.txt             1424
f day1.txt             1690
f module1-day3.ipynb   57634
f module1-day4.html    825133
f Makefile             627
d .ipynb_checkpoints   4096
f module1-day4.ipynb   87198
In [156]:
print_listdir("/home/vikrant/trainings/")
d 2018                 4096
d nakul                4096
f day5.org             243
d 2022                 4096
f hello.py             0
f day5.org~            216
d 2020                 4096
d trainingvenv         4096
d 2021                 4096
d 2019                 4096
d 2017                 4096
f indexdata.xlsx       6301

"" , [], {}, these result in False in if condition

In [158]:
if []:
    pass
else:
    print("here")
here
In [159]:
if [1, 2]:
    print("hello")
else:
    print("here")
hello
In [160]:
if "":
    print("hello")
else:
    print("here") #<
here
In [161]:
if "":
    print("hello")
else:
    print("here") #<
here
In [163]:
import math
In [164]:
import random
In [165]:
random.random() # random number between 0, 1
Out[165]:
0.5896644221780958
In [166]:
random.choice(words)
Out[166]:
'two'

Writing our own modules and scripts¶

In [171]:
%%file mystas.py

import math

def mean(nums):
    return sum(nums)/len(nums)

def std(nums):
    m = mean(nums)
    s = 0
    for n in nums:
        s += (n-m)**2
        
    return math.sqrt(s/(len(nums)-1))
Overwriting mystas.py
In [168]:
import mystas
In [169]:
mystas.mean([12, 3, 34, 5, 4, 56])
Out[169]:
19.0
In [170]:
mystas.std([12, 3, 34, 5, 4, 56])
Out[170]:
21.540659228538015

Writing scripts¶

In [172]:
%%file hello.py
import sys

def say_hello(name):
    print("Hello", name.title())
    

print(sys.argv) # list of arguments that we will pass to our script
say_hello(sys.argv[1])
Writing hello.py
In [173]:
!python hello.py vikrant
['hello.py', 'vikrant']
Hello Vikrant
In [185]:
!python hello.py vikrant patil sadjsa kjhsdkjsa askhdkjsa
['hello.py', 'vikrant', 'patil', 'sadjsa', 'kjhsdkjsa', 'askhdkjsa']
Hello Vikrant
In [188]:
%%file sqaure.py
import sys

print(float(sys.argv[1])**2) # there is no return inside python script ..but we print
Overwriting sqaure.py
In [189]:
!python sqaure.py 5
25.0
In [183]:
!python sqaure.py 56
3136.0

problem

  • Write a script add.py which takes two numbers from commandline and prints addition of those
In [184]:
input("Enter a number")
Out[184]:
'656'
In [186]:
def sqaure(x):
    return x*x   # return is a statement ..it is not a function
In [187]:
def sqaure(x):
    return(x*x)# this is not needed!
In [194]:
%%file arguments.py
import sys

print(sys.argv) # sys.argv is a list ... you handle it like a like
Overwriting arguments.py
In [195]:
!python arguments.py 1 2 3 4 4 5
['arguments.py', '1', '2', '3', '4', '4', '5']
In [ ]: