Dec 07-11, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch3/module1-day5.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from http://lab.pipal.in for this training. Create a notebook with name module1-day5.ipynb for today's session
>>> product([3, 2, 4])
24
>>> factorial(5)
120
>>> findlens(["one", "two", "three"])
[3, 3, 5]
>>> find_words_of_len(words, 3)
['one', 'two', 'six']
>>> unique([1, 1, 2, 3, 1, 2, 3, 2, 4])
[1, 2, 3, 4]
urls = ['www.abrakadabra.com/dccEcB/EGdd',
'www.abrakadabra.com/gADFeD/bcAF',
'www.abra.com/AGadbb/eagE',
'www.dabra.com/cffdfD/FCAD',
'www.abra.com/GFGaBE/dcfc',
'www.abra.com/gaFegG/Bdaf',
'www.abrakadabra.com/aGabaf/EEfa',
'www.dabra.com/ceEgFD/bGgc',
'www.dabra.com/bDEffC/bcEA']
Write a function min2 which find minimum from given two numbers. Also write a function min3 which can find minimum number from given 3 numbers. Do not make use of bulit in min function.
Write a function rearramge_max to rearrange digits of an integer so as to make maximum integer from it.
>>> rearramge_max(1312)
3211
x = 10
def bar():
x = 20
bar()
print(x)
x = 10
def bar():
print(x)
bar()
x = 10
def bar():
x = x + 1
bar()
x = [1, 1, 1]
def appendone(y):
y.append(1)
appendone(x) # x is argument while calling -> inside the the function y is pointing to x
print(x)
def product(nums): # give names to paramters such that you understand what it is
p = 1.0
#print(p)
for n in nums:
p = p * n # every iteration next number from the list will get multiplied to p
#print(p, n)
return p
product([2, 3, 4, 5, 6]) #
def factorial(n):
"""
Here I can put help for this function.
factorial(5) = 5*4*3*2*1
"""
f = 1
i = 1
while i <= n:
f = f * i
i = i + 1
return f
factorial(6)
factorial(5)
help(factorial)
[1, 2, 3, 4]
list("hello")
list((1, 2, 3, 4))
range(5) # this will create number from 0 to 4
for i in range(5):
print(i, end=",")
for n in nums: # nums has to be a collection
print(n)
for i in [2, 3, 4, 5]:
print(i, end=",")
nums = range(5)
nums # it is a collection of integers from 0 to 4
for i in nums:
print(i, end=",")
for i in range(5):
print(i, end=",")
for i in range(1, 6):
print(i, end=",")
for i in range(1, 21, 2):
print(i, end=",")
def factorial(n):
return product(range(1, n+1)) # make use of already written code
factorial(5)
def findlens(words):
"""
returns a list of length of every word
"""
lens = []
for word in words:
print(lens)
lens.append(len(word))
return lens
words = "one two three four five six".split()
words
findlens(words)
def findlens(words):
"""
returns a list of length of every word
"""
lens = []
for word in words:
#print(lens)
lens.append(len(word))
return lens
findlens(words)
words
def find_words_of_len(words, l):
desired_words = []
for word in words:
if len(word)==l:
desired_words.append(word)
else:
pass # a statement which does nothing
return desired_words
find_words_of_len(words, 3)
find_words_of_len(words, 4)
find_words_of_len(words, 2)
def find_words_of_len(words, l):
desired_words = []
for word in words:
if len(word)==l:
desired_words.append(word)
return desired_words
find_words_of_len(words, 3)
### these statements make visible change in the varioable or output!
return x
s = x +1
s.append(2)
# these statements ..just compute ... and forget
x + 2
x +3
x**2
square(2)
pass
def unique(items):
seen = []
for item in items: # for loop will run only as many times as many items are there in list
if item not in seen:
seen.append(item) # first occurence
return seen
unique([1, 1, 1, 2, 2, 2, 3, 1, 2])
urls = ['www.abrakadabra.com/dccEcB/EGdd',
'www.abrakadabra.com/gADFeD/bcAF',
'www.abra.com/AGadbb/eagE',
'www.dabra.com/cffdfD/FCAD',
'www.abra.com/GFGaBE/dcfc',
'www.abra.com/gaFegG/Bdaf',
'www.abrakadabra.com/aGabaf/EEfa',
'www.dabra.com/ceEgFD/bGgc',
'www.dabra.com/bDEffC/bcEA']
def get_domain(url):
return url.split("/")[0]
def find_domains(urls):
domains = []
for url in urls:
d = get_domain(url)
domains.append(d)
return domains
find_domains(urls)
unique(find_domains(urls))
def find_unique_domains(urls):
domains = find_domains(urls)
return unique(domains)
find_unique_domains(urls)
def min2(x, y):
if x < y:
return x
else:
return y
def min3(x, y, z):
if (x<y) and (x<z):
return x
elif y < z:
return y
else:
return z
3 67 5
\ / /
\ / /
\ / /
min? /
3 /
\ /
\ /
min?
3
def min3(x, y, z):
m = min2(x, y)
return min2(m, z)
min3(34, 2, 32)
def min3(a, b, c):
return min2(min2(a,b), c)
We made functions to abstract few statements together ... now what if we can collect bunch of functions together somewhere?
abs(45)
len("hello")
import os # here os is module name
os
os.getcwd() # get me current working directory
os.getcwd # this is just a function
os.path # this is a module inside os module
os.getcwd # this is function in os module
import math
math.pi # a data/variable
import math
import math as m
del m
math
m
import math as m
math
m
m.pi
m.sin
m.sin(0)
from math import sqrt # this is going to import only sqrt from math
math
sqrt
sqrt(2)
import os
import pandas as pd # it will not work for you as of now
os.getcwd()
pythondir = "/home/vikrant/anaconda/python3"
python_exe = "/".join([pythondir, "bin", "python"]) # platform dependent
python_exe
"\\".join([pythondir, "bin","python"])
os.path.sep
os.path.sep # which is platform specific
os.path.join(pythondir, "bin","python") # remember this is function/not string method
module1_file_path = "/home/vikrant/trainings/2020/arcesium_finop_batch3/module1-day1.ipynb"
os.path.isfile(module1_file_path)
os.path.isdir(module1_file_path)
os.path.exists("/home/vikrant/trainings/2020/arcesium_finop_batch3/module1-day1.ipynb")
os.path.exists("/home/vikrant/trainings/2020/arcesium_finop_batch3/module1-day6.ipynb")
os.path.exists("module1-day1.ipynb") # have given just filename? where will it check? it will check in current folder
os.path.exists("Makefile")
os.path.isfile("module1-day1.ipynb")
os.path.isdir("module1-day1.ipynb")
os.path.getsize("module1-day1.ipynb") # size in bytes
os.path.getsize(os.getcwd()) # this does not actually compute size of cintents of directory
os.listdir() # if you don't pass any arguments, it will list contents of current working directory
os.listdir("/tmp/")
def ls(dirpath=None):
if dirpath == None:
dirpath = os.getcwd()
contents = os.listdir(dirpath)
for f in contents:
print(f)
ls()
ls("/tmp")
ls("/home/vikrant/trainings/2020/arcesium_finop_batch3")
ls("/home/vikrant/trainings/")
os.walk("/home/vikrant/trainings/arcesium_finop_batch3")
records = [
("TATA", 230, 5.5),
("REL", 334, 5.2),
("INF", 3243, 3),
("HCL", 3231, 1)
]
records[0]
records[1]
records[3]
for item in records:
print(item)
for item in records:
print(item[0], item[1], item[2])
x, y, z = (1, 2, 3)
print(x, y, z)
for name, value, gain in records:
print(name)
for name, value, gain in records:
print(name, value)
for name, value, gain in records:
print(value)
for path, dirs, files in os.walk("/home/vikrant/trainings/2020/arcesium_finop_batch3/"):
#print(path, dirs, files)
for f in files:
print(f)
for path, dirs, files in os.walk("/home/vikrant/trainings/2020/arcesium_finop_batch3/"):
#print(path, dirs, files)
for d in dirs:
print(d)
def compute_size(dirpath=None):
if dirpath==None:
dirpath = os.getcwd()
s = 0
for path, dirs, files in os.walk(dirpath):
for f in files:
filepath = os.path.join(path, f)
s = s + os.path.getsize(filepath)
for d in dirs:
dirp = os.path.join(path, d)
s += os.path.getsize(dirp)
return s
os.path.getsize("Makefile")
os.path.getsize("module1-day4-checkpoint.ipynb")
os.path.getsize(".ipynb_checkpoints/module1-day4-checkpoint.ipynb")
compute_size(os.getcwd())/1024/1024
Problem
longlistdir which will print filesname and folndernames in given folder, such that before every folder it shows char d and before every file it shows char f>>> longlistdir("/home/vikrant/trainings")
d arcesium_finop_bathc3
f notes.txt
f hello.py
f scrtach.ipynb
findfiles which finds all files in given directory with a given extension>>> findfiles("/var/", ".log")
['/var/log/bootstrap.log',
'/var/log/dpkg.log',
.
.]
ls()
ls("c:\\Users\\user\\Desktop") # this will not work ..becuase jupyter hub is running on a remote linux machine
os.getcwd()
for i in range(5):
print(i, end=",")
for path, dirs, files in os.walk(os.getcwd()):
for f in files:
print(f)
longlistdir()
def longlistdir(dirpath=None):
if dirpath==None:
dirpath = os.getcwd()
contents = os.listdir(dirpath)
for item in contents:
path = os.path.join(dirpath, item)
if os.path.isfile(path):
print("f",item)
else:
print("d", item)
longlistdir()
longlistdir("/tmp/")
longlistdir("/home/vikrant/trainings")
os.path.isfile("/tmp/xyz.txt")
os.path.exists("/tmp/xyz.txt")
def findfiles(dirpath=None, ext=".log"):
if dirpath==None:
dirpath = os.getcwd()
for path, dirs, files in os.walk(dirpath):
for f in files:
if f.endswith(ext):
print(os.path.join(path, f))
findfiles(".", ".ipynb")
findfiles("/home/vikrant/Downloads/", ".txt")
ls()
import sample
sample.hello()
In jupyter if you want to write files you start with %%file filename, next line start writing contents of file
%%file stats.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))
def median(nums):
nums = sorted(nums)
n = len(nums)
c = n//2
if n%2==0:
return (num[c]+num[c-1])/2.0
else:
return num[c]
import stats
stats.mean([1, 2, 3, 4, 5])
which py files can be imported modules? All those .py files which are in current working directory can be imported as module directly.
if your .py file is in some differet folder , then that folder has to be added to a system variable called PYTHONPATH.
%%file hello.py
import sys
def hello(name):
print("Hello", name)
def welcome(name):
print("Welcome", name)
## here is code which takes command line arguments
# and decides what to do with it
sys.argv # list of commandline argumets
print(sys.argv)
name = sys.argv[1]
welcome(name)
IF YOU WANT TO run a system command from jupyter, this how you run it. you start with !command
!python3 hello.py vikrant
!python3 hello.py arcesium
%%file mean.py
import sys
def mean(nums):
return sum(nums)/len(nums)
numbers = sys.argv[1:] # drop first item and take all remaing
print(mean(numbers))
!python mean.py 1 2 3 4 5
%%file mean.py
import sys
def mean(nums):
return sum(nums)/len(nums)
def ints(strnums):
nums = []
for stn in strnums:
nums.append(int(stn))
return nums
numbers = sys.argv[1:] # drop first item and take all remaing
nums = ints(numbers)
print(mean(nums))
!python mean.py 1 2 3 4 5
%%file add.py
import sys
def add(x, y):
return x+y
x, y = sys.argv[1:3] # start from 1 till 2
add(int(x), int(y))
!python add.py 4 6
2 +3
%%file add.py
import sys
def add(x, y):
return x+y
x, y = sys.argv[1:3] # start from 1 till 2
print(add(int(x), int(y)))
!python3 add.py 5 6
%%file addall.py
import sys
def ints(snums):
nums = []
for s in snums:
nums.append(int(s))
return nums
nums = sys.argv[1:]
print(sum(ints(nums)))
!python3 addall.py 1 2 3 4 5 6