problems
repeat which takeks function as argument and , initial value , n an integer. it repeated calling of function n times.>>> repeat(lambda x: 2*x, 1, 5)
32
twice(twice(twice(twice(twice(1)))))
compose which composed two functions>>> f = lambda x:2*x
>>> g = lambda x:x*x
>>> fg = compose(f, g)
>>> fg(2) ## f(g(2))
8
def repeat(func, initial_value, n):
r = initial_value
for i in range(n):
r = func(r)
return r
repeat(lambda x: 1/x, 5, 25)
0.2
def repeat_list(func, initial_value, n):
r = initial_value
results = []
for i in range(n):
r = func(r)
results.append(r)
return results
repeat_list(lambda x:x+1/x, 10, 50)
[10.1, 10.199009900990099, 10.297058634122804, 10.394173745877744, 10.490381489220171, 10.585706907178288, 10.680173909596604, 10.773805343734718, 10.866623059304596, 10.958647968473018, 11.04990010129788, 11.140398657016274, 11.230162051557832, 11.319207961617632, 11.407553365588555, 11.495214581622534, 11.58220730306322, 11.66854663146874, 11.754247107422026, 11.839322739307336, 11.923787030214783, 12.0076530031197, 12.090933224470216, 12.173639826304404, 12.255784527007577, 12.337378650810614, 12.418433146121439, 12.498958602773941, 12.578965268271503, 12.65846306309588, 12.737461595146376, 12.815970173368983, 12.893997820630362, 12.971553285887229, 13.048645055697708, 13.125281365117694, 13.201470208021865, 13.277219346886138, 13.352536322065472, 13.427428460598543, 13.501902884568434, 13.575966519046425, 13.649626099644024, 13.72288817969661, 13.795759137100427, 13.868245180823148, 13.940352357106907, 14.012086555381327, 14.083453513903, 14.154458825136699]
compose = lambda f,g: lambda x:f(g(x))
f = compose(lambda x:2*x, lambda x:x*x)
f(2)
8
def compose(f, g):
def composed(x):
return f(g(x))
return composed
#if else, while, for --> these are not functions or variables can not be passed as function parameters
def add(x, y):
return x+y
lambda x,y: x+y
<function __main__.<lambda>(x, y)>
import math
math.sin(3.14)
0.0015926529164868282
math.log(1)
0.0
math.pi
3.141592653589793
import os
os.getcwd() # get current working directory!
'/home/vikrant/trainings/2021/pccoe-python-ml'
os.listdir() # this returns contents if current working dorectory
['index.html', 'Lecture6-Comprehensions-1.ipynb', 'presenty5.txt', 'Lecture10-Modules-and-Scripts.html', 'sample.html', 'presenty3.txt', 'Welcome.html', 'Lecture9-Functions2.ipynb', 'Lecture10-Modules-and-Scripts.ipynb', 'push', 'Lecture4-Working_With_Data.html', 'presenty8.txt', 'joy.py', 'presenty6.txt', 'Lecture2-Quick_Tour.ipynb', 'Lecture7-Recap.html', 'sample.ipynb', '#presenty4.txt#', '__pycache__', 'Untitled.html', 'Lecture4-Working_With_Data.ipynb', 'Lecture5-Comprehensions.html', 'Modules-and-Scripts.html', 'Lecture1-Fundamental_ideas_of_programming.ipynb.ipynb', 'index.ipynb', 'Lecture8-Functions.ipynb', 'Lecture7-Recap.ipynb', 'presenty5.txt~', 'Lecture9-Functions2.html', 'Lecture6-Comprehensions-1.html', 'test.html', 'Lecture5-Comprehensions.ipynb', 'Lecture6-Recap.html', 'Lecture3-Quick_Tour.ipynb', 'Lecture1-Fundamental_ideas_of_programming.ipynb.html', '#google.txt#', 'Lecture8-Functions.html', 'Makefile', 'Lecture2-Quick_Tour.html', '.ipynb_checkpoints', 'presenty9.txt', 'Lecture3-Quick_Tour.html', 'presenty7.txt']
os.listdir("/etc/ssh/")
['ssh_host_ed25519_key', 'ssh_host_rsa_key.pub', 'ssh_host_ecdsa_key.pub', 'ssh_config.d', 'ssh_host_ecdsa_key', 'sshd_config', 'ssh_host_ed25519_key.pub', 'ssh_host_rsa_key', 'ssh_import_id', 'moduli', 'ssh_config', 'sshd_config.d']
"\n \t"
'\n \t'
"c:\\Program Files\Python3"
'c:\\Program Files\\Python3'
r"c:\Program Files\Python3"
'c:\\Program Files\\Python3'
os.path.sep
'/'
"\\"
'\\'
current_dierctory = os.getcwd()
current_dierctory
'/home/vikrant/trainings/2021/pccoe-python-ml'
"/".join([current_dierctory, "index.html"]) # it will fail on windows
'/home/vikrant/trainings/2021/pccoe-python-ml/index.html'
os.path.sep.join([current_dierctory, "index.html"]) # platform independent code
'/home/vikrant/trainings/2021/pccoe-python-ml/index.html'
os.path.isdir("index.html") # whenever path of a file does not start with / on unix, c: on windows ..
#then the path is called relative path
False
os.path.isdir("ssh_config.d") # relative path which is wrong!
False
os.path.isdir("/etc/ssh/ssh_config.d") # absolute path
True
os.path.exists("ssh_config.d")
False
os.path.exists("/etc/ssh/ssh_config.d")
True
os.path.getsize("index.html") # given size of file in bytes
571989
os.path.getsize(os.getcwd())
4096
def compute_foldersize(folderpath):
contents = os.listdir(folderpath)
total_size = 0
for file in contents:
path = os.path.sep.join([folderpath, file])
if os.path.isdir(path):
total_size += compute_foldersize(path)
else:
total_size += os.path.getsize(path)
return total_size/(1024*1024)
compute_foldersize(os.getcwd())
10.674447420487922
Problems
find_ipynb to find all .ipynb files in given directory (not recursive)
>>> find_ipynb(".")
['index.ipynb','Lecture1.ipynb','Lecture2.ipynb']
find_txt_recursive to find all .txt files recursively!
>>> find_txt_recursively("/home/training/")
[traning/2021/pccoe/presenty1.txt, ...]
For second problem you can write a recursive function or make use os.walk. make use of help(os.walk)os.path.isfile("index.html")
True
Any python file (.py) in current working directory can be imported as python module. If the py file that you want to make use of is not in current working directory, then you set the folder path when the file is, as PYTHONPATH.
import mymodule
mymodule.say_hello("vikrant")
Hello Vikrant
mymodule.welcome("vikrant")
Welcome Vikrant
import mymodule1
mymodule1.data
42
scripts are .py files but it has got more than just definations