Python Virtual Training For Arcesium - Module I - Day 5

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

Simple

  1. Write a function product which finds product of all elements from a list.
        >>> product([3, 2, 4])
        24
  1. Write a function factorial to find factorial of a number.
    >>> factorial(5)
    120
  1. Write a function findlens which finds lengths of every word from a given list of words.
        >>> findlens(["one", "two", "three"])
        [3, 3, 5]
  1. Write a function find_words_of_len to find words of given length from given list.:
    >>> find_words_of_len(words, 3)
    ['one', 'two', 'six']

Medium level Problems

  1. Write a function unique which will remove duplicates from a list.:
    >>> unique([1, 1, 2, 3, 1, 2, 3, 2, 4])
    [1, 2, 3, 4]
  1. List of urls is given. Some urls are from same domain, some are from different. Find unique domain names used in the urls.:
     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']
  1. 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.

  2. Write a function rearramge_max to rearrange digits of an integer so as to make maximum integer from it.

        >>> rearramge_max(1312)
        3211
In [1]:
x = 10
def bar():
    x = 20

bar() 
print(x)
10
In [2]:
x = 10
def bar():
    print(x) 

bar()
10
In [4]:
x = 10
def bar():
    x = x + 1 
    
bar()
---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
<ipython-input-4-7e58434f1f14> in <module>
      3     x = x + 1
      4 
----> 5 bar()

<ipython-input-4-7e58434f1f14> in bar()
      1 x = 10
      2 def bar():
----> 3     x = x + 1
      4 
      5 bar()

UnboundLocalError: local variable 'x' referenced before assignment
In [6]:
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)
[1, 1, 1, 1]

Solutions

In [16]:
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
In [15]:
product([2, 3, 4, 5, 6]) #
Out[15]:
720.0
In [17]:
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
 
    
In [18]:
factorial(6)
Out[18]:
720
In [19]:
factorial(5)
Out[19]:
120
In [20]:
help(factorial)
Help on function factorial in module __main__:

factorial(n)
    Here I can put help for this function.
    factorial(5) = 5*4*3*2*1

In [21]:
[1, 2, 3, 4]
Out[21]:
[1, 2, 3, 4]
In [22]:
list("hello")
Out[22]:
['h', 'e', 'l', 'l', 'o']
In [23]:
list((1, 2, 3, 4))
Out[23]:
[1, 2, 3, 4]
In [24]:
range(5) # this will create number from 0 to 4
Out[24]:
range(0, 5)
In [26]:
for i in range(5):
    print(i, end=",")
0,1,2,3,4,
In [27]:
for n in nums: # nums has to be a collection
    print(n)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-27-82091c285d1f> in <module>
----> 1 for n in nums: # nums has to be a collection
      2     print(n)

NameError: name 'nums' is not defined
In [28]:
for i in [2, 3, 4, 5]:
    print(i, end=",")
2,3,4,5,
In [29]:
nums = range(5)
In [31]:
nums # it is a collection of integers from 0 to 4
Out[31]:
range(0, 5)
In [32]:
for i in nums:
    print(i, end=",")
0,1,2,3,4,
In [33]:
for i in range(5):
    print(i, end=",")
0,1,2,3,4,
In [34]:
for i in range(1, 6):
    print(i, end=",")
1,2,3,4,5,
In [35]:
for i in range(1, 21, 2):
    print(i, end=",")
1,3,5,7,9,11,13,15,17,19,
In [38]:
def factorial(n):
    return product(range(1, n+1)) # make use of already written code
In [39]:
factorial(5)
Out[39]:
120.0
In [52]:
def findlens(words):
    """
    returns a list of length of every word
    """
    
    lens = []
    
    for word in words:
        print(lens)
        lens.append(len(word))
    
    return lens
In [53]:
words = "one two three four five six".split()
In [54]:
words
Out[54]:
['one', 'two', 'three', 'four', 'five', 'six']
In [55]:
findlens(words)
[]
[3]
[3, 3]
[3, 3, 5]
[3, 3, 5, 4]
[3, 3, 5, 4, 4]
Out[55]:
[3, 3, 5, 4, 4, 3]
In [56]:
def findlens(words):
    """
    returns a list of length of every word
    """
    
    lens = []
    
    for word in words:
        #print(lens)
        lens.append(len(word))
    
    return lens
