Skip to content

Commit 7d4b301

Browse files
committed
Initial commit
1 parent 82e815f commit 7d4b301

File tree

259 files changed

+600233
-2
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

259 files changed

+600233
-2
lines changed

Data/ctabus.csv

+577,564
Large diffs are not rendered by default.

Data/dowstocks.csv

+2,340
Large diffs are not rendered by default.

Data/missing.csv

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name,shares,price
2+
"AA",15,39.48
3+
"AXP",10,62.58
4+
"BA",5,98.31
5+
"C",,53.08
6+
"CAT",15,78.29
7+
"DD",10,50.75
8+
"DIS",50,N/A
9+
"GE",,37.23
10+
"GM",15,31.44
11+
"HD",20,37.67
12+
"HPQ",5,45.81
13+
"IBM",10,102.86
14+
"INTC",,21.84
15+
"JNJ",20,62.25
16+
"JPM",10,50.35
17+
"KO",5,51.65
18+
"MCD",,51.11
19+
"MMM",10,85.60
20+
"MO",,70.09
21+
"MRK",5,50.21
22+
"MSFT",20,30.08
23+
"PFE",,26.40
24+
"PG",5,62.79
25+
"T",10,40.03
26+
"UTX",8,69.81
27+
"VZ",,42.92
28+
"WMT",10,49.78
29+
"XOM",15,82.50

Data/portfolio.csv

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name,shares,price
2+
"AA",100,32.20
3+
"IBM",50,91.10
4+
"CAT",150,83.44
5+
"MSFT",200,51.23
6+
"GE",95,40.37
7+
"MSFT",50,65.10
8+
"IBM",100,70.44

Data/portfolio.csv.gz

136 Bytes
Binary file not shown.

Data/portfolio.dat

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
AA 100 32.20
2+
IBM 50 91.10
3+
CAT 150 83.44
4+
MSFT 200 51.23
5+
GE 95 40.37
6+
MSFT 50 65.10
7+
IBM 100 70.44

Data/portfolio1.dat

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
AA 100 32.20
2+
IBM 50 91.10
3+
CAT 150 83.44
4+
MSFT 200 51.23
5+
GE 95 40.37
6+
MSFT 50 65.10
7+
IBM 100 70.44

Data/portfolio2.csv

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name,shares,price
2+
"AA",50,27.10
3+
"HPQ",250,43.15
4+
"MSFT",25,50.15
5+
"GE",125,52.10

Data/portfolio2.dat

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
AA 50 27.10
2+
HPQ 250 43.15
3+
MSFT 25 50.15
4+
GE 125 52.10

Data/portfolio3.csv

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"AA",15,39.48
2+
"AXP",10,62.58
3+
"BA",5,98.31
4+
"C",-,53.08
5+
"CAT",15,78.29
6+
"DD",10,50.75
7+
"DIS",-,N/A
8+
"GE",-,37.23
9+
"GM",15,31.44
10+
"HD",20,37.67
11+
"HPQ",5,45.81
12+
"IBM",10,102.86
13+
"INTC",-,21.84
14+
"JNJ",20,62.25
15+
"JPM",10,50.35
16+
"KO",5,51.65
17+
"MCD",-,51.11
18+
"MMM",10,85.60
19+
"MO",-,70.09
20+
"MRK",5,50.21
21+
"MSFT",20,30.08
22+
"PFE",-,26.40
23+
"PG",5,62.79
24+
"T",10,40.03
25+
"UTX",8,69.81
26+
"VZ",-,42.92
27+
"WMT",10,49.78
28+
"XOM",15,82.50

Data/portfolio3.dat

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
AA 15 39.48
2+
AXP 10 62.58
3+
BA 5 98.31
4+
C - 53.08
5+
CAT 15 78.29
6+
DD 10 50.75
7+
DIS - N/A
8+
GE - 37.23
9+
GM 15 31.44
10+
HD 20 37.67
11+
HPQ 5 45.81
12+
IBM 10 102.86
13+
INTC - 21.84
14+
JNJ 20 62.25
15+
JPM 10 50.35
16+
KO 5 51.65
17+
MCD - 51.11
18+
MMM 10 85.60
19+
MO - 70.09
20+
MRK 5 50.21
21+
MSFT 20 30.08
22+
PFE - 26.40
23+
PG 5 62.79
24+
T 10 40.03
25+
UTX 8 69.81
26+
VZ - 42.92
27+
WMT 10 49.78
28+
XOM 15 82.50

