Aug 17-21, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch2/module1-day5.html
© Pipal Academy LLP
Day 1 | Day 2 | Day 3 | Day 4 | Day 5
We will be using jupyter hub from http://lab2.pipal.in for this training. Use notebook with name module1-day5.ipynb for today's session.
problem Write a function rearrange_max to rearrange digits of an integer so as to make maximum integer from it.
intvalue = 34656
strnum = str(intvalue)
strnum
for c in strnum:
print(c, end=",")
digits = []
for c in strnum:
digits.append(c)
digits
list(strnum)
sorted(digits)
sorted(digits, reverse=True)
"".join(sorted(digits, reverse=True))
int("".join(sorted(digits, reverse=True)))
digits.sort(reverse=True)
digits
def rearrangemax(intvalue):
strnum = str(intvalue)
digits = list(strnum)
digits.sort(reverse=True)
return int("".join(digits))
strnum = list(str(232324))
def rearrangemax(intvalue):
digits = list(str(intvalue))
digits.sort(reverse=True)
return int("".join(digits))
rearrangemax(45465)
num = 3452
a = list(str(num))
lista = []
for i in a:
lista.append(a[i])
for i in a:
print(i, type(i))
a = ['3', '4','5','2']
a['3']
a[0]
digits = list(str(num))
lista = []
for digit in digits: ## i itself
lista.append(digit)
lista
nums = [2, 3, 4, 56, 6]
for n in nums:
print(n, end=",")
sorted(nums)
digits = list(str(5465))
digits
sorted(digits, reverse=True) # this function returns new sorted list
digits.sort(reverse=True) ## does not return anything. it sorts the list inplace
digits
x = digits.sort(reverse=True)
print(x)
digits
x = sorted(digits, reverse=True)
print(x)
int('6,6,5,4,3')
int('6643')
",".join(digits)
"".join(digits)
def fib(n): # recursion
if n == 1 or n == 0:
return 1
return fib(n-1) + fib(n-2)
fib(5)
fib(5)
fib(4) + fib(3)
fib(3)+fib(2) fib(2) + fib(1)
fib(1)+fib(0)
import os
import math
import math as m
math.sqrt(45)
m.sqrt(45)
from math import pi
pi
math
m
mathematics
import math as mathematics
mathematics
mathematics.sqrt(34)
def call_sqrt_from_module(module, n):
return module.sqrt(n)
call_sqrt_from_module(math, 45)
call_sqrt_from_module(mathematics, 45)
import math
m = math
import math as m
import os
from os import listdir
os.getcwd()
cwd = os.getcwd()
os.listdir() # if no paramters are passed then it will return files in curret directory
os.listdir("/tmp/") # if folder path is passed, it will return list files in the folder
os.mkdir("test")
os.listdir()
os.path.isfile("test")
os.path.isdir("test")
os.path.getsize("module1-day1.ipynb") # gives size in bytes
os.path.getsize(cwd)
os.path.getsize("test/")
os.listdir()
os.path.join("home", "vikrant", "training")
os.path.isfile("/home/vikrant/trainings/") # not a good style!
path = os.path.join("/", "home", "vikrant" "trainings")
os.path.isfile(path)
os.mkdir(os.path.join(cwd, "test", "test2"))
os.listdir()
data = {"x":1, "y":2, "z":3}
for k, v in data.items():
print(k, v)
for path, dirs, files in os.walk(cwd):
for f in files:
print(f)
for path, dirs, files in os.walk(cwd):
for f in files:
print(os.path.join(path, f))
os.getcwd() # wheree the interpreter is running
folderpath = "/home/vikrant/trainings/2020"
os.listdir(folderpath)
folderpath = "/home/vikrant/trainings/2020"
for item in os.listdir(folderpath):
path = os.path.join(folderpath, item)
print(path)
We want to find files with extension ".ipynb" from given folder
def findfiles(folderpath, ext):
files = []
for item in os.listdir(folderpath):
if os.path.isfile(item) and item.endswith(ext):
files.append(os.path.join(folderpath, item))
return files
findfiles(cwd, ".ipynb")
findfiles(cwd, ".html")
def findfiles(folderpath, ext):
files = []
for item in os.listdir(folderpath):
path = os.path.join(folderpath, item)
if os.path.isfile(path) and item.endswith(ext):
files.append(path)
return files
findfiles("/home/vikrant/Downloads/", ".pdf")
def findfiles(folderpath, ext):
files_ext = []
for path, dirs, files in os.walk(folderpath):
for file in files:
if file.endswith(ext):
files_ext.append(os.path.join(path, file))
return files_ext
findfiles(os.getcwd(), ".ipynb")
pdffiles = findfiles("/home/vikrant/Downloads/", ".pdf")
max([1, 2,3, 4, 5])
max(["one", "two", "three", "four", "six"], )
pdffiles[0]
os.path.getsize(pdffiles[0])
def findfiles(folderpath, ext):
files_ext = []
for path, dirs, files in os.walk(folderpath):
for file in files:
if file.endswith(ext):
files_ext.append(os.path.join(path, file))
return files_ext
``` path dirs files
x, y = 2, 3
for x, y in zip([1, 2, 3, 4], ['a','b','c','d']):
print(x, y)
for k, v in data.items():
print(k, v)
list(zip([1, 2, 3, 4], ['a','b','c','d']))
list(data.items())
data
list(os.walk("test"))
Problem
can you modify above function to compute real size of a folder findsize(folder)
def findfiles(folderpath, ext):
files_ext = []
for path, dirs, files in os.walk(folderpath):
for file in files:
if file.endswith(ext):
files_ext.append(os.path.join(path, file))
return files_ext
def findsize(folderpath):
size = 0
for path, dirs, files in os.walk(folderpath):
for file in files:
filepath = os.path.join(path, file)
size += os.path.getsize(filepath)
for dir_ in dirs:
dirpath = os.path.join(path, dir_)
size += os.path.getsize(dirpath)
return size/1024/1024 # make it in MB
findsize(".")
max(pdffiles, key=os.path.getsize)
os.path.getsize("Handbook_Final_WebVersion.pdf")
os.path.getsize('/home/vikrant/Downloads/Handbook_Final_WebVersion.pdf')
%%file stats.py
def mean(nums):
return sum(nums)/len(nums)
def median(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([3, 4, 5, 6,7 ])
%%file hello.py
import sys
def hello(name):
print("Hello", name + "!")
def welcome(name):
hello(name)
print("Welcome to python programming!")
name = sys.argv[1]
welcome(name)
!python3 hello.py arcesium
%%file hello1.py
import sys
def hello(name):
print("Hello", name + "!")
def welcome(name):
hello(name)
print("Welcome to python programming!")
print(sys.argv)
name = sys.argv[1]
welcome(name)
!python3 hello1.py arcesium
!python3 hello1.py arcesium dhfjdsh kfjdhsf ds ksdhfkdsj kjhkjhg kjhkjhgkjf kjhgkjhf kjhgkjfh kjfhgkjfh
!python3 hello1.py arcesium 34 3434 456
%%file add.py
import sys
def add(x, y):
return x+y
a = int(sys.argv[1])
b = int(sys.argv[2])
print(add(a, b)) ## unless print statement is there nothing will be printed
!python3 add.py 34 56
!python3 add.py sdfdsf dfdsfds
import add
%%file add1.py
import sys
def add(x, y):
return x+y
print(__name__) ## dunder name
a = int(sys.argv[1])
b = int(sys.argv[2])
print(add(a, b))
!python3 add1.py 34 45
import add1
When you call a script from commandline the variable __name__ will have values of "__main__". When you import same module, it takes value as module name
%%file add2.py
import sys
def add(x, y):
return x+y
print(__name__) ## dunder name
if __name__ == "__main__":
a = int(sys.argv[1])
b = int(sys.argv[2])
print(add(a, b))
!python3 add2.py 34 56
import add2
add2.add(2, 3)
if the script is not in current directory one should give complete path for commandline execution. but for module you must add the folderepath in a sytem variable called PYHTHONPATH