Dec 07-11, 2020 Vikrant Patil
These notes are available online at http://notes.pipal.in/2020/arcesium_finop_batch3/module2-day2.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 module2-day2.ipynb for today's session.
Problems
>>> listpy(os.getcwd())
add.py
add1.py
add2.py
hello.py
records = [("2018-11-11 24:04","11803","16602"),
("2018-11-11 24:09","11782","16568"),
("2018-11-11 24:14","11741","16524"),
("2018-11-11 24:19","11756","16543"),
("2018-11-11 24:24","11741","16538"),
("2018-11-11 24:28","11722","16558"),
("2018-11-11 24:34","11716","16457"),
("2018-11-11 24:39","11724","16430"),
("2018-11-11 24:44","11723","16572"),
("2018-11-11 24:49","11739","16611"),
("2018-11-11 24:54","11740","16501"),
("2018-11-11 24:58","11743","16568"),
("2018-11-12 01:04","11754","16626")]
The timestamp given above has been misprinted, instead of 11th Nov 24:04 , it should be 12 Nov 00:04! Write a function to correct the record. use list comprehension to do this.
Bonus Problem
"<" --------- less than
"<="--------- less than or equal to
">" --------- greater than
">="--------- greater than or equal to
"<>"--------- not equal to
Sample run is shown below:
>>> a = [10,20,30,40,50,40,40,50]
>>> COUNTIFS(a, "<40")
3
>>> COUNTIFS(a, ">=40")
5
>>> COUNTIFS(a, "40")
>>> COUNTIFS(a, "<>40")
5
names = ["akshat", "aman","avinash", "deekash", "shivam", "jyoti", "kunal"]
emails = []
domain = "arcesium.com"
for name in names:
emails.append("@".join([name,domain]))
emails
["@".join([name, domain]) for name in names]
newlist = []
for item in oldlist:
newlist.append(do_some_operation(item))
newlist = [do_some_operation(item) for item in oldlist]
names_with_a = []
for name in names:
if name.startswith("a"):
names_with_a.append(name)
names_with_a
names_with_a = [name for name in names if name.startswith("a")]
names_with_a
import os
def print_list(items):
for item in items:
print(item)
def listpy(dirpath):
"""
filters files with extension .py
"""
files = os.listdir(dirpath)
pyfiles = [file for file in files if file.endswith(".py")]
print_list(pyfiles)
listpy(".")
listpy(os.getcwd())
sum([2, 3, 4, 5])
sum([n for n in range(1, 1000) if n%7==0 or n%11==0])
[print(file) for file in os.listdir() if file.endswith(".py")]
x = print("hello")
print(x)
text = "abrakadabra"
[c for i, c in enumerate(text) if i%2==1]
[c for i, c in enumerate(text) if i%2==0]
def transform(c, index):
if index%2==0:
return c.lower()
else:
return c.upper()
[transform(c, i) for i, c in enumerate(text)]
def transform(c, index):
if index%2==0:
return c.lower()
else:
return c.upper()
"".join([transform(c, i) for i, c in enumerate(text)])
"".join([c.lower() if i%2==0 else c.upper() for i,c in enumerate(text)])
x = "even" if len(names)%2==0 else "odd" # one liner if else
x
len(names)
8%2 # remainder
8/2 # division
records = [("2018-11-11 23:58","11803","16602"),
("2018-11-11 24:04","11803","16602"),
("2018-11-11 24:09","11782","16568"),
("2018-11-11 24:14","11741","16524"),
("2018-11-11 24:19","11756","16543"),
("2018-11-11 24:24","11741","16538"),
("2018-11-11 24:28","11722","16558"),
("2018-11-11 24:34","11716","16457"),
("2018-11-11 24:39","11724","16430"),
("2018-11-11 24:44","11723","16572"),
("2018-11-11 24:49","11739","16611"),
("2018-11-11 24:54","11740","16501"),
("2018-11-11 24:58","11743","16568"),
("2018-11-12 01:04","11754","16626")]
def increament(strnum):
return str(int(strnum)+1)
def correct_time(date):
dt, t = date.split()
if "24:" in t: # make note of :, if you don't give it them we will confuse with min and hrs
y , m , d_ = dt.split("-")
d_ = increament(d_)
return "-".join([y, m, d_]) + " " + t.replace("24:","00:")
else:
return date
[(correct_time(dt), x, y) for dt, x, y in records]
"2018-11-11 24:09".split()
def correct_hour(h):
hi = int(h)
actual_hour = hi - 24
return str(actual_hour).zfill(2) #1 -> 01
def correct_time(date):
dt, t = date.split()
h, min_ = t.split(":")
if int(h)>=24:
y , m , d_ = dt.split("-")
d_ = increament(d_)
return "-".join([y, m, d_]) + " " + correct_hour(h) + ":" + min_
else:
return date
rec = records + [("2018-11-11 25:04","11754","16626")]
print_list(rec)
print("*"*10)
[(correct_time(dt), x, y) for dt, x, y in rec]
def factors(n):
return [f for f in range(1, n+1) if n%f==0]
factors(5)
factors(10)
factors(7)
factors(11)
def is_prime(p):
return factors(p)==[1,p]
is_prime(49)
is_prime(47)
def primes(n):
"""
generate prime numbers less than n
"""
return [i for i in range(1, n) if is_prime(i)]
def print_list1(items):
for item in items:
print(item, end=" ")
print_list1(primes(100))
import this # just prints phillosphy of python
%%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!
filename = "zen.txt" # relative path..
with open(filename) as f:
print(f.readline())
print(f.readline())
# when you come out of with block ... file is automatially closed.
with open(filename) as f:
for line in f:
print(line)
with open(filename) as f:
for line in f:
print(line, end="")
with open(filename) as f:
for i , line in enumerate(f, start=1):
print(i, line, end="")
for index, p in enumerate(primes(50)):
print(index, p)
p_ = primes(20)
p_
for p in p_:
print(p)
for i, p in enumerate(p_):
print(i, p)
for i, c in enumerate("some text"):
print(i, c)
f = open(filename) # no data is loaded as of now
f.readline() # here data loading in memory starts
f.close() # how many files you can open , is pre-defined. so close it whenever work is done.
with open(filename) as f: # with block will make sure file is closed after the block is over
f.readline()
with open(filename) as file:
filetext = file.read() # you can read comple file with read method
filetext
with open(filename) as f:
lines = f.readlines()
lines
!ls # these are system command
!cat add.py
!wc zen.txt
!wc /home/vikrant/Downloads/eMARC\ sample\ data.csv
!head /home/vikrant/Downloads/eMARC\ sample\ data.csv
!head zen.txt
%%file head.py
import sys
def head(filename):
with open(filename) as f:
for i in range(5):
print(f.readline(), end="")
filename = sys.argv[1]
head(filename)
!python head.py zen.txt
import head
import math
math.pi
!python head.py zen.txt lksjad dkfjdsj
%%file head1.py
import sys
def head(filename):
with open(filename) as f:
for i in range(5):
print(f.readline(), end="")
print(__name__)
if __name__ == "__main__":
filename = sys.argv[1]
head(filename)
!python head1.py zen.txt
import head1 # when you import file as a module, the variable __name__ -> modulename
%%file head2.py
import sys
def head(filename):
with open(filename) as f:
for i in range(5):
print(f.readline(), end="")
print(__name__)
if __name__ == "__main__":
filename = sys.argv[1]
head(filename)
import head2
head2.head("zen.txt")
%%file head3.py
import sys
def head(filename):
with open(filename) as f:
for i in range(5):
print(f.readline(), end="")
import head3
head3.head("zen.txt")
!python head3.py zen.txt kfdshf kjhfds
!head -n 4 zen.txt
%%file head5.py
import sys
def head(filename, n):
with open(filename) as f:
for i in range(n):
print(f.readline(), end="")
if __name__ == "__main__":
n = int(sys.argv[1])
filename = sys.argv[2]
head(filename, n)
!python head5.py 9 zen.txt
with open("data.txt", "w") as f:
for word in "hello create some words out of this!".split():
f.write(word)
f.write("\n")
!python3 head5.py 7 data.txt
with open("data.txt", "w") as f: # w mode , overwrites
for word in "let me change the contents!".split():
f.write(word)
f.write("\n")
!cat data.txt
with open("data.txt", "a") as f: # a mode (append) , appends data
for word in "this is additional data!".split():
f.write(word)
f.write("\n")
!cat data.txt
problems
cat.py which mimics uniz command cat. Essentially cat.py should print contents of file to standard output (print statement).!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!
wc.py which mimics unix command wc. It should print line count, word count and char count and filename.python3 wc.py zen.txt
21 144 857 zen.txt
csvparse which will load data from file and create a list as shown below.file%% data.csv
symbol,day,price
IBM,Monday,111.23
IBM,Tuesday,112.54
APPLE,Monday,200.45
APPLE,Tuesday,205.54
```
csvparse("data.csv") [['IBM', 'Monday', 111.23], ['IBM','Tuesday',112.54], ['APPLE','Monday',200.45] ['APPLE','Tuesday',205.54]]