Login to Lab using your credentials. There is a notebook with name 2-4.ipynb already created for you. Open that and use it for today’s training.
Shut down all previous notebooks.
Command line options
Using typer module (third party module)
to install any thrid party library use pip module
pip install typer
python -m pip install typer
python3 -m pip install typer (if you have python3 and python2 installed then use this)
!python3 -m pip install typer
Defaulting to user installation because normal site-packages is not writeable
Collecting typer
Downloading typer-0.12.3-py3-none-any.whl.metadata (15 kB)
Requirement already satisfied: click>=8.0.0 in /opt/tljh/user/lib/python3.10/site-packages (from typer) (8.1.7)
Requirement already satisfied: typing-extensions>=3.7.4.3 in /opt/tljh/user/lib/python3.10/site-packages (from typer) (4.12.2)
Collecting shellingham>=1.3.0 (from typer)
Downloading shellingham-1.5.4-py2.py3-none-any.whl.metadata (3.5 kB)
Collecting rich>=10.11.0 (from typer)
Downloading rich-13.7.1-py3-none-any.whl.metadata (18 kB)
Collecting markdown-it-py>=2.2.0 (from rich>=10.11.0->typer)
Downloading markdown_it_py-3.0.0-py3-none-any.whl.metadata (6.9 kB)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/tljh/user/lib/python3.10/site-packages (from rich>=10.11.0->typer) (2.18.0)
Collecting mdurl~=0.1 (from markdown-it-py>=2.2.0->rich>=10.11.0->typer)
Downloading mdurl-0.1.2-py3-none-any.whl.metadata (1.6 kB)
Downloading typer-0.12.3-py3-none-any.whl (47 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 47.2/47.2 kB 3.5 MB/s eta 0:00:00
Downloading rich-13.7.1-py3-none-any.whl (240 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 240.7/240.7 kB 5.0 MB/s eta 0:00:00a 0:00:01
Downloading shellingham-1.5.4-py2.py3-none-any.whl (9.8 kB)
Downloading markdown_it_py-3.0.0-py3-none-any.whl (87 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 87.5/87.5 kB 18.6 MB/s eta 0:00:00
Downloading mdurl-0.1.2-py3-none-any.whl (10.0 kB)
Installing collected packages: shellingham, mdurl, markdown-it-py, rich, typer
WARNING: The script markdown-it is installed in '/home/jupyter-pipal/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
WARNING: The script typer is installed in '/home/jupyter-pipal/.local/bin' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed markdown-it-py-3.0.0 mdurl-0.1.2 rich-13.7.1 shellingham-1.5.4 typer-0.12.3
[notice] A new release of pip is available: 24.0 -> 24.1.2
[notice] To update, run: pip install --upgrade pip
import typer
---------------------------------------------------------------------------ModuleNotFoundError Traceback (most recent call last)
Cell In[2], line 1----> 1importtyperModuleNotFoundError: No module named 'typer'
import typer
%%file head.pyimport sysdef head(filename:str, n:int):withopen(filename) as f:for i inrange(n):print(f.readline(), end="")n =int(sys.argv[1])filename = sys.argv[2]head(filename, n)
Overwriting head.py
!python head.py 3 zen.txt
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
!python head.py --help
Traceback (most recent call last):
File "/opt/arcesium-python-2024-june/head.py", line 8, in <module>
n = int(sys.argv[1])
ValueError: invalid literal for int() with base 10: '--help'
%%file head1.pyimport typerdef head(filename:str, n:int):withopen(filename) as f:for i inrange(n):print(f.readline(), end="")if__name__=="__main__":# this if block will make sure than this code is exucuted only when it is used as python program typer.run(head)
Overwriting head1.py
!python head1.py --help
Usage: head1.py [OPTIONS] FILENAME N
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ * filename TEXT [default: None] [required] │
│ * n INTEGER [default: None] [required] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
!python head1.py zen.txt 5
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
!head --help
Usage: head [OPTION]... [FILE]...
Print the first 10 lines of each FILE to standard output.
With more than one FILE, precede each with a header giving the file name.
With no FILE, or when FILE is -, read standard input.
Mandatory arguments to long options are mandatory for short options too.
-c, --bytes=[-]NUM print the first NUM bytes of each file;
with the leading '-', print all but the last
NUM bytes of each file
-n, --lines=[-]NUM print the first NUM lines instead of the first 10;
with the leading '-', print all but the last
NUM lines of each file
-q, --quiet, --silent never print headers giving file names
-v, --verbose always print headers giving file names
-z, --zero-terminated line delimiter is NUL, not newline
--help display this help and exit
--version output version information and exit
NUM may have a multiplier suffix:
b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,
GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y, R, Q.
Binary prefixes can be used, too: KiB=K, MiB=M, and so on.
GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation <https://www.gnu.org/software/coreutils/head>
or available locally via: info '(coreutils) head invocation'
%%file head2.pyimport typerfrom typing_extensions import Annotateddef head(filename:str, n:Annotated[int, typer.Option(help="Number of lines to be printed")]=10):withopen(filename) as f:for i inrange(n):print(f.readline(), end="")if__name__=="__main__":# this if block will make sure than this code is exucuted only when it is used as python program typer.run(head)
Overwriting head2.py
!python head2.py --help
Usage: head2.py [OPTIONS] FILENAME
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ * filename TEXT [default: None] [required] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --n INTEGER Number of lines to be printed [default: 10] │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
!python head2.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.
!python head2.py --n 4 zen.txt
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
problem
Implement unix command grep which will look for given keyword into a file and print those lines which have that keyword. Add option –v for your program which reverses the beahviour, it means it will print all those lines which do not contain the keyword
!grep better zen.txt
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.
Now is better than never.
Although never is often better than *right* now.
!grep -v better zen.txt
The Zen of Python, by Tim Peters
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.
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!
Implement unix command grep which will look for given keyword into a file and print those lines which have that keyword. Add option –v for your program which reverses the beahviour, it means it will print all those lines which do not contain the keyword
%%file grep.pyimport typerfrom typing_extensions import Annotateddef grep(keyword:str, filename:str):withopen(filename) as f:for line in f:if keyword in line:print(line, end="")if__name__=="__main__": typer.run(grep)
Overwriting grep.py
!python grep.py --help
Usage: grep.py [OPTIONS] KEYWORD FILENAME
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ * keyword TEXT [default: None] [required] │
│ * filename TEXT [default: None] [required] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
!python grep.py better zen.txt
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.
Now is better than never.
Although never is often better than *right* now.
%%file grep.pyimport typerfrom typing_extensions import Annotateddef grep(keyword:str, filename:str, v:bool=False):withopen(filename) as f:for line in f:if keyword in line:print(line, end="")if__name__=="__main__": typer.run(grep)
Overwriting grep.py
!python grep.py --help
Usage: grep.py [OPTIONS] KEYWORD FILENAME
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ * keyword TEXT [default: None] [required] │
│ * filename TEXT [default: None] [required] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --v --no-v [default: no-v] │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
%%file grep.pyimport typerfrom typing_extensions import Annotateddef grep(keyword:str, filename:str, v:Annotated[bool, typer.Option(help="print line which do not have this keyword")]=False):withopen(filename) as f:for line in f:if keyword in line andnot v:print(line, end="")elif keyword notin line and v:print(line, end="")if__name__=="__main__": typer.run(grep)
Overwriting grep.py
!python grep.py --help
Usage: grep.py [OPTIONS] KEYWORD FILENAME
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ * keyword TEXT [default: None] [required] │
│ * filename TEXT [default: None] [required] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --v --no-v print line which do not have this keyword │
│ [default: no-v] │
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
!python grep.py --v better zen.txt
The Zen of Python, by Tim Peters
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.
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!
!python grep.py better zen.txt
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.
Now is better than never.
Although never is often better than *right* now.
---------------------------------------------------------------------------ValueError Traceback (most recent call last)
Cell In[69], line 1----> 1for k,v in stock:
2print(k)
ValueError: too many values to unpack (expected 2)
%%file words.txtoneone twoone two threeone two three fourone two three four fiveone two three four five sixone two three seven sixone two eight seven sixone nine eight seven sixten nine eight seven sixten nine eight seventen nine eightten nineten
Overwriting words.txt
d = {}d['one'] =1
d
{'one': 1}
d['one'] =2
d
{'one': 2}
def get_words(filename):withopen(filename) as f:return f.read().split()
words = get_words("words.txt")
unique_words= [][unique_words.append(word) for word in words if word notin unique_words]
Cell In[94], line 1 print({word}:{counts})
^
SyntaxError: invalid syntax
print(f"{word}:{counts}")
def word_count(words): count={}for word in words:if word in word_count: count[word]+=1else: count[word]=1return countdef word_count_(words): count={}for word in words: count[word] = count.get(word, 0) +1return countdef word_count__(words): unique =set(words) # one pass from all the words count = {}for w in unique: count[w] = words.count(w) # for every count there will be one pass return countdef pretty_print(word_count): for word, counts in word_count.items():print(f"{word}:{counts}")
def mean(nums):returnsum(nums)/len(nums)def get_prices(symbol, data):return [price for name, day, price in data if name==symbol] def get_weekly_average(symbol, data): p = get_prices(symbol, data)return mean(p)def get_symbols(data):returnset([item[0] for item in data])
get_weekly_average("IBM", indexdata)
128.62503125576717
{symbol:get_weekly_average(symbol, indexdata) for symbol in get_symbols(indexdata)}