In [57]:
findlens(words)
Out[57]:
[3, 3, 5, 4, 4, 3]
In [58]:
words
Out[58]:
['one', 'two', 'three', 'four', 'five', 'six']
In [59]:
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
In [60]:
find_words_of_len(words, 3)
Out[60]:
['one', 'two', 'six']
In [61]:
find_words_of_len(words, 4)
Out[61]:
['four', 'five']
In [62]:
find_words_of_len(words, 2)
Out[62]:
[]
In [63]:
def find_words_of_len(words, l):
    desired_words = []
    
    for word in words:
        if len(word)==l:
            desired_words.append(word)
            
    return desired_words
In [64]:
find_words_of_len(words, 3)
Out[64]:
['one', 'two', 'six']
### 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
In [73]:
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
In [72]:
unique([1, 1, 1, 2, 2, 2, 3, 1, 2])
Out[72]:
[1, 2, 3]
In [74]:
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']
In [76]:
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
In [77]:
find_domains(urls)
Out[77]:
['www.abrakadabra.com',
 'www.abrakadabra.com',
 'www.abra.com',
 'www.dabra.com',
 'www.abra.com',
 'www.abra.com',
 'www.abrakadabra.com',
 'www.dabra.com',
 'www.dabra.com']
In [78]:
unique(find_domains(urls))
Out[78]:
['www.abrakadabra.com', 'www.abra.com', 'www.dabra.com']
In [79]:
def find_unique_domains(urls):
    domains = find_domains(urls)
    return unique(domains)
In [80]:
find_unique_domains(urls)
Out[80]:
['www.abrakadabra.com', 'www.abra.com', 'www.dabra.com']
In [87]:
def min2(x, y):
    if x < y:
        return x
    else:
        return y
    
In [86]:
def min3(x, y, z):
    if (x<y) and (x<z):
        return x
    elif y < z:
        return y
    else:
        return z
In [ ]:
    
3        67       5
 \      /        /
  \    /        /
   \  /        /
   min?       /
    3        /
     \      /
      \    /
       min?
       3
In [83]:
def min3(x, y, z):
    m = min2(x, y)
    return min2(m, z)
In [84]:
min3(34, 2, 32)
Out[84]:
2
In [85]:
def min3(a, b, c):
    return min2(min2(a,b), c)

Modules and Scripts

We made functions to abstract few statements together ... now what if we can collect bunch of functions together somewhere?

In [88]:
abs(45)
Out[88]:
45
In [89]:
len("hello")
Out[89]:
5
In [94]:
import os # here os is module name
In [91]:
os
Out[91]:
<module 'os' from '/home/vikrant/anaconda3/lib/python3.8/os.py'>
In [92]:
os.getcwd() # get me current working directory
Out[92]:
'/home/vikrant/trainings/2020/arcesium_finop_batch3'
In [93]:
os.getcwd # this is just a function
Out[93]:
<function posix.getcwd()>
In [98]:
os.path # this is a module inside os module
Out[98]:
<module 'posixpath' from '/home/vikrant/anaconda3/lib/python3.8/posixpath.py'>
In [97]:
os.getcwd # this is function in os module
Out[97]:
<function posix.getcwd()>
In [99]:
import math
In [101]:
math.pi # a data/variable
Out[101]:
3.141592653589793
In [102]:
import math
In [103]:
import math as m 
In [109]:
del m
In [106]:
math
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-106-a984292e3e46> in <module>
----> 1 math

NameError: name 'math' is not defined
In [110]:
m
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-110-9a40b379906c> in <module>
----> 1 m

NameError: name 'm' is not defined
In [111]:
import math as m
In [112]:
math
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-112-a984292e3e46> in <module>
----> 1 math

