Skip to content

Commit 6a2d03b

Browse files
committed
more change
1 parent 089d2b4 commit 6a2d03b

14 files changed

Lines changed: 2315 additions & 16 deletions
File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
'''
22
# Chap 2: Working with Data
33
https://dabeaz-course.github.io/practical-python/Notes/02_Working_with_data/00_Overview.html
4+
2.1 Datatypes and Data Structures
5+
2.2 Containers
6+
2.3 Formatted Output
7+
2.4 Sequences
8+
2.5 Collections module
9+
2.6 List comprehensions
10+
2.7 Object model
11+
412
'''
513
##### 2.1 Datatypes and Data structures
614
#####
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
'''
22
# Chapter 3: Program Organization - practice
33
# Dated:2023/10/06
4+
3.1 Functions and Script Writing
5+
3.2 More Detail on Functions
6+
3.3 Exception Handling
7+
3.4 Modules
8+
3.5 Main module
9+
3.6 Design Discussion about Embracing Flexibility
410
'''
511

612
'''
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
'''
22
# 4.0 Class & Object
3-
3+
4.1 Introducing Classes
4+
4.2 Inheritance
5+
4.3 Special Methods
6+
4.4 Defining new Exception
47
'''
58
# class statement
69
# The object is always passed as the first argument. It is merely Python programming style to call this argument self

Work/06_chap.py renamed to Work/06_Generators.py

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
'''
2-
6. Generators
2+
6. Generators
3+
6.1 Iteration Protocol
4+
6.2 Customizing Iteration with Generators
5+
6.3 Producer/Consumer Problems and Workflows
6+
6.4 Generator Expressions
37
'''
48
## 6.1 Iteration Protocol
59

@@ -47,7 +51,7 @@ def countdown(n):
4751
for i in y:
4852
print(i)
4953

50-
# Exercise 6.4: A Simple Generator
54+
# Exercise 6.4: A Simple Generator filematch check
5155

5256
def quickLines(filename):
5357
print(filename)
@@ -60,15 +64,54 @@ def filematch(filename,substr):
6064
for line in f:
6165
if substr in line:
6266
yield line
67+
68+
6369
import os
6470
import pathlib
6571

6672
workdir = os.getcwd()
67-
workdir = workdir + r'\work\data'
68-
quickLines(workdir+r'\portfolio.csv')
73+
datadir = workdir + r'\work\data'
74+
quickLines(datadir+r'\portfolio.csv')
6975

7076
print('\n ----\n')
7177

7278

73-
for x in filematch(workdir+r'\portfolio.csv','IBM'):
79+
for x in filematch(datadir+r'\portfolio.csv','IBM'):
7480
print(x)
81+
82+
########
83+
####### 6.3 Producers, Consumers and Pipelines
84+
######## producer → processing → processing → consumer
85+
86+
## Generator Pipelines
87+
88+
## Exercise 6.8: Setting up a simple pipeline
89+
## producer → processing → processing → consumer
90+
def filematch(lines,substr):
91+
for line in lines:
92+
if substr in line:
93+
yield line
94+
95+
96+
from follow import follow
97+
import os
98+
import time
99+
100+
datadir = os.getcwd() + r'\work\data'
101+
stocklogFile = datadir + r'\stocklog.csv'
102+
lines = follow(stocklogFile) #processing
103+
104+
ibm = filematch(lines,'IBM') #processing
105+
106+
for line in ibm: #consumer
107+
print(line)
108+
109+
110+
##Exercise 6.9: Setting up a more complex pipeline
111+
from follow import follow
112+
import csv
113+
114+
lines = follow(stocklogFile)
115+
116+
for row in csv.reader(lines):
117+
print(row)

Work/07_AdvPython.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
'''
2+
7.1 Variable argument functions
3+
7.2 Anonymous functions and lambda
4+
7.3 Returning function and closures
5+
7.4 Function decorators
6+
7.5 Static and class methods
7+
'''
8+
9+
##7.1 Variable argument functions
10+
11+
###Positional variable arguments (*args)
12+
###
13+
def f(x,*args):
14+
print('x ={%d}'%x)
15+
for a in args:
16+
print(a)
17+
f(1,2,3,4,5)
18+
19+
### Keyword variable arguments (**kwargs)
20+
###
21+
def f2(x,**kwargs):
22+
print('x ={%d}'%x)
23+
for a in kwargs:
24+
print(a)
25+
26+
f2(4,mode='fast',flag=False,hdr="what")
27+
28+
### Passing Tuples and Dicts
29+
###
30+
nums=(1,2,3,4)
31+
nums2=(11,22,33,44)
32+
f(0,nums,nums2)
33+
34+
35+
d1 ={
36+
'x':2,
37+
'y':2.2,
38+
'z':99
39+
}
40+
41+
f(12,d1)
42+
43+
#average of nums
44+
def avg(*args):
45+
av = sum(args)/len(args)
46+
print(f'average=%d'%av)
47+
48+
avg(1,2,3,4,5)
49+
50+
'''scratch code test 15 min code
51+
read meslog.txt
52+
'''
53+
def read_meslog(filename):
54+
sections=[]
55+
print(filename)
56+
sections = []
57+
logs = []
58+
with open(filename,'r+') as f:
59+
hdr =f.readline()
60+
hdr = hdr.split()
61+
print(hdr)
62+
print(hdr[0])
63+
print(hdr[1])
64+
print(hdr[2])
65+
if hdr[0] != '.' and hdr[1] != '.'and hdr[2] != '.':
66+
id,lno,nsecs=str(hdr[0]),int(hdr[1]),int(hdr[2])
67+
#print(f'id=%s , sections=%d'%(id,secs))
68+
i =0
69+
while i < nsecs:
70+
line =f.readline()
71+
line = line.split()
72+
xs =XSec(line[0],line[1],line[2],line[3])
73+
print('\t',line)
74+
sections.append(xs)
75+
i +=1
76+
lgs = XLog(hdr[0],sections)
77+
logs.append(lgs)
78+
return logs
79+
80+
81+
## 2nd card; use a quick class
82+
83+
class XSec:
84+
def __init__(self,z,x,y,d):
85+
#instance data
86+
self.z=z
87+
self.x =x
88+
self.y=y
89+
self.d =d #dia
90+
#special methods
91+
def __str__(self):
92+
return f'{self.z} {self.x} {self.y} {self.d} '
93+
94+
class XLog:
95+
def __init__(self,lid,xs) -> None:
96+
self.logId = lid
97+
self.secs =xs
98+
99+
100+
import os
101+
import sys
102+
data_dir = os.getcwd()
103+
data_dir += r'\work\data'
104+
mesfile_path= data_dir + r'\meslogs1.txt'
105+
mesfile_path
106+
xlogs = read_meslog(mesfile_path)
107+
108+
print("\n\n")
109+
110+
for lg in xlogs:
111+
print(lg.logId)
112+
for x in lg.secs:
113+
print(x)
114+
115+
x1 = [1,3,4]
116+
117+
118+
### 7.2 Anonymous Functions and Lambda
119+
120+
import queue
121+
q1 =queue.Queue()
122+
q1.put(11)
123+
q1.put(12)
124+
q1.put(13)
125+
while( q1.qsize() !=0):
126+
print(q1.get())
127+
128+
129+
st = stack_data.s

Work/Data/meslogs1.txt

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
D06L1601 1 52 1.000000 . . . 0.000
2+
0.000 -0.131 -9.861 8.219 0.051 88.000
3+
0.173 -0.165 -9.882 8.222 0.099 92.300
4+
0.498 -0.153 -9.691 8.258 0.074 76.000
5+
0.822 -0.127 -9.487 8.244 0.066 62.200
6+
1.147 -0.102 -9.487 8.361 0.066 70.300
7+
1.472 -0.165 -9.742 8.417 0.140 59.100
8+
1.797 -0.280 -9.576 8.283 0.148 54.100
9+
2.122 -0.369 -9.551 8.219 0.140 50.900
10+
2.446 -0.420 -9.665 8.316 0.148 63.800
11+
2.771 -0.331 -9.793 8.262 0.263 62.000
12+
3.096 -0.280 -9.983 8.208 0.181 62.000
13+
3.421 -0.318 -9.983 8.154 0.115 63.900
14+
3.746 -0.369 -9.945 8.381 0.066 62.800
15+
4.071 -0.471 -10.085 8.197 0.115 60.600
16+
4.395 -0.636 -10.187 7.913 0.197 224.100
17+
4.720 -0.699 -10.365 7.819 0.115 214.700
18+
5.045 -0.674 -10.543 7.675 0.074 214.800
19+
5.370 -0.687 -10.492 7.592 0.082 210.600
20+
5.694 -0.432 -10.161 7.498 0.090 208.800
21+
6.019 -0.331 -10.161 7.454 0.066 202.900
22+
6.344 -0.420 -10.390 7.460 0.057 216.200
23+
6.669 -0.318 -10.454 7.407 0.066 197.200
24+
6.994 -0.343 -10.149 7.363 0.066 183.200
25+
7.319 -0.369 -10.034 7.369 0.074 188.800
26+
7.643 -0.369 -10.314 7.475 0.099 208.000
27+
7.968 -0.318 -10.161 7.522 0.066 202.400
28+
8.293 -0.267 -10.072 7.508 0.099 190.700
29+
8.618 -0.331 -10.085 7.594 0.049 176.500
30+
8.942 -0.356 -10.149 7.850 0.082 167.600
31+
9.267 -0.394 -10.060 7.887 0.205 190.100
32+
9.592 -0.585 -9.869 7.503 0.246 194.400
33+
9.917 -0.674 -9.831 7.439 0.214 193.300
34+
10.242 -0.547 -9.716 7.295 0.255 204.800
35+
10.567 -0.496 -9.818 7.102 0.131 194.900
36+
10.891 -0.598 -9.805 6.988 0.090 206.800
37+
11.216 -0.941 -9.920 6.844 0.099 224.500
38+
11.541 -0.661 -9.932 6.840 0.041 161.000
39+
11.866 -0.674 -9.983 6.837 0.016 173.800
40+
12.191 -0.687 -9.983 6.833 0.016 80.600
41+
12.515 -0.636 -10.047 6.829 0.033 108.800
42+
12.840 -0.801 -10.314 6.975 0.025 112.800
43+
13.165 -0.814 -10.454 6.782 0.074 131.200
44+
13.490 -0.649 -10.390 6.688 0.025 190.200
45+
13.815 -0.420 -10.365 6.584 0.033 105.700
46+
14.139 -0.560 -10.441 6.690 0.008 197.400
47+
14.464 -0.687 -10.416 7.036 0.214 68.600
48+
14.789 -0.839 -10.238 6.993 0.205 61.600
49+
15.114 -0.954 -10.263 6.649 0.049 80.500
50+
15.439 -0.814 -10.390 6.385 0.057 70.200
51+
15.763 -0.967 -10.416 6.271 0.016 85.600
52+
16.088 -1.259 -10.441 6.118 0.025 204.100
53+
16.350 -1.290 -10.400 6.042 0.038 21.700

0 commit comments

Comments
 (0)