x # NameErrorNameError: name 'x' is not defined
Look at error trace carefully, it have useful information with which you can figure whats wrong immediately
SyntaxError: unmatched ')' (2414728682.py, line 6)
NameError: name 'a' is not defined
500
‘XXX’ object is not callable!
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
<>:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
/tmp/ipykernel_4078463/1956412847.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
45([1, 2, 3])
/tmp/ipykernel_4078463/1956412847.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
45([1, 2, 3])
/tmp/ipykernel_4078463/1956412847.py:1: SyntaxWarning: 'int' object is not callable; perhaps you missed a comma?
45([1, 2, 3])
TypeError: 'int' object is not callable
Questions 1. What is x in function1? 2. what is data in function2? 3. what is function2 doing? 4. what is function1 doing?
where do we use this kind of access? x[0] - list, tuple, dictionary? data in function2? - list, tuple, dictionary
TypeError: 'int' object is not subscriptable
it is making list of columns
def column(matrix, n):
"""finds nth column from the matrix.
assumes that all rows of matrix are of same size
"""
return [row[n] for row in matrix]
def transpose(matrix):
"""finds transpose of a 2d matrix
assumes that every row in matrix is of same size
"""
firstrow = matrix[0]
colcount = len(firstrow)
return [column(matrix, c) for c in range(colcount)]Naming guidelines - i,j,k are for iteration varaiables , preferable if those are integers - x,y ..look like int/float from algebra! - if you know exactly what is the integers/float data, make use of that - for example instaed i , I can use col/c if i represents column index - for example instaed i , I can use row/r if i represents row index - for iterating over 2D data, each item from 2D can be iterated as [row for row in data2D] instead [item for item in data2d] - Give function name which represents the activity done by that function clearly.
%%file sumofsquares.py
import sys
def square(x):
return x*x
def sumofsquares(*args):
return sum([square(a) for a in args])
args = sys.argv[1:] # these are strings/text
intargs = [int(arg) for arg in args]
print(sumofsquares(intargs))Overwriting sumofsquares.py
Traceback (most recent call last):
File "/home/jupyter-vikrant/arcesium-python-2024/sumofsquares.py", line 13, in <module>
print(sumofsquares(intargs))
File "/home/jupyter-vikrant/arcesium-python-2024/sumofsquares.py", line 7, in sumofsquares
return sum([square(a) for a in args])
File "/home/jupyter-vikrant/arcesium-python-2024/sumofsquares.py", line 7, in <listcomp>
return sum([square(a) for a in args])
File "/home/jupyter-vikrant/arcesium-python-2024/sumofsquares.py", line 4, in square
return x*x
TypeError: can't multiply sequence by non-int of type 'list'
%%file sumofsquares.py
import sys
def square(x):
print(x)
return x*x
def sumofsquares(*args): # variable number of arguments
return sum([square(a) for a in args])
args = sys.argv[1:] # these are strings/text
intargs = [int(arg) for arg in args]
print(sumofsquares(*intargs)) # while passing make sure to pass list as variable number of argumentOverwriting sumofsquares.py
to 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
requirements.txt is nothing but list of packages that you will need for executing your python scipt
if you want to be very strict about the versions of packages , then make use pip freeze to create requirements.txt
For a common user, you make executable and instructions about using the program
you will need a thrid party libray pyinstaller
pip install pyinstaller
once you have pyinstaller you can make use this commnad to create executable
pyinstaller -F sumofsquares.py
there will be build and dist folder created. in dist folder there will be executable. This executable does not need even python installed on user’s system.
./sumofsquares 1 2 3 4 5 6
91
You will need to follow some folder structure
tableA
|
|--setup.py
|--requirements.txt
+-A
|
|--__init__.py
|-- extract_tableA.py
+-B
|
|-- __init__.py
|-- stats.py
%%file tableA/A/extract_tableA.py
import PyPDF2
def get_row_lables():
rowlabels = """Demand Met during Evening Peak hrs(MW) (at 2000 hrs; from RLDCs)
Peak Shortage (MW)
Energy Met (MU)
Hydro Gen (MU)
Wind Gen (MU)
Solar Gen (MU)
Energy Shortage (MU)
Maximum Demand Met During the Day (MW) (From NLDC SCADA)
Time Of Maximum Demand Met (From NLDC SCADA)""".split("\n")
return rowlabels
def extract_page(i):
with open("data.pdf", "rb") as f: # you will have to open in read-binary mode
pdfreader = PyPDF2.PdfReader(f)
page = pdfreader.pages[i]
return page.extract_text()
def extract_tableA_data(pagetext):
lines = pagetext.split("\n")
data_of_interest = lines[:10]
headers = data_of_interest[0].split()
data = [line.split() for line in data_of_interest[1:]]
return headers, data
def extract_table_A(filename):
page = extract_page(1)
colnames, rows = extract_tableA_data(page)
dictrows = [dict(zip(colnames, row)) for row in rows]
rowlabels = get_row_lables()
return pd.DataFrame(dictrows, index=rowlabels)
if __name__ == "main__":
extract_table_A(sys.argv[1])Overwriting tableA/A/extract_tableA.py
%%file tableA/setup.py
from distutils.core import setup
setup(
name="tableA",
version='1.0',
description="A sample package",
author="Vikrant",
author_email="sads@sdsa.com",
url="https://somesample.web.com",
packages=['A','A.B'],
install_requires=[
'PyPDF2',
'pandas'
]
entry_points={"console_scripts": ["extract_table=A.extract_tableA"]
)Overwriting tableA/setup.py
import time
import datetime
while True:
time.sleep(5) # during sleep cpu and resources are released for others to use
date = datetime.datetime.now()
print("tick")
if date.hour == 6 and date.minute == 43:
print("hello, the time has come to execute")
break tick
tick
tick
tick
tick
tick
tick
tick
hello, the time has come to execute