NameError: name 'math' is not defined
In [113]:
m
Out[113]:
<module 'math' from '/home/vikrant/anaconda3/lib/python3.8/lib-dynload/math.cpython-38-x86_64-linux-gnu.so'>
In [114]:
m.pi
Out[114]:
3.141592653589793
In [115]:
m.sin
Out[115]:
<function math.sin(x, /)>
In [116]:
m.sin(0)
Out[116]:
0.0
In [117]:
from math import sqrt # this is going to import only sqrt from math
In [118]:
math
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-118-a984292e3e46> in <module>
----> 1 math

NameError: name 'math' is not defined
In [119]:
sqrt
Out[119]:
<function math.sqrt(x, /)>
In [120]:
sqrt(2)
Out[120]:
1.4142135623730951
In [121]:
import os
In [125]:
import pandas as pd # it will not work for you as of now
In [123]:
os.getcwd()
Out[123]:
'/home/vikrant/trainings/2020/arcesium_finop_batch3'
In [134]:
pythondir = "/home/vikrant/anaconda/python3"
python_exe = "/".join([pythondir, "bin", "python"]) # platform dependent
In [127]:
python_exe
Out[127]:
'/home/vikrant/anaconda/python3/bin/python'
In [129]:
"\\".join([pythondir, "bin","python"])
Out[129]:
'/home/vikrant/anaconda/python3\\bin\\python'
In [130]:
os.path.sep
Out[130]:
'/'
In [132]:
os.path.sep # which is platform specific
Out[132]:
'/'
In [133]:
os.path.join(pythondir, "bin","python") # remember this is function/not string method 
Out[133]:
'/home/vikrant/anaconda/python3/bin/python'
In [135]:
 
Out[135]:
True
In [136]:
module1_file_path = "/home/vikrant/trainings/2020/arcesium_finop_batch3/module1-day1.ipynb"
In [137]:
os.path.isfile(module1_file_path)
Out[137]:
True
In [138]:
os.path.isdir(module1_file_path) 
Out[138]:
False
In [139]:
os.path.exists("/home/vikrant/trainings/2020/arcesium_finop_batch3/module1-day1.ipynb")
Out[139]:
True
In [140]:
os.path.exists("/home/vikrant/trainings/2020/arcesium_finop_batch3/module1-day6.ipynb")
Out[140]:
False

absolute path and relative path?

In [142]:
os.path.exists("module1-day1.ipynb") # have given just filename? where will it check? it will check in current folder
Out[142]:
True
In [143]:
os.path.exists("Makefile")
Out[143]:
True
In [144]:
os.path.isfile("module1-day1.ipynb")
Out[144]:
True
In [146]:
os.path.isdir("module1-day1.ipynb")
Out[146]:
False
In [148]:
os.path.getsize("module1-day1.ipynb") # size in bytes
Out[148]:
79292
In [150]:
os.path.getsize(os.getcwd()) # this does not actually compute size of cintents of directory
Out[150]:
4096
In [151]:
os.listdir() # if you don't pass any arguments, it will list contents of current working directory
Out[151]:
['module1-day1_2.ipynb',
 'module-day5.html',
 'module1-day5.html',
 'push',
 'module1-day5.ipynb',
 'Untitled.html',
 'module1-day1.html',
 'module1-day1.ipynb',
 'module1-day2.ipynb',
 'module1-day3.html',
 'module1-day2.html',
 'module1-day3.ipynb',
 'module1-day4.html',
 'Makefile',
 '.ipynb_checkpoints',
 'module1-day1_2.html',
 'module1-day4.ipynb']