Data/portfolio_noheader.csv

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
"AA",100,32.20
2+
"IBM",50,91.10
3+
"CAT",150,83.44
4+
"MSFT",200,51.23
5+
"GE",95,40.37
6+
"MSFT",50,65.10
7+
"IBM",100,70.44

Data/prices.csv

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"AA",9.22
2+
"AXP",24.85
3+
"BA",44.85
4+
"BAC",11.27
5+
"C",3.72
6+
"CAT",35.46
7+
"CVX",66.67
8+
"DD",28.47
9+
"DIS",24.22
10+
"GE",13.48
11+
"GM",0.75
12+
"HD",23.16
13+
"HPQ",34.35
14+
"IBM",106.28
15+
"INTC",15.72
16+
"JNJ",55.16
17+
"JPM",36.90
18+
"KFT",26.11
19+
"KO",49.16
20+
"MCD",58.99
21+
"MMM",57.10
22+
"MRK",27.58
23+
"MSFT",20.89
24+
"PFE",15.19
25+
"PG",51.94
26+
"T",24.79
27+
"UTX",52.61
28+
"VZ",29.26
29+
"WMT",49.74
30+
"XOM",69.35

Data/stocksim.py

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
#!/usr/bin/env python
2+
# stocksim.py
3+
#
4+
# Stock market simulator. This simulator creates stock market
5+
# data and provides it in several different ways:
6+
#
7+
# 1. Makes periodic updates to a log file stocklog.dat
8+
#
9+
# The purpose of this module is to provide data to the user
10+
# in different ways in order to write interesting Python examples
11+
12+
import math
13+
import time
14+
import threading
15+
try:
16+
import queue
17+
except ImportError:
18+
import Queue as queue
19+
20+
history_file = "dowstocks.csv"
21+
22+
# Convert a time string such as "4:00pm" to minutes past midnight
23+
def minutes(tm):
24+
am_pm = tm[-2:]
25+
fields = tm[:-2].split(":")
26+
hour = int(fields[0])
27+
minute = int(fields[1])
28+
if hour == 12:
29+
hour = 0
30+
if am_pm == 'pm':
31+
hour += 12
32+
return hour*60 + minute
33+
34+
# Convert time in minutes to a format string
35+
def minutes_to_str(m):
36+
frac,m = math.modf(m)
37+
hours = m//60
38+
minutes = m % 60
39+
seconds = frac * 60
40+
return "%02d:%02d.%02.f" % (hours,minutes,seconds)
41+
42+
# Read the stock history file as a list of lists
43+
def read_history(filename):
44+
result = []
45+
for line in open(filename):
46+
str_fields = line.strip().split(",")
47+
fields = [eval(x) for x in str_fields]
48+
fields[3] = minutes(fields[3])
49+
result.append(fields)
50+
return result
51+
52+
# Format CSV record
53+
def csv_record(fields):
54+
s = '"%s",%0.2f,"%s","%s",%0.2f,%0.2f,%0.2f,%0.2f,%d' % tuple(fields)
55+
return s
56+
57+
class StockTrack(object):
58+
def __init__(self,name):
59+
self.name = name
60+
self.history = []
61+
self.price = 0
62+
self.time = 0
63+
self.index = 0
64+
self.open = 0
65+
self.low = 0
66+
self.high = 0
67+
self.volume = 0
68+
self.initial = 0
69+
self.change = 0
70+
self.date = ""
71+
def add_data(self,record):
72+
self.history.append(record)
73+
def reset(self,time):
74+
self.time = time
75+
# Sort the history by time
76+
self.history.sort(key=lambda t:t[3])
77+
# Find the first entry who's time is behind the given time
78+
self.index = 0
79+
while self.index < len(self.history):
80+
if self.history[self.index][3] > time:
81+
break
82+
self.index += 1
83+
self.open = self.history[0][5]
84+
self.initial = self.history[0][1] - self.history[0][4]
85+
self.date = self.history[0][2]
86+
self.update()
87+
self.low = self.price
88+
self.high = self.price
89+
90+
# Calculate interpolated value of a given field based on
91+
# current time
92+
def interpolate(self,field):
93+
first = self.history[self.index][field]
94+
next = self.history[self.index+1][field]
95+
first_t = self.history[self.index][3]
96+
next_t = self.history[self.index+1][3]
97+
try:
98+
slope = (next - first)/(next_t-first_t)
99+
return first + slope*(self.time - first_t)
100+
except ZeroDivisionError:
101+
return first
102+
103+
# Update all computed values
104+
def update(self):
105+
self.price = round(self.interpolate(1),2)
106+
self.volume = int(self.interpolate(-1))
107+
if self.price < self.low:
108+
self.low = self.price
109+
if self.price >= self.high:
110+
self.high = self.price
111+
self.change = self.price - self.initial
112+
113+
# Increment the time by a delta
114+
def incr(self,dt):
115+
self.time += dt
116+
if self.index < (len(self.history) - 2):
117+
while self.index < (len(self.history) - 2) and self.time >= self.history[self.index+1][3]:
118+
self.index += 1
119+
self.update()
120+
121+
def make_record(self):
122+
return [self.name,round(self.price,2),self.date,minutes_to_str(self.time),round(self.change,2),self.open,round(self.high,2),
123+
round(self.low,2),self.volume]
124+
125+
class MarketSimulator(object):
126+
def __init__(self):
127+
self.stocks = { }
128+
self.prices = { }
129+
self.time = 0
130+
self.observers = []
131+
def register(self,observer):
132+
self.observers.append(observer)
133+
134+
def publish(self,record):
135+
for obj in self.observers:
136+
obj.update(record)
137+
def add_history(self,filename):
138+
hist = read_history(filename)
139+
for record in hist:
140+
if record[0] not in self.stocks:
141+
self.stocks[record[0]] = StockTrack(record[0])
142+
self.stocks[record[0]].add_data(record)
143+
144+
def reset(self,time):
145+
self.time = time
146+
for s in list(self.stocks.values()):
147+
s.reset(time)
148+
149+
# Run forever. Dt is in seconds
150+
def run(self,dt):
151+
for s in self.stocks:
152+
self.prices[s] = self.stocks[s].price
153+
self.publish(self.stocks[s].make_record())
154+
while self.time < 1000:
155+
for s in self.stocks:
156+
self.stocks[s].incr(dt/60.0) # Increment is in minutes
157+
if self.stocks[s].price != self.prices[s]:
158+
self.prices[s] = self.stocks[s].price
159+
self.publish(self.stocks[s].make_record())
160+
time.sleep(dt)
161+
self.time += (dt/60.0)
162+
163+
164+
class BasicPrinter(object):
165+
def update(self,record):
166+
print(csv_record(record))
167+
168+
class LogPrinter(object):
169+
def __init__(self,filename):
170+
self.f = open(filename,"w")
171+
def update(self,record):
172+
self.f.write(csv_record(record)+"\n")
173+
self.f.flush()
174+
175+
m = MarketSimulator()
176+
m.add_history(history_file)
177+
m.reset(minutes("9:30am"))
178+
m.register(BasicPrinter())
179+
m.register(LogPrinter("stocklog.csv"))
180+
m.run(1)
181+
182+
183+

