forked from dabeaz-course/practical-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path07_AdvPython.py
More file actions
161 lines (130 loc) · 3.11 KB
/
07_AdvPython.py
File metadata and controls
161 lines (130 loc) · 3.11 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
152
153
154
155
156
157
158
159
160
161
'''
7.1 Variable argument functions
7.2 Anonymous functions and lambda
7.3 Returning function and closures
7.4 Function decorators
7.5 Static and class methods
'''
##7.1 Variable argument functions
###Positional variable arguments (*args)
###
def f(x,*args):
print('x ={%d}'%x)
for a in args:
print(a)
f(1,2,3,4,5)
### Keyword variable arguments (**kwargs)
###
def f2(x,**kwargs):
print('x ={%d}'%x)
for a in kwargs:
print(a)
f2(4,mode='fast',flag=False,hdr="what")
### Passing Tuples and Dicts
###
nums=(1,2,3,4)
nums2=(11,22,33,44)
f(0,nums,nums2)
d1 ={
'x':2,
'y':2.2,
'z':99
}
f(12,d1)
#average of nums
def avg(*args):
av = sum(args)/len(args)
print(f'average=%d'%av)
avg(1,2,3,4,5)
'''scratch code test 15 min code
read meslog.txt
'''
def read_meslog(filename):
sections=[]
print(filename)
sections = []
logs = []
with open(filename,'r+') as f:
hdr =f.readline()
hdr = hdr.split()
print(hdr)
print(hdr[0])
print(hdr[1])
print(hdr[2])
if hdr[0] != '.' and hdr[1] != '.'and hdr[2] != '.':
id,lno,nsecs=str(hdr[0]),int(hdr[1]),int(hdr[2])
#print(f'id=%s , sections=%d'%(id,secs))
i =0
while i < nsecs:
line =f.readline()
line = line.split()
xs =XSec(line[0],line[1],line[2],line[3])
print('\t',line)
sections.append(xs)
i +=1
lgs = XLog(hdr[0],sections)
logs.append(lgs)
return logs
## 2nd card; use a quick class
class XSec:
def __init__(self,z,x,y,d):
#instance data
self.z=z
self.x =x
self.y=y
self.d =d #dia
#special methods
def __str__(self):
return f'{self.z} {self.x} {self.y} {self.d} '
class XLog:
def __init__(self,lid,xs) -> None:
self.logId = lid
self.secs =xs
import os
import sys
data_dir = os.getcwd()
data_dir += r'\work\data'
mesfile_path= data_dir + r'\meslogs1.txt'
mesfile_path
xlogs = read_meslog(mesfile_path)
print("\n\n")
for lg in xlogs:
print(lg.logId)
for x in lg.secs:
print(x)
x1 = [1,3,4]
### 7.2 Anonymous Functions and Lambda
import queue
q1 =queue.Queue()
q1.put(11)
q1.put(12)
q1.put(13)
while( q1.qsize() !=0):
print(q1.get())
##
excdir = os.getcwd()
excdir += r'\work'
sys.path
sys.path.append(excdir)
from stock import Stock
slist = []
slist.append(Stock('DAA',22,1111))
slist.append(Stock('CAB',122,1211))
slist.append(Stock('CC',222,5353))
slist.append(Stock('AC',222,5353))
for s in slist:
print(s.name)
## sorting using lambda function
slist.sort(key=lambda a:a.name)
for s in slist:
print(s.name)
## dict + lambda function
mdict =[{'name': 'AA', 'price': 32.2, 'shares': 100},
{'name': 'IBM', 'price': 91.1, 'shares': 50},
{'name': 'CAT', 'price': 83.44, 'shares': 150},
{'name': 'MSFT', 'price': 51.23, 'shares': 200},
{'name': 'GE', 'price': 40.37, 'shares': 95},
{'name': 'MSFT', 'price': 65.1, 'shares': 50},
{'name': 'IBM', 'price': 70.44, 'shares': 100}]
mdict.sort(key=lambda a:a['name'])
mdict