In [152]:
os.listdir("/tmp/")
Out[152]:
['.org.chromium.Chromium.6GPDuz',
 'timeshift',
 '.org.chromium.Chromium.PAPHF1',
 '.X11-unix',
 'net-export',
 'Temp-19e8c0b6-caf8-49eb-ba05-b23f10768176',
 'bluejeans-v2 Crashes',
 'systemd-private-43e9b732ffd44dfeb38e03a132845e38-systemd-resolved.service-oDfHuH',
 '.org.chromium.Chromium.07e6Gd',
 'systemd-private-43e9b732ffd44dfeb38e03a132845e38-rtkit-daemon.service-vqoz3U',
 'ssh-SnsgL83LCR9i',
 'Temp-4ed6d016-d3f1-4fdc-811a-ac3f96fba48e',
 'systemd-private-43e9b732ffd44dfeb38e03a132845e38-ModemManager.service-llIKY1',
 'systemd-private-43e9b732ffd44dfeb38e03a132845e38-systemd-timesyncd.service-CcE4xn',
 '.XIM-unix',
 'systemd-private-43e9b732ffd44dfeb38e03a132845e38-colord.service-bTKzc0',
 '.Test-unix',
 '.font-unix',
 '.org.chromium.Chromium.FtVWvw',
 '.org.chromium.Chromium.Eno32s',
 'systemd-private-43e9b732ffd44dfeb38e03a132845e38-bolt.service-GiPj0m',
 '.X0-lock',
 '.ICE-unix',
 'mintUpdate',
 'config-err-jipeyO']
In [155]:
def ls(dirpath=None):
    if dirpath == None:
        dirpath = os.getcwd()
    contents = os.listdir(dirpath)
    for f in contents:
        print(f)
In [156]:
ls()
module1-day1_2.ipynb
module-day5.html
module1-day5.html
push
module1-day5.ipynb
Untitled.html
module1-day1.html
module1-day1.ipynb
module1-day2.ipynb
module1-day3.html
module1-day2.html
module1-day3.ipynb
module1-day4.html
Makefile
.ipynb_checkpoints
module1-day1_2.html
module1-day4.ipynb
In [157]:
ls("/tmp")
.org.chromium.Chromium.6GPDuz
timeshift
.org.chromium.Chromium.PAPHF1
.X11-unix
net-export
Temp-19e8c0b6-caf8-49eb-ba05-b23f10768176
bluejeans-v2 Crashes
systemd-private-43e9b732ffd44dfeb38e03a132845e38-systemd-resolved.service-oDfHuH
.org.chromium.Chromium.07e6Gd
systemd-private-43e9b732ffd44dfeb38e03a132845e38-rtkit-daemon.service-vqoz3U
ssh-SnsgL83LCR9i
Temp-4ed6d016-d3f1-4fdc-811a-ac3f96fba48e
systemd-private-43e9b732ffd44dfeb38e03a132845e38-ModemManager.service-llIKY1
systemd-private-43e9b732ffd44dfeb38e03a132845e38-systemd-timesyncd.service-CcE4xn
.XIM-unix
systemd-private-43e9b732ffd44dfeb38e03a132845e38-colord.service-bTKzc0
.Test-unix
.font-unix
.org.chromium.Chromium.FtVWvw
.org.chromium.Chromium.Eno32s
systemd-private-43e9b732ffd44dfeb38e03a132845e38-bolt.service-GiPj0m
.X0-lock
.ICE-unix
mintUpdate
config-err-jipeyO
In [159]:
ls("/home/vikrant/trainings/2020/arcesium_finop_batch3")
module1-day1_2.ipynb
module-day5.html
module1-day5.html
push
module1-day5.ipynb
Untitled.html
module1-day1.html
module1-day1.ipynb
module1-day2.ipynb
module1-day3.html
module1-day2.html
module1-day3.ipynb
module1-day4.html
Makefile
.ipynb_checkpoints
module1-day1_2.html
module1-day4.ipynb
In [162]:
ls("/home/vikrant/trainings/")
2018
nakul
day5.org
hello.py
day5.org~
__pycache__
2020
trainingvenv
2019
2017
indexdata.xlsx
In [164]:
os.walk("/home/vikrant/trainings/arcesium_finop_batch3")
Out[164]:
<generator object walk at 0x7f90dcee9c80>
In [171]:
records = [
    ("TATA", 230, 5.5),
    ("REL", 334, 5.2),
    ("INF", 3243, 3),
    ("HCL", 3231, 1)
]
In [172]:
records[0]
Out[172]:
('TATA', 230, 5.5)
In [173]:
records[1]
Out[173]:
('REL', 334, 5.2)
In [174]:
records[3]
Out[174]:
('HCL', 3231, 1)
In [167]:
for item in records:
    print(item)
    