Data/words.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
look into my eyes
2+
look into my eyes
3+
the eyes the eyes the eyes
4+
not around the eyes
5+
don't look around the eyes
6+
look into my eyes you're under

Exercises/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Advanced Python Mastery
2+
3+
Copyright (C) 2007-2023
4+
David Beazley (dave@dabeaz.com)
5+
http://www.dabeaz.com
6+
7+
Welcome to the Python Mastery course. This
8+
directory, `pythonmaster` is where you find support files
9+
related to the class exercises. It is also where you will be doing
10+
your work.
11+
12+
This course requires the use of Python 3.6 or newer. If you are
13+
using Python 2, most of the material still applies, but you will
14+
have to make minor code modifications here and there.
15+
16+
- link:PythonMastery.pdf[`PythonMastery.pdf`] is a PDF that contains
17+
all of the presentation slides.
18+
19+
- The link:Exercises/index.html[`Exercises/`] folder is where you
20+
find all the class exercises.
21+
22+
- The `Data/` folder is where you find data files, scripts, and
23+
other files used by the exercises.
24+
25+
- The `Solutions/` folder contains complete solution code for
26+
various exercises. Each problem has its own directory. For example,
27+
the solution to exercise 3.2 can be found in the `Solution/3_2/` directory.
28+
29+
Every attempt has been made to make sure exercises work. However, it's
30+
possible that you will find typos or minor mistakes. If you find any
31+
errors, please let me know so that I can fix them for future editions
32+
of the course.
33+
34+
35+
36+
37+

0 commit comments

Comments
 (0)