hello--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 1 ----> 1 hello NameError: name 'hello' is not defined
Login to Lab using your credentials. There is a notebook with name 3-5.ipynb already created for you. Open that and use it for today’s training.
Shut down all previous notebooks.
You can access live notes from https://live.arcesium-lab.pipal.in
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[1], line 1 ----> 1 hello NameError: name 'hello' is not defined
--------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[2], line 1 ----> 1 A NameError: name 'A' is not defined
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[4], line 1 ----> 1 int("-") ValueError: invalid literal for int() with base 10: '-'
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[5], line 1 ----> 1 int("") ValueError: invalid literal for int() with base 10: ''
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[6], line 1 ----> 1 float("test") ValueError: could not convert string to float: 'test'
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[8], line 1 ----> 1 [int(item) for item in data.split()] Cell In[8], line 1, in <listcomp>(.0) ----> 1 [int(item) for item in data.split()] ValueError: invalid literal for int() with base 10: 'six'
Questions 1. What is datatype of x in function1? 2. what is data in function2? 3. what is function2 doing? 4. what is function1 doing?
for i in range(10):
# here i makes sense!
# in genreal convention is i is loop variable with continiour integer
print(i)0
1
2
3
4
5
6
7
8
9
def column(data2d, col):
"""a function that find given column from 2d matrix
"""
return [row[col] for row in data2d]
def transpose(data2d):
"""A function to return transpose of 2d list data
"""
t = [] # t stands for a a transpose value
columncount = len(data2d)
for c in range(columncount):
t.append(column(data2d, c))
return tto debug python programs we can make use of pdb module
python -m pdb sumofsquares.py 1 2 3 4 5 6
(Pdb)
on this (Pdb) prompt you can make use some basic debugging command. To get list of all possible commands use h (enter)
l -> print the code with line numbers
h -> to see help (only h will show list f available commnads)
b -> to set brakepoint
h b -> help for brekpoint
r -> start the execution in debuger
p var -> print variable/ print result of statement that you pass
c -> continue execution till next breakpoint
s -> step in a function (if there is a function call at current line)
n -> execute only this line and go to next line
q-> exit the debugger
simpleest way is 1. script and requirements.txt and instructions to use the program There is assumption that the users will have python installed on their system. 3. make an executable pyinstaller -F sumofsquares.py this will create an executable with anme sumofsquares in a dist folder. You can pass on this executabele to any user with same platform. 4. Package creation using python package manager
pip install pandas it actually gets this python package from python’s package repository
problem - Write a python program mygrep.py which will find given keyword from given text file and print those line which has the given keyword. Create an executable from your program using pyinstaller.
%%file mygrep.py
import typer
def grep(keyword:str, filename:str):
with open(filename) as f:
for line in f:
if keyword in line:
print(line, end="")
if __name__ == "__main__":
typer.run(grep)Overwriting mygrep.py
Usage: mygrep.py [OPTIONS] KEYWORD FILENAME
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ * keyword TEXT [default: None] [required] │
│ * filename TEXT [default: None] [required] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
257 INFO: PyInstaller: 6.10.0, contrib hooks: 2024.8
257 INFO: Python: 3.10.10 (conda)
258 INFO: Platform: Linux-6.8.0-31-generic-x86_64-with-glibc2.39
258 INFO: Python environment: /opt/tljh/user
258 INFO: wrote /opt/arcesium-python-2024-june/mygrep.spec
259 INFO: Module search paths (PYTHONPATH):
['/opt/tljh/user/lib/python310.zip',
'/opt/tljh/user/lib/python3.10',
'/opt/tljh/user/lib/python3.10/lib-dynload',
'/home/jupyter-pipal/.local/lib/python3.10/site-packages',
'/opt/tljh/user/lib/python3.10/site-packages',
'/opt/sigma/src',
'/opt/arcesium-python-2024-june']
399 INFO: checking Analysis
399 INFO: Building Analysis because Analysis-00.toc is non existent
399 INFO: Running Analysis Analysis-00.toc
399 INFO: Target bytecode optimization level: 0
399 INFO: Initializing module dependency graph...
400 INFO: Caching module graph hooks...
407 INFO: Analyzing base_library.zip ...
1154 INFO: Processing standard module hook 'hook-heapq.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
1405 INFO: Processing standard module hook 'hook-encodings.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
2415 INFO: Processing standard module hook 'hook-pickle.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
3185 INFO: Caching module dependency graph...
3251 INFO: Looking for Python shared library...
3257 INFO: Using Python shared library: /opt/tljh/user/lib/libpython3.10.so.1.0
3257 INFO: Analyzing /opt/arcesium-python-2024-june/mygrep.py
3383 INFO: Processing pre-safe-import-module hook 'hook-typing_extensions.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_safe_import_module'
3383 INFO: SetuptoolsInfo: initializing cached setuptools info...
3845 INFO: Processing standard module hook 'hook-difflib.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
3867 INFO: Processing standard module hook 'hook-platform.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
3926 INFO: Processing pre-safe-import-module hook 'hook-importlib_metadata.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_safe_import_module'
4192 INFO: Processing standard module hook 'hook-pygments.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
5430 INFO: Processing standard module hook 'hook-IPython.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
6620 INFO: Processing standard module hook 'hook-_tkinter.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
6808 INFO: Processing standard module hook 'hook-xml.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
6810 INFO: Processing standard module hook 'hook-xml.dom.domreg.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
6962 INFO: Processing standard module hook 'hook-matplotlib.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
7197 INFO: Processing pre-safe-import-module hook 'hook-packaging.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_safe_import_module'
7203 INFO: Processing standard module hook 'hook-packaging.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
7255 INFO: Processing standard module hook 'hook-numpy.py' from '/opt/tljh/user/lib/python3.10/site-packages/numpy/_pyinstaller'
7273 WARNING: Conda distribution 'numpy', dependency of 'numpy', was not found. If you installed this distribution with pip then you may ignore this warning.
7522 INFO: Processing standard module hook 'hook-sysconfig.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
8056 INFO: Processing standard module hook 'hook-multiprocessing.util.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
8650 INFO: Processing standard module hook 'hook-psutil.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
9192 INFO: Processing pre-safe-import-module hook 'hook-gi.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_safe_import_module'
9333 INFO: Processing standard module hook 'hook-PIL.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
9386 INFO: Processing standard module hook 'hook-PIL.Image.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
9732 INFO: Processing standard module hook 'hook-xml.etree.cElementTree.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
9919 INFO: Processing standard module hook 'hook-pycparser.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
10271 INFO: Processing pre-safe-import-module hook 'hook-distutils.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_safe_import_module'
10271 INFO: Processing pre-find-module-path hook 'hook-distutils.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_find_module_path'
10505 INFO: Processing standard module hook 'hook-distutils.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
10615 INFO: Processing standard module hook 'hook-distutils.util.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
10682 INFO: Processing standard module hook 'hook-setuptools.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
10730 INFO: Processing standard module hook 'hook-pkg_resources.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
12425 INFO: Processing standard module hook 'hook-PIL.ImageFilter.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
12930 INFO: Processing standard module hook 'hook-jinja2.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
13300 INFO: Processing standard module hook 'hook-matplotlib.backends.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
14338 INFO: Processing pre-safe-import-module hook 'hook-six.moves.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_safe_import_module'
15557 INFO: Processing pre-safe-import-module hook 'hook-importlib_resources.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_safe_import_module'
15586 INFO: Processing standard module hook 'hook-certifi.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
16240 INFO: Processing standard module hook 'hook-wcwidth.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
16947 INFO: Processing standard module hook 'hook-jedi.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
17227 INFO: Processing standard module hook 'hook-parso.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
17983 INFO: Processing standard module hook 'hook-sqlite3.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
18329 INFO: Processing standard module hook 'hook-nbformat.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
18418 INFO: Processing standard module hook 'hook-jsonschema.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
19029 INFO: Processing standard module hook 'hook-jsonschema_specifications.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
19078 INFO: Processing pre-safe-import-module hook 'hook-urllib3.packages.six.moves.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_safe_import_module'
19317 INFO: Processing standard module hook 'hook-charset_normalizer.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
19406 INFO: Processing standard module hook 'hook-cryptography.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
20983 INFO: Processing standard module hook 'hook-zmq.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
21603 INFO: Processing pre-safe-import-module hook 'hook-platformdirs.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_safe_import_module'
21608 INFO: Processing standard module hook 'hook-platformdirs.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/stdhooks'
22324 INFO: Processing module hooks (post-graph stage)...
22339 WARNING: Hidden import "charset_normalizer.md__mypyc" not found!
24484 INFO: Processing standard module hook 'hook-PIL.SpiderImagePlugin.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
24560 INFO: Processing pre-safe-import-module hook 'hook-win32com.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/pre_safe_import_module'
24887 INFO: Processing standard module hook 'hook-_tkinter.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
24887 INFO: TclTkInfo: initializing cached Tcl/Tk info...
24929 INFO: Processing standard module hook 'hook-matplotlib.backends.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
24929 INFO: Matplotlib backend selection method: automatic discovery of used backends
25158 INFO: Found configured default matplotlib backend: module://matplotlib_inline.backend_inline
25158 INFO: Selected matplotlib backends: ['module://matplotlib_inline.backend_inline']
25194 INFO: Performing binary vs. data reclassification (2379 entries)
25232 INFO: Looking for ctypes DLLs
25277 WARNING: Library user32 required via ctypes not found
25322 INFO: Analyzing run-time hooks ...
25333 INFO: Including run-time hook 'pyi_rth_inspect.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/rthooks'
25334 INFO: Including run-time hook 'pyi_rth_pkgres.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/rthooks'
25337 INFO: Including run-time hook 'pyi_rth_multiprocessing.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/rthooks'
25338 INFO: Including run-time hook 'pyi_rth_pkgutil.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/rthooks'
25339 INFO: Including run-time hook 'pyi_rth_setuptools.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/rthooks'
25339 INFO: Including run-time hook 'pyi_rth__tkinter.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/rthooks'
25340 INFO: Including run-time hook 'pyi_rth_mplconfig.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/rthooks'
25341 INFO: Processing pre-find-module-path hook 'hook-_pyi_rth_utils.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks/pre_find_module_path'
25341 INFO: Processing standard module hook 'hook-_pyi_rth_utils.py' from '/opt/tljh/user/lib/python3.10/site-packages/PyInstaller/hooks'
25343 INFO: Including run-time hook 'pyi_rth_cryptography_openssl.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/rthooks'
25343 INFO: Including run-time hook 'pyi_rth_traitlets.py' from '/opt/tljh/user/lib/python3.10/site-packages/_pyinstaller_hooks_contrib/rthooks'
25428 INFO: Looking for dynamic libraries
26289 INFO: Warnings written to /opt/arcesium-python-2024-june/build/mygrep/warn-mygrep.txt
26416 INFO: Graph cross-reference written to /opt/arcesium-python-2024-june/build/mygrep/xref-mygrep.html
26501 INFO: checking PYZ
26501 INFO: Building PYZ because PYZ-00.toc is non existent
26501 INFO: Building PYZ (ZlibArchive) /opt/arcesium-python-2024-june/build/mygrep/PYZ-00.pyz
28133 INFO: Building PYZ (ZlibArchive) /opt/arcesium-python-2024-june/build/mygrep/PYZ-00.pyz completed successfully.
28284 INFO: checking PKG
28284 INFO: Building PKG because PKG-00.toc is non existent
28284 INFO: Building PKG (CArchive) mygrep.pkg
51217 INFO: Building PKG (CArchive) mygrep.pkg completed successfully.
51261 INFO: Bootloader /opt/tljh/user/lib/python3.10/site-packages/PyInstaller/bootloader/Linux-64bit-intel/run
51261 INFO: checking EXE
51261 INFO: Building EXE because EXE-00.toc is non existent
51261 INFO: Building EXE from EXE-00.toc
51262 INFO: Copying bootloader EXE to /opt/arcesium-python-2024-june/dist/mygrep
51262 INFO: Appending PKG archive to custom ELF section in EXE
51486 INFO: Building EXE from EXE-00.toc completed successfully.
Usage: mygrep [OPTIONS] KEYWORD FILENAME
╭─ Arguments ──────────────────────────────────────────────────────────────────╮
│ * keyword TEXT [default: None] [required] │
│ * filename TEXT [default: None] [required] │
╰──────────────────────────────────────────────────────────────────────────────╯
╭─ Options ────────────────────────────────────────────────────────────────────╮
│ --help Show this message and exit. │
╰──────────────────────────────────────────────────────────────────────────────╯
import time
import datetime
while True: # this will create infinite loop, run forever
time.sleep(5) # do n othing for 5 seconds!
today = datetime.datetime.now()
print("Tick")
if today.hour == 6 and oday.minute == 15:
print("This is the time ..to go!")
break
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
Tick
This is the time ..to go!
import datetime
import os
import win32com.client
def get_outlook():
return win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
def get_inbox(email=None):
outlook = get_outlook()
if email == None:
inbox = outlook.GetDefaultFolder(6) # this works
else:
inbox = outlook.Folders.Items(email).Folders.Items('Inbox')
# inbox1 = outlook.Folders.Items('123@abc.com').Folders.Items('Inbox') # To access 123@abc.com Inbox
# inbox2 = outlook.Folders.Items('456@def.com').Folders.Itmes('Inbox') # To access 456@def.com Inbox
return inbox
def get_default_inbox_messages():
inbox = get_inbox()
messages = inbox.Items
return messages
def saveattachemnts_from_email(subject):
"""saves attachment from email that matches with subject
"""
messages = get_default_inbox_messages()
path = os.path.expanduser("~/Desktop/Attachments")
today = datetime.date.today()
for message in messages:
if message.Subject == subject and message.Unread or message.Senton.date() == today:
# body_content = message.body
attachments = message.Attachments
attachment = attachments.Item(1)
for attachment in message.Attachments:
attachment.SaveAsFile(os.path.join(path, str(attachment)))
if message.Subject == subject and message.Unread:
message.Unread = False
break
# print first 3 emails from ...
def print_emails(folderindex: int = 6, emailcount: int = 3):
"""
folderindex 3, 4, 5, 6 ..Trash, Outbox, Sent, Inbox
""" # folders,and emailcount,
# try different numbers
messages = get_default_inbox_messages()
for i in range(emailcount):
message = messages[i]
print(message.Subject)
# this body of message can be parsed to extract table
print(message.Body)
print("="*30)
def send_email_with_attachment():
import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'To address'
mail.Subject = 'Message subject'
mail.Body = 'Message body'
mail.HTMLBody = '<h2>HTML Message body</h2>' # this field is optional
# To attach a file to the email (optional):
attachment = "Path to the attachment"
mail.Attachments.Add(attachment)
mail.Send()print is very good friend of debugging!