Sep 20-24, 2021 Vikrant Patil
These notes are available online at https://notes.pipal.in/2021/arcesium_finop_batch1/
© Pipal Academy LLP
We will be using jupyter hub from https://lab.pipal.in for this training.
create a notebook with name module2-day2
import os
files_folders = os.listdir()
path = "/etc/ssh"
files_in_ssh = os.listdir(path) # i gave argument to function folder path for which I want to find the listing
files_in_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']
files_folders
['index.html', 'problems-day4.html', 'module2-day2.ipynb', 'helloworld.py~', 'mymath.py~', 'capitalize.py', 'module1-day5.html', 'problems-day2.html', 'mymodule.py', 'push', 'module2-day2.html', 'hello.py', 'module1-day5.ipynb', 'square.py', '__pycache__', 'problems-day3.ipynb', 'Untitled.html', 'problems-day3.html', 'index.ipynb', 'mymathmodule.py', 'problems-day4.ipynb', 'module1-day1.html', 'module1-day1.ipynb', 'module1-day2.ipynb', 'module1-assignment.ipynb', 'just_another.py', 'module2-day1.ipynb', 'module1-day3.html', 'module1-day2.html', 'mymath.py', 'module1-day3.ipynb', 'backup', 'module1-assignment.html', 'module1-day4.html', 'helloworld.py', 'Makefile', 'module2-day1.html', '.ipynb_checkpoints', 'mymodule1.py', 'module1-day4.ipynb', 'problems-day2.ipynb']
def biggest_file_in_given_path(path):
"""this will look
"""
files_folders = os.listdir(path)
maxsize = 0
maxfile = None
for file in files_folders:
completepath = os.path.join(path, file) # os.path.sep.join([path, file])
if os.path.isfile(completepath) and os.path.getsize(completepath) > maxsize:
maxfile = file
maxsize = os.path.getsize(completepath)
return maxfile
biggest_file_in_given_path(os.getcwd())
'module1-day2.html'
path = os.getcwd()
def get_size_file(file):
completepath = os.path.join(path, file)
return os.path.getsize(completepath)
max(os.listdir(path), key=get_size_file)
'module1-day2.html'
index = [(1, 2, 3),
(2, 3, 5),
(2, 3, 4)]
def column1(r):
return r[1]
max(index, key=column1) # first row where max from column of index 1 occures
(2, 3, 5)
column1(index)##??? no we don't call column1
(2, 3, 4)
max(index, key=column1) #
(2, 3, 4)
os.path.getsize("index.html")
569778
os.path.getsize("ssh_config")## how will it know that this file ssh_config is from other folder
--------------------------------------------------------------------------- FileNotFoundError Traceback (most recent call last) <ipython-input-22-f0e5713b0a84> in <module> ----> 1 os.path.getsize("ssh_config")## how will it know that this file ssh_config is from other folder ~/anaconda3/lib/python3.8/genericpath.py 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 51 52 FileNotFoundError: [Errno 2] No such file or directory: 'ssh_config'
completepath = os.path.join("/etc/ssh", "ssh_config")
os.path.getsize(completepath)
1603
def some_proceesing(file):
return file.split(".")[-1]
newlist = []
oldlist = files_folders
for item in oldlist:
newlist.append(some_proceesing(item))
newlist = [item for item in oldlist] # this is nothing but copy of old list
newlist = [some_proceesing(item) for item in oldlist] # this is equivalent to above for loop
newlist
['html', 'html', 'ipynb', 'py~', 'py~', 'py', 'html', 'html', 'py', 'push', 'html', 'py', 'ipynb', 'py', '__pycache__', 'ipynb', 'html', 'html', 'ipynb', 'py', 'ipynb', 'html', 'ipynb', 'ipynb', 'ipynb', 'py', 'ipynb', 'html', 'html', 'py', 'ipynb', 'backup', 'html', 'html', 'py', 'Makefile', 'html', 'ipynb_checkpoints', 'py', 'ipynb', 'ipynb']
def some_proceesing(file):
return file.split(".")[-1]
newlist = []
oldlist = files_folders
for item in oldlist:
if os.path.isfile(item):
newlist.append(some_proceesing(item))
[some_proceesing(item) for item in oldlist if os.path.isfile(item)]
['html', 'html', 'ipynb', 'py~', 'py~', 'py', 'html', 'html', 'py', 'push', 'html', 'py', 'ipynb', 'py', 'ipynb', 'html', 'html', 'ipynb', 'py', 'ipynb', 'html', 'ipynb', 'ipynb', 'ipynb', 'py', 'ipynb', 'html', 'html', 'py', 'ipynb', 'html', 'html', 'py', 'Makefile', 'html', 'py', 'ipynb', 'ipynb']
problems
listpy which workds same as os.listdir. but it returns only files with .py extentiondef listpy(path=None):# default argument
if path==None:
path = os.getcwd()
return [file for file in os.listdir(path) if file.endswith(".py")]
listpy() # that means path argument is None
['capitalize.py', 'mymodule.py', 'hello.py', 'square.py', 'mymathmodule.py', 'just_another.py', 'mymath.py', 'helloworld.py', 'mymodule1.py']
def listpy():
return [file for file in os.listdir() if file.endswith(".py")]
listpy()
['capitalize.py', 'mymodule.py', 'hello.py', 'square.py', 'mymathmodule.py', 'just_another.py', 'mymath.py', 'helloworld.py', 'mymodule1.py']
listpy("/home/vikrant/trainings/2021/pccoe-python-ml/")
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-42-e501923b7254> in <module> ----> 1 listpy("/home/vikrant/trainings/2021/pccoe-python-ml/") TypeError: listpy() takes 0 positional arguments but 1 was given
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']
def listpy(path=None):# default argument
if path==None:
path = os.getcwd()
return [file for file in os.listdir(path) if file.endswith(".py")]
def listpy(path): # this will work fine but path is compulsory argument
return [file for file in os.listdir(path) if file.endswith(".py")]
listpy()# will not without any argument
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-53-1410cdfc723c> in <module> ----> 1 listpy()# will not without any argument TypeError: listpy() missing 1 required positional argument: 'path'
listpy(".")
['capitalize.py', 'mymodule.py', 'hello.py', 'square.py', 'mymathmodule.py', 'just_another.py', 'mymath.py', 'helloworld.py', 'mymodule1.py']
def offset(values, offset=5): # default argument take defualt value defined here if the user does not pass the value
return values + offset
offset(89) # offset is not passed, so it will take default value of 5
94
offset(89, 3)
92
offset(89, offset=3)
92
os.path.isdir("/home/vikrant/.profile")
False
os.path.isdir("/home/vikrant/.emacs.d")
True
def listfolders(path):
def myisdir(name):
completepath = os.path.join(path, name)
return os.path.isdir(completepath)
#[item for item in os.listdir(path) if os.path.isdir(os.path.join(path,item))]
return [item for item in os.listdir(path) if myisdir(item)]
listfolders(".")# . is current working directory
['__pycache__', 'backup', '.ipynb_checkpoints']
listfolders("..") # parent folder is ..
['arcesium_finop_batch2', 'pccoe-python-ml', 'arcesium_finop_batch1']
listfolders("/home/vikrant/trainings/")
['2018', 'nakul', '__pycache__', '2020', 'trainingvenv', '2021', '2019', '2017']
os.listdir(".") # it will have files and folders
['index.html', 'problems-day4.html', 'module2-day2.ipynb', 'helloworld.py~', 'mymath.py~', 'capitalize.py', 'module1-day5.html', 'problems-day2.html', 'mymodule.py', 'push', 'module2-day2.html', 'hello.py', 'module1-day5.ipynb', 'square.py', '__pycache__', 'problems-day3.ipynb', 'Untitled.html', 'problems-day3.html', 'index.ipynb', 'mymathmodule.py', 'problems-day4.ipynb', 'module1-day1.html', 'module1-day1.ipynb', 'module1-day2.ipynb', 'module1-assignment.ipynb', 'just_another.py', 'module2-day1.ipynb', 'module1-day3.html', 'module1-day2.html', 'mymath.py', 'module1-day3.ipynb', 'backup', 'module1-assignment.html', 'module1-day4.html', 'helloworld.py', 'Makefile', 'module2-day1.html', '.ipynb_checkpoints', 'mymodule1.py', 'module1-day4.ipynb', 'problems-day2.ipynb']
[n for n in range(10) if n%2==0]
[0, 2, 4, 6, 8]
os.path.join() # this is equiavalent to os.path.sep.join()
cond = False
if cond:
print("we will come here only if cond is True")
if 0:
print("this is false")
elif 1:
print("this is true")
this is true
5%2
1
4%2
0
4%2==0
True
problems
is_primesum([item**2 for item in range(1, 11)])
385
[sum(item) for item in range(1, 11)]
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-80-b63b03783f59> in <module> ----> 1 [sum(item) for item in range(1, 11)] <ipython-input-80-b63b03783f59> in <listcomp>(.0) ----> 1 [sum(item) for item in range(1, 11)] TypeError: 'int' object is not iterable
x = [item*item for item in range(1, 11)]
sum(x)
385
[i for i in range(1, 10)]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
nums = [i for i in range(1, 1000) if i%7==0 or i%11==0]
sum(nums)
110110
## sum is built in function it will take list or tuple as an argument
list(range(5))
[0, 1, 2, 3, 4]
"""
10 factors
1 -> 10%1 0 divisible 1
2 -> 10%2 0 divisible 2
3 -> 10%3 1 not divisible
4 -> 10%4 2 not divisible
5 -> 10%5 0 divisible 5
.
.
10-> 10%10 0 divisible 10
"""
def factors(n):
return [i for i in range(1, n+1) if n%i==0]
factors(10)
[1, 2, 5, 10]
l = []
if not l: # if the list empty then this will result tru
print("Hello")
Hello
if l:
print("Will come here only if l has some data in it")
def is_prime(p):
def factors(y):
for n in range(1, y+1):
if y%n==0:
return n # this will end in first iteartion
def factors(y):
f = []
for n in range(1, y+1):
if y%n==0:
f.append(n)
return f
factors(5)
[1, 5]
import math
a = 5
factor_ = [ i for i in range(2, int(math.sqrt(a))) if a%i==0]
if not factor_:
print("prime")
else:
print("composite")
#list(range(2, int(math.sqrt(5))))
prime
if []:
print("Should not come here!")
def factors(n):
return [i for i in range(1, n+1) if n%i==0]
def isprime(z):
for n in range(2, z):
if z%n!=0:
continue
else:
return False
return True
os.path.isfile("index.html")
True
isprime(7)
True
isprime(10)
False
def factors(n):
return [i for i in range(1, n+1) if n%i==0]
def is_prime(n):
return len(factors(n))==2 #this is not return len
factors(5)
[1, 5]
factors(13)
[1, 13]
is_prime(13)
True
is_prime(10)
False
def primes(n):
return [i for i in range(n) if is_prime(i)]
primes(50)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
Reading text files
%%file zen.txt
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!
Writing zen.txt
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!
with open("zen.txt") as z:
text = z.read() # read complete file
print(text)
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!
z = open("zen.txt")
z.read()
z.close() # if you don't use with this close has to be called!
with open("zen.txt") as f:
for line in f:
print(line) # this print has added its own \n and line already has one \n
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!
with open("zen.txt") as f:
for line in f:
print(line, end="")
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!
for i in [1, 2, 3, 4]: # list
print(i, end=",")
1,2,3,4,
for item in (1, 2, 3, 5):
print(item, end=",")
1,2,3,5,
for c in "kdhsjfdsf":
print(c, end=",")
k,d,h,s,j,f,d,s,f,
with open("zen.txt") as f:
for line in f: # f is some kind object on which for loop can be applied.but it is not a list
print(line, end="")
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!
with open("zen.txt") as f:
print(f[0])# f is not a list
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-142-36c8c021f9cc> in <module> 1 with open("zen.txt") as f: ----> 2 print(f[0])# f is not a list TypeError: '_io.TextIOWrapper' object is not subscriptable
with open("zen.txt") as f:
print(len(f))# can not find number of line
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-143-f864315e3bc1> in <module> 1 with open("zen.txt") as f: ----> 2 print(len(f))# can not find number of line TypeError: object of type '_io.TextIOWrapper' has no len()
with open("zen.txt") as f: # f is some kind of iterator..can be used only once to go over the file
lines = [line for line in f]
lines2 = [line for line in f] # this is empty just like reversed, zip, enumerate!
lines
['The Zen of Python, by Tim Peters\n', '\n', 'Beautiful is better than ugly.\n', 'Explicit is better than implicit.\n', 'Simple is better than complex.\n', 'Complex is better than complicated.\n', 'Flat is better than nested.\n', 'Sparse is better than dense.\n', 'Readability counts.\n', "Special cases aren't special enough to break the rules.\n", 'Although practicality beats purity.\n', 'Errors should never pass silently.\n', 'Unless explicitly silenced.\n', 'In the face of ambiguity, refuse the temptation to guess.\n', 'There should be one-- and preferably only one --obvious way to do it.\n', "Although that way may not be obvious at first unless you're Dutch.\n", 'Now is better than never.\n', 'Although never is often better than *right* now.\n', "If the implementation is hard to explain, it's a bad idea.\n", 'If the implementation is easy to explain, it may be a good idea.\n', "Namespaces are one honking great idea -- let's do more of those!\n"]
lines2
[]
with open("zen.txt") as f:
p = f # creating an alias
lines2 = [line for line in f]
lines1 = [line for line in p] # will be empty
lines2
['The Zen of Python, by Tim Peters\n', '\n', 'Beautiful is better than ugly.\n', 'Explicit is better than implicit.\n', 'Simple is better than complex.\n', 'Complex is better than complicated.\n', 'Flat is better than nested.\n', 'Sparse is better than dense.\n', 'Readability counts.\n', "Special cases aren't special enough to break the rules.\n", 'Although practicality beats purity.\n', 'Errors should never pass silently.\n', 'Unless explicitly silenced.\n', 'In the face of ambiguity, refuse the temptation to guess.\n', 'There should be one-- and preferably only one --obvious way to do it.\n', "Although that way may not be obvious at first unless you're Dutch.\n", 'Now is better than never.\n', 'Although never is often better than *right* now.\n', "If the implementation is hard to explain, it's a bad idea.\n", 'If the implementation is easy to explain, it may be a good idea.\n', "Namespaces are one honking great idea -- let's do more of those!\n"]
lines1
[]
f = open("zen.txt")
p = open("zen.txt")
[line for line in f]
['The Zen of Python, by Tim Peters\n', '\n', 'Beautiful is better than ugly.\n', 'Explicit is better than implicit.\n', 'Simple is better than complex.\n', 'Complex is better than complicated.\n', 'Flat is better than nested.\n', 'Sparse is better than dense.\n', 'Readability counts.\n', "Special cases aren't special enough to break the rules.\n", 'Although practicality beats purity.\n', 'Errors should never pass silently.\n', 'Unless explicitly silenced.\n', 'In the face of ambiguity, refuse the temptation to guess.\n', 'There should be one-- and preferably only one --obvious way to do it.\n', "Although that way may not be obvious at first unless you're Dutch.\n", 'Now is better than never.\n', 'Although never is often better than *right* now.\n', "If the implementation is hard to explain, it's a bad idea.\n", 'If the implementation is easy to explain, it may be a good idea.\n', "Namespaces are one honking great idea -- let's do more of those!\n"]
[line for line in p]
['The Zen of Python, by Tim Peters\n', '\n', 'Beautiful is better than ugly.\n', 'Explicit is better than implicit.\n', 'Simple is better than complex.\n', 'Complex is better than complicated.\n', 'Flat is better than nested.\n', 'Sparse is better than dense.\n', 'Readability counts.\n', "Special cases aren't special enough to break the rules.\n", 'Although practicality beats purity.\n', 'Errors should never pass silently.\n', 'Unless explicitly silenced.\n', 'In the face of ambiguity, refuse the temptation to guess.\n', 'There should be one-- and preferably only one --obvious way to do it.\n', "Although that way may not be obvious at first unless you're Dutch.\n", 'Now is better than never.\n', 'Although never is often better than *right* now.\n', "If the implementation is hard to explain, it's a bad idea.\n", 'If the implementation is easy to explain, it may be a good idea.\n', "Namespaces are one honking great idea -- let's do more of those!\n"]
x = [1, 2, 3, 4]
y = x # this is not creating new object...x and y are same!
for i in reversed(range(5)):
print(i)
4 3 2 1 0
%%file cat.py
import sys
def cat(filename):
with open(filename) as f:
print(f.read())
# outside the if block you put only defination
if __name__ == "__main__": # in both you will have to give two _ at start and at _ end
# you put execution part here and it comes at end of .py file
cat(sys.argv[1])
Overwriting cat.py
import cat
!python3 cat.py zen.txt
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!
%%file numbers.csv
1,2,3,4
1,2,3,4
1,2,3,4
Writing numbers.csv
!python3 cat.py numbers.csv
1,2,3,4 1,2,3,4 1,2,3,4