forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_FunctionModules.py
More file actions
151 lines (125 loc) · 3.73 KB
/
03_FunctionModules.py
File metadata and controls
151 lines (125 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
'''
# Chapter 3: Program Organization - practice
# Dated:2023/10/06
3.1 Functions and Script Writing
3.2 More Detail on Functions
3.3 Exception Handling
3.4 Modules
3.5 Main module
3.6 Design Discussion about Embracing Flexibility
'''
'''
3.1 Scripting
'''
#functions
def square_root(num):
'''
About square root
'''
return num*num
square_root(44)
## to try... writing report.py program as a consolidated one
'''
3.2 More on Functions
'''
# default arguments
# If no return value is given or return is missing, None is returned.
# multiple reutrn value as tuple
#note:: function can't modify global variable outside
# to modify a global vairbale it has to prefix with global keyword inside its scope
name = 'Dave'
def spam():
global name
name = 'Guido' # Changes the global name above
# argument passing - pass by value ; pass by ref
## Key point: Functions don’t receive a copy of the input arguments.
## Reminder: Variable assignment never overwrites memory. The name is merely bound to a new value.
# fileparse.py
#Reading CSV Files
# Exercise 3.3
## 3.3 Exception Handling --
## Error Checking
# raising an error
code = ['dfd2','32df','ffss','ggd']
cd ='ffs2s'
if cd not in code:
raise LookupError(f'{cd} no there in list')
else:
print("found")
# to catch an exception use try-except
# Exceptions propagate to the first matching except##
def foo(nm):
try:
code.append('adfs')
code[2] = '1233'
print(code)
except NameError as e:
print(e)
foo(cd)
# Catching Multiple Errors
'''
try:
...
except (IOError,LookupError,RuntimeError) as e:
...
'''
# Catching All Errors -To catch any exception, use Exception like this:
'''
try:
...
except Exception: # DANGER. See below
print('An error occurred')
'''
### finally statement
'''
It specifies code that must run regardless of whether or not an exception occurs.
finally:
lock.release() # this will ALWAYS be executed. With and without exception.
with statement
In modern code, try-finally is often replaced with the with statement.
with open(filename) as f:
with defines a usage context for a resource. When execution leaves that context, resources are released.
with only works with certain objects that have been specifically programmed to support it.
'''
##todo excercise- 3.9 and 3.10
### 3.4 Modules
###
'''
** any python file is a module; module names is tied to file name
import fileparse
import report
import os
import csv
workdir = os.getcwd
workdir += r'\work'
recs = fileparse.parse_csv(workdir+r'\data\portfolio.csv')
'''
##### Namespaces
'''
A module is a collection of named values and is sometimes said to be a namespace. The names are all of the global variables
and functions defined in the source file. After importing, the module name is used as a prefix. Hence the namespace.
'''
## import as statement
#You can change the name of a module as you import it: -- like an alias
'''
Specifically, import always executes the entire file and modules are still isolated environments.
The import module as statement is only changing the name locally. The from math import cos, sin statement still loads
the entire math module behind the scenes. It’s merely copying the cos and sin names from the module into the local space
after it’s done. Each module loads and executes only once. Note: Repeated imports just return a reference to the previously loaded module.
'''
import fileparse as fi
import math as m
fi.parse_csv()
def rectangle(r,theta):
x = r*m.cos(theta)
y = r*m.sin(theta)
return x,y
#from module import
#This picks selected symbols out of a module and makes them available locally.
#
from math import sin,cos
#locating modules
import sys
sys.path
#sys.path.append(workdir)
sys.modules.keys