%%file hello.py
import typer
def hello(name):
print(f"Hello {name}, how are you doing?")
typer.run(hello)Overwriting hello.py
typer is third party library which makes command line arguments very easy
%%file hello.py
import typer
def hello(name):
print(f"Hello {name}, how are you doing?")
typer.run(hello)Overwriting hello.py
Usage: hello.py [OPTIONS] NAME
Arguments:
NAME [required]
Options:
--help Show this message and exit.
%%file sqaure.py
import typer
def square(x:float): # this expects x as float
return x**2
def print_square(x:float):
print(square(x))
typer.run(print_square)Overwriting sqaure.py
Usage: sqaure.py [OPTIONS] X
Arguments:
X [required]
Options:
--help Show this message and exit.
%%file square1.py
import sys
def square(x:float): # this expects x as float
return x**2
def print_square(x:float):
print(square(x))
a = float(sys.argv[1])
print_square(a)Writing square1.py
Traceback (most recent call last):
File "/home/jupyter-vikrant/arcesium-python-2024/square1.py", line 9, in <module>
a = float(sys.argv[1])
ValueError: could not convert string to float: '--help'
%%file square2.py
import sys
def square(x:float): # this expects x as float
return x**2
def print_square(x:float):
print(square(x))
def check_args():
if sys.argv[1] == '--help':
print("Usage: python square2.py N")
else:
return float(sys.argv[1])
a = check_args()
if a is not None:
print_square(a)Overwriting square2.py
%%file head.py
import typer
def head(filename, n:int):
with open(filename) as f:
for i in range(n):
print(f.readline(), end="")
typer.run(head)Overwriting head.py
Usage: head.py [OPTIONS] FILENAME N
Arguments:
FILENAME [required]
N [required]
Options:
--help Show this message and exit.
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.
%%file head1.py
import typer
def head(filename, n:int=10):
with open(filename) as f:
for i in range(n):
print(f.readline(), end="")
typer.run(head)Overwriting head1.py
Usage: head1.py [OPTIONS] FILENAME
Arguments:
FILENAME [required]
Options:
--n INTEGER [default: 10]
--help Show this message and exit.
Usage: head1.py [OPTIONS] FILENAME
Try 'head1.py --help' for help.
Error: Missing argument 'FILENAME'.
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
%%file head2.py
import typer
def head(filename, lines:int=10):
with open(filename) as f:
for i in range(lines):
print(f.readline(), end="")
typer.run(head)Overwriting head2.py
Usage: head2.py [OPTIONS] FILENAME
Arguments:
FILENAME [required]
Options:
--lines INTEGER [default: 10]
--help Show this message and exit.
problem
Write a function triangle to print a text triangle using any char that is passed
*
* *
* * *
* * * *
* * * * *
@
@ @
@ @ @
@ @ @ @
@ @ @ @ @
@ @ @ @ @ @
@ @ @ @ @ @ @
@ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
@ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @ @
*
* * size 3
* * * size 5
* * * * size 7
{'name': 'Vikrant',
'email': 'vikrant@pipalacademy',
'address': 'Dapoli',
'mobile': 1111111111}
person.get("company", "Arcesium") # if the key is not present in the dictionary then don't throw error return the default value 'Arcesium'
{'name': 'Vikrant',
'email': 'vikrant@pipalacademy',
'address': 'Dapoli',
'mobile': 1111111111}
{'name': 'Vikrant',
'email': 'vikrant@pipalacademy',
'address': 'Dapoli',
'mobile': 1111111111,
'company': 'Pipal Academy'}
person.get("company", "Arcesium") # return arcesium only if key and value pair for company is missing in the dictionary!'Pipal Academy'
name Vikrant
email vikrant@pipalacademy
address Dapoli
mobile 1111111111
company Pipal Academy
Vikrant
vikrant@pipalacademy
Dapoli
1111111111
Pipal Academy
name Vikrant
email vikrant@pipalacademy
address Dapoli
mobile 1111111111
company Pipal Academy
%%file words.txt
one
one two
one two three
one two three four
one two three four five
one two three four six
one two three six seven
one two six seven eight
one six seven eight nine
six seven eight nine ten
seven eight nine ten
eight nine ten
nine ten
tenWriting words.txt
get_wordsword_frequency
['one',
'one',
'two',
'one',
'two',
'three',
'one',
'two',
'three',
'four',
'one',
'two',
'three',
'four',
'five',
'one',
'two',
'three',
'four',
'six',
'one',
'two',
'three',
'six',
'seven',
'one',
'two',
'six',
'seven',
'eight',
'one',
'six',
'seven',
'eight',
'nine',
'six',
'seven',
'eight',
'nine',
'ten',
'seven',
'eight',
'nine',
'ten',
'eight',
'nine',
'ten',
'nine',
'ten',
'ten']
{'eight': 5,
'ten': 5,
'five': 1,
'three': 5,
'two': 7,
'seven': 5,
'six': 5,
'one': 9,
'nine': 5,
'four': 3}
def word_frequency(words):
uniquewords = set(words) # set will convert the list into unique items
freq = {}
for word in uniquewords:
f = words.count(word)
freq[word] = f # here word is key and f is value
return freq
# dictionary comprehension
def word_frequency(words):
uniquewords = set(words) # set will convert the list into unique items
return {word: words.count(word) for word in uniquewords}
def word_frequency(words):
uwords=[]
freq={}
for word in words:
if word in uwords:
pass
else:
uwords.append(word)
freq[word] = words.count(word)
return freq
def word_frequency(words):
freq={}
for word in words:
freq[word] = freq.get(word, 0) + 1
return freq
def word_frequency(words):# XXX : not possible
return {freq.get(word, 0) + 1 for word in words}{'one': 9,
'two': 7,
'three': 5,
'four': 3,
'five': 1,
'six': 5,
'seven': 5,
'eight': 5,
'nine': 5,
'ten': 5}
one 9
two 7
three 5
four 3
five 1
six 5
seven 5
eight 5
nine 5
ten 5
def get_freq(pair):
return pair[1]
for word, count in sorted(freq.items(), key=get_freq):
print(word, count)five 1
four 3
three 5
six 5
seven 5
eight 5
nine 5
ten 5
two 7
one 9
def get_freq(pair):
return pair[1]
for word, count in sorted(freq.items(), key=get_freq, reverse=True):
print(word, count)one 9
two 7
three 5
six 5
seven 5
eight 5
nine 5
ten 5
four 3
five 1
def get_freq(pair):
return pair[1]
for word, count in sorted(freq.items(), key=get_freq, reverse=True):
print(word.rjust(5), count) one 9
two 7
three 5
six 5
seven 5
eight 5
nine 5
ten 5
four 3
five 1
def get_freq(pair):
return pair[1]
for word, count in sorted(freq.items(), key=get_freq, reverse=True):
print(word.rjust(5), count, "*"*count) one 9 *********
two 7 *******
three 5 *****
six 5 *****
seven 5 *****
eight 5 *****
nine 5 *****
ten 5 *****
four 3 ***
five 1 *
dict_items([('one', 9), ('two', 7), ('three', 5), ('four', 3), ('five', 1), ('six', 5), ('seven', 5), ('eight', 5), ('nine', 5), ('ten', 5)])
['one',
'one',
'two',
'one',
'two',
'three',
'one',
'two',
'three',
'four',
'one',
'two',
'three',
'four',
'five',
'one',
'two',
'three',
'four',
'six',
'one',
'two',
'three',
'six',
'seven',
'one',
'two',
'six',
'seven',
'eight',
'one',
'six',
'seven',
'eight',
'nine',
'six',
'seven',
'eight',
'nine',
'ten',
'seven',
'eight',
'nine',
'ten',
'eight',
'nine',
'ten',
'nine',
'ten',
'ten']
{'one': 3,
'two': 3,
'three': 5,
'four': 4,
'five': 4,
'six': 3,
'seven': 5,
'eight': 5,
'nine': 4,
'ten': 3}
one 9
two 7
three 5
four 3
five 1
six 5
seven 5
eight 5
nine 5
ten 5
problem There is indexdata given as a list. Find weekly average for each stock.
indexdata = [('IBM', 'Monday', 111.71436961893693),
('IBM', 'Tuesday', 141.21220022208635),
('IBM', 'Wednesday', 112.40571010053796),
('IBM', 'Thursday', 137.54133351926248),
('IBM', 'Friday', 140.25154281801224),
('MICROSOFT', 'Monday', 235.0403622499107),
('MICROSOFT', 'Tuesday', 225.0206535036475),
('MICROSOFT', 'Wednesday', 216.10342426936444),
('MICROSOFT', 'Thursday', 200.38038844494193),
('MICROSOFT', 'Friday', 235.80850482793264),
('APPLE', 'Monday', 321.49182055844256),
('APPLE', 'Tuesday', 340.63612771662815),
('APPLE', 'Wednesday', 303.9065277507285),
('APPLE', 'Thursday', 338.1350605764038),
('APPLE', 'Friday', 318.3912296144338)]
This is how we can store week averages weekly_average = {"IBM": ibmavaegra, "MICROSOFT": somevalue, "APPLE": another_value}
Homework