('TATA', 230, 5.5)
('REL', 334, 5.2)
('INF', 3243, 3)
('HCL', 3231, 1)
In [178]:
for item in records:
    print(item[0], item[1], item[2])
    
TATA 230 5.5
REL 334 5.2
INF 3243 3
HCL 3231 1
In [168]:
x, y, z = (1, 2, 3)
In [169]:
print(x, y, z)
1 2 3
In [175]:
for name, value, gain in records:
    print(name)
TATA
REL
INF
HCL
In [176]:
for name, value, gain in records:
    print(name, value)
TATA 230
REL 334
INF 3243
HCL 3231
In [177]:
for name, value, gain in records:
    print(value)
230
334
3243
3231
In [183]:
for path, dirs, files in os.walk("/home/vikrant/trainings/2020/arcesium_finop_batch3/"):
    #print(path, dirs, files)
    for f in files:
        print(f)
module1-day1_2.ipynb
module-day5.html
module1-day5.html
push
module1-day5.ipynb
Untitled.html
module1-day1.html
module1-day1.ipynb
module1-day2.ipynb
module1-day3.html
module1-day2.html
module1-day3.ipynb
module1-day4.html
Makefile
module1-day1_2.html
module1-day4.ipynb
module1-day4-checkpoint.ipynb
module1-day2-checkpoint.html
module1-day3-checkpoint.html
module1-day2-checkpoint.ipynb
module1-day3-checkpoint.ipynb
module1-day1_2-checkpoint.ipynb
module1-day5-checkpoint.ipynb
module1-day1-checkpoint.ipynb
In [184]:
for path, dirs, files in os.walk("/home/vikrant/trainings/2020/arcesium_finop_batch3/"):
    #print(path, dirs, files)
    for d in dirs:
        print(d)
.ipynb_checkpoints
In [188]:
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
            
In [185]:
os.path.getsize("Makefile")
Out[185]:
617
In [186]:
os.path.getsize("module1-day4-checkpoint.ipynb")
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-186-d749906cee95> in <module>
----> 1 os.path.getsize("module1-day4-checkpoint.ipynb")

~/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: 'module1-day4-checkpoint.ipynb'
In [187]:
os.path.getsize(".ipynb_checkpoints/module1-day4-checkpoint.ipynb")
Out[187]:
72
In [190]:
compute_size(os.getcwd())/1024/1024
Out[190]:
4.341192245483398

Problem

  • Write a function 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
  • Write a function findfiles which finds all files in given directory with a given extension
>>> findfiles("/var/", ".log")
['/var/log/bootstrap.log',
'/var/log/dpkg.log',
.
.]
In [192]:
ls()
module1-day1_2.ipynb
module-day5.html
module1-day5.html
push
module1-day5.ipynb
Untitled.html
module1-day1.html
module1-day1.ipynb
module1-day2.ipynb
module1-day3.html
module1-day2.html
module1-day3.ipynb
module1-day4.html
Makefile
.ipynb_checkpoints
module1-day1_2.html
module1-day4.ipynb
In [193]:
ls("c:\\Users\\user\\Desktop") # this will not work ..becuase jupyter hub is running on a remote linux machine
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-193-711d4351d117> in <module>
----> 1 ls("c:\\Users\\user\\Desktop") # this will not work ..becuase jupyter hub is running on a remote linux machine

<ipython-input-155-fe7478342e8b> in ls(dirpath)
      2     if dirpath == None:
      3         dirpath = os.getcwd()
----> 4     contents = os.listdir(dirpath)
      5     for f in contents:
      6         print(f)

FileNotFoundError: [Errno 2] No such file or directory: 'c:\\Users\\user\\Desktop'
In [194]:
os.getcwd()
Out[194]:
'/home/vikrant/trainings/2020/arcesium_finop_batch3'
In [196]:
for i in range(5):
    print(i, end=",")
0,1,2,3,4,
In [198]:
for path, dirs, files in os.walk(os.getcwd()):
    for f in files:
        print(f)
module1-day1_2.ipynb
module-day5.html
module1-day5.html
push
module1-day5.ipynb
Untitled.html
module1-day1.html
module1-day1.ipynb
module1-day2.ipynb
module1-day3.html
module1-day2.html
module1-day3.ipynb
module1-day4.html
Makefile
module1-day1_2.html
module1-day4.ipynb
module1-day4-checkpoint.ipynb
module1-day2-checkpoint.html
module1-day3-checkpoint.html
module1-day2-checkpoint.ipynb
module1-day3-checkpoint.ipynb
module1-day1_2-checkpoint.ipynb
module1-day5-checkpoint.ipynb
module1-day1-checkpoint.ipynb
In [199]:
longlistdir()
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-199-d78dd912867b> in <module>
----> 1 longlistdir()

NameError: name 'longlistdir' is not defined
In [204]:
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)
In [205]:
longlistdir()
f module1-day1_2.ipynb
f module-day5.html
f module1-day5.html
f push
f module1-day5.ipynb
f Untitled.html
f module1-day1.html
f module1-day1.ipynb
f module1-day2.ipynb
f module1-day3.html
f module1-day2.html
f module1-day3.ipynb
f module1-day4.html
f Makefile
d .ipynb_checkpoints
f module1-day1_2.html
f module1-day4.ipynb
In [206]:
longlistdir("/tmp/")
f .org.chromium.Chromium.6GPDuz
d timeshift
f .org.chromium.Chromium.PAPHF1
d .X11-unix
d net-export
d Temp-19e8c0b6-caf8-49eb-ba05-b23f10768176
d bluejeans-v2 Crashes
d systemd-private-43e9b732ffd44dfeb38e03a132845e38-systemd-resolved.service-oDfHuH
d .org.chromium.Chromium.07e6Gd
d systemd-private-43e9b732ffd44dfeb38e03a132845e38-rtkit-daemon.service-vqoz3U
d ssh-SnsgL83LCR9i
d Temp-4ed6d016-d3f1-4fdc-811a-ac3f96fba48e
d systemd-private-43e9b732ffd44dfeb38e03a132845e38-ModemManager.service-llIKY1
d systemd-private-43e9b732ffd44dfeb38e03a132845e38-systemd-timesyncd.service-CcE4xn
d .XIM-unix
d systemd-private-43e9b732ffd44dfeb38e03a132845e38-colord.service-bTKzc0
d .Test-unix
d .font-unix
f .org.chromium.Chromium.FtVWvw
f .org.chromium.Chromium.Eno32s
d systemd-private-43e9b732ffd44dfeb38e03a132845e38-bolt.service-GiPj0m
f .X0-lock
d .ICE-unix
d mintUpdate
f config-err-jipeyO
In [207]:
longlistdir("/home/vikrant/trainings")
d 2018
d nakul
f day5.org
f hello.py
f day5.org~
d __pycache__
d 2020
d trainingvenv
d 2019
d 2017
f indexdata.xlsx
In [208]:
os.path.isfile("/tmp/xyz.txt")
Out[208]:
False
In [209]:
os.path.exists("/tmp/xyz.txt")
Out[209]:
False
In [210]:
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))
In [211]:
findfiles(".", ".ipynb")
./module1-day1_2.ipynb
./module1-day5.ipynb
./module1-day1.ipynb
./module1-day2.ipynb
./module1-day3.ipynb
./module1-day4.ipynb
./.ipynb_checkpoints/module1-day4-checkpoint.ipynb
./.ipynb_checkpoints/module1-day2-checkpoint.ipynb
./.ipynb_checkpoints/module1-day3-checkpoint.ipynb
./.ipynb_checkpoints/module1-day1_2-checkpoint.ipynb
./.ipynb_checkpoints/module1-day5-checkpoint.ipynb
./.ipynb_checkpoints/module1-day1-checkpoint.ipynb
In [215]:
findfiles("/home/vikrant/Downloads/", ".txt")
/home/vikrant/Downloads/bills.txt
/home/vikrant/Downloads/permission-denied (1).txt
/home/vikrant/Downloads/batch1-users.txt
/home/vikrant/Downloads/sha256sum.txt
/home/vikrant/Downloads/JH How Children Learn.txt
/home/vikrant/Downloads/Pippi Longstocking by Lindgren Astrid (z-lib.org).txt
/home/vikrant/Downloads/permission-denied.txt
/home/vikrant/Downloads/Untitled Document.txt
/home/vikrant/Downloads/permission-denied (2).txt
/home/vikrant/Downloads/batch2-users.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/loop.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/unscheduled.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/gradientEffects.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/block.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/Delayfile.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/gradient.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/scheduled.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/TraversalDetails.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/station.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/Maintenance.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/error.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/BlockDirectionInInterval.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/loco-typeDetails.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/debug.txt
/home/vikrant/Downloads/LKO-CNB(NEW)/GradientDebug.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/loop.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/unscheduled.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/gradientEffects.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/block.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/Oldunscheduled.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/loopFiles.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/Delayfile.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/gradient.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/scheduled.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/TraversalDetails.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/station.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/loopList.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/Maintenance.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/error.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/TDwithAllowances.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/debug.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/TDwithoutAllowance.txt
/home/vikrant/Downloads/simples solutions kudawale - Google Maps_files/NDLS-MMCT/GradientDebug.txt
/home/vikrant/Downloads/paraview/ParaView-5.8.1-MPI-Linux-Python3.7-64bit/lib/python3.7/site-packages/matplotlib/mpl-data/fonts/pdfcorefonts/readme.txt
/home/vikrant/Downloads/paraview/ParaView-5.8.1-MPI-Linux-Python3.7-64bit/lib/python3.7/site-packages/matplotlib/mpl-data/sample_data/README.txt
/home/vikrant/Downloads/paraview/ParaView-5.8.1-MPI-Linux-Python3.7-64bit/share/paraview-5.8/examples/README.txt
/home/vikrant/Downloads/paraview/ParaView-5.8.1-MPI-Linux-Python3.7-64bit/share/paraview-5.8/materials/README.txt
/home/vikrant/Downloads/tabula/tabula/AUTHORS.txt
/home/vikrant/Downloads/tabula/tabula/README.txt
/home/vikrant/Downloads/tabula/tabula/LICENSE.txt

Writing your own modules

In [216]:
ls()
module1-day1_2.ipynb
module-day5.html
module1-day5.html
push
module1-day5.ipynb
Untitled.html
sample.py
module1-day1.html
module1-day1.ipynb
module1-day2.ipynb
module1-day3.html
module1-day2.html
module1-day3.ipynb
module1-day4.html
Makefile
.ipynb_checkpoints
module1-day1_2.html
module1-day4.ipynb
In [217]:
import sample
In [218]:
sample.hello()
Hello

In jupyter if you want to write files you start with %%file filename, next line start writing contents of file

In [221]:
%%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]
Overwriting stats.py
In [222]:
import stats
In [223]:
stats.mean([1, 2, 3, 4, 5])
Out[223]:
3.0

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.

Writing scripts

In [226]:
%%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)
Overwriting hello.py

IF YOU WANT TO run a system command from jupyter, this how you run it. you start with !command

In [227]:
!python3 hello.py vikrant
['hello.py', 'vikrant']
Welcome vikrant
In [228]:
!python3 hello.py arcesium
['hello.py', 'arcesium']
Welcome arcesium
In [229]:
%%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))
Writing mean.py
In [230]:
!python mean.py 1 2 3 4 5
Traceback (most recent call last):
  File "mean.py", line 8, in <module>
    print(mean(numbers))
  File "mean.py", line 4, in mean
    return sum(nums)/len(nums)
TypeError: unsupported operand type(s) for +: 'int' and 'str'
In [231]:
%%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))
Overwriting mean.py
In [232]:
!python mean.py 1 2 3 4 5
3.0
In [235]:
%%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))
Overwriting add.py
In [236]:
!python add.py 4 6
In [237]:
2 +3
Out[237]:
5
In [238]:
%%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)))
Overwriting add.py
In [239]:
!python3 add.py 5 6
11
In [246]:
%%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)))
Overwriting addall.py
In [245]:
!python3 addall.py 1 2 3 4 5 6
21
In [ ]: