-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
259 changed files
with
600,233 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name,shares,price | ||
"AA",15,39.48 | ||
"AXP",10,62.58 | ||
"BA",5,98.31 | ||
"C",,53.08 | ||
"CAT",15,78.29 | ||
"DD",10,50.75 | ||
"DIS",50,N/A | ||
"GE",,37.23 | ||
"GM",15,31.44 | ||
"HD",20,37.67 | ||
"HPQ",5,45.81 | ||
"IBM",10,102.86 | ||
"INTC",,21.84 | ||
"JNJ",20,62.25 | ||
"JPM",10,50.35 | ||
"KO",5,51.65 | ||
"MCD",,51.11 | ||
"MMM",10,85.60 | ||
"MO",,70.09 | ||
"MRK",5,50.21 | ||
"MSFT",20,30.08 | ||
"PFE",,26.40 | ||
"PG",5,62.79 | ||
"T",10,40.03 | ||
"UTX",8,69.81 | ||
"VZ",,42.92 | ||
"WMT",10,49.78 | ||
"XOM",15,82.50 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
name,shares,price | ||
"AA",100,32.20 | ||
"IBM",50,91.10 | ||
"CAT",150,83.44 | ||
"MSFT",200,51.23 | ||
"GE",95,40.37 | ||
"MSFT",50,65.10 | ||
"IBM",100,70.44 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
AA 100 32.20 | ||
IBM 50 91.10 | ||
CAT 150 83.44 | ||
MSFT 200 51.23 | ||
GE 95 40.37 | ||
MSFT 50 65.10 | ||
IBM 100 70.44 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
AA 100 32.20 | ||
IBM 50 91.10 | ||
CAT 150 83.44 | ||
MSFT 200 51.23 | ||
GE 95 40.37 | ||
MSFT 50 65.10 | ||
IBM 100 70.44 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name,shares,price | ||
"AA",50,27.10 | ||
"HPQ",250,43.15 | ||
"MSFT",25,50.15 | ||
"GE",125,52.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
AA 50 27.10 | ||
HPQ 250 43.15 | ||
MSFT 25 50.15 | ||
GE 125 52.10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"AA",15,39.48 | ||
"AXP",10,62.58 | ||
"BA",5,98.31 | ||
"C",-,53.08 | ||
"CAT",15,78.29 | ||
"DD",10,50.75 | ||
"DIS",-,N/A | ||
"GE",-,37.23 | ||
"GM",15,31.44 | ||
"HD",20,37.67 | ||
"HPQ",5,45.81 | ||
"IBM",10,102.86 | ||
"INTC",-,21.84 | ||
"JNJ",20,62.25 | ||
"JPM",10,50.35 | ||
"KO",5,51.65 | ||
"MCD",-,51.11 | ||
"MMM",10,85.60 | ||
"MO",-,70.09 | ||
"MRK",5,50.21 | ||
"MSFT",20,30.08 | ||
"PFE",-,26.40 | ||
"PG",5,62.79 | ||
"T",10,40.03 | ||
"UTX",8,69.81 | ||
"VZ",-,42.92 | ||
"WMT",10,49.78 | ||
"XOM",15,82.50 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
AA 15 39.48 | ||
AXP 10 62.58 | ||
BA 5 98.31 | ||
C - 53.08 | ||
CAT 15 78.29 | ||
DD 10 50.75 | ||
DIS - N/A | ||
GE - 37.23 | ||
GM 15 31.44 | ||
HD 20 37.67 | ||
HPQ 5 45.81 | ||
IBM 10 102.86 | ||
INTC - 21.84 | ||
JNJ 20 62.25 | ||
JPM 10 50.35 | ||
KO 5 51.65 | ||
MCD - 51.11 | ||
MMM 10 85.60 | ||
MO - 70.09 | ||
MRK 5 50.21 | ||
MSFT 20 30.08 | ||
PFE - 26.40 | ||
PG 5 62.79 | ||
T 10 40.03 | ||
UTX 8 69.81 | ||
VZ - 42.92 | ||
WMT 10 49.78 | ||
XOM 15 82.50 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
"AA",100,32.20 | ||
"IBM",50,91.10 | ||
"CAT",150,83.44 | ||
"MSFT",200,51.23 | ||
"GE",95,40.37 | ||
"MSFT",50,65.10 | ||
"IBM",100,70.44 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
"AA",9.22 | ||
"AXP",24.85 | ||
"BA",44.85 | ||
"BAC",11.27 | ||
"C",3.72 | ||
"CAT",35.46 | ||
"CVX",66.67 | ||
"DD",28.47 | ||
"DIS",24.22 | ||
"GE",13.48 | ||
"GM",0.75 | ||
"HD",23.16 | ||
"HPQ",34.35 | ||
"IBM",106.28 | ||
"INTC",15.72 | ||
"JNJ",55.16 | ||
"JPM",36.90 | ||
"KFT",26.11 | ||
"KO",49.16 | ||
"MCD",58.99 | ||
"MMM",57.10 | ||
"MRK",27.58 | ||
"MSFT",20.89 | ||
"PFE",15.19 | ||
"PG",51.94 | ||
"T",24.79 | ||
"UTX",52.61 | ||
"VZ",29.26 | ||
"WMT",49.74 | ||
"XOM",69.35 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
#!/usr/bin/env python | ||
# stocksim.py | ||
# | ||
# Stock market simulator. This simulator creates stock market | ||
# data and provides it in several different ways: | ||
# | ||
# 1. Makes periodic updates to a log file stocklog.dat | ||
# | ||
# The purpose of this module is to provide data to the user | ||
# in different ways in order to write interesting Python examples | ||
|
||
import math | ||
import time | ||
import threading | ||
try: | ||
import queue | ||
except ImportError: | ||
import Queue as queue | ||
|
||
history_file = "dowstocks.csv" | ||
|
||
# Convert a time string such as "4:00pm" to minutes past midnight | ||
def minutes(tm): | ||
am_pm = tm[-2:] | ||
fields = tm[:-2].split(":") | ||
hour = int(fields[0]) | ||
minute = int(fields[1]) | ||
if hour == 12: | ||
hour = 0 | ||
if am_pm == 'pm': | ||
hour += 12 | ||
return hour*60 + minute | ||
|
||
# Convert time in minutes to a format string | ||
def minutes_to_str(m): | ||
frac,m = math.modf(m) | ||
hours = m//60 | ||
minutes = m % 60 | ||
seconds = frac * 60 | ||
return "%02d:%02d.%02.f" % (hours,minutes,seconds) | ||
|
||
# Read the stock history file as a list of lists | ||
def read_history(filename): | ||
result = [] | ||
for line in open(filename): | ||
str_fields = line.strip().split(",") | ||
fields = [eval(x) for x in str_fields] | ||
fields[3] = minutes(fields[3]) | ||
result.append(fields) | ||
return result | ||
|
||
# Format CSV record | ||
def csv_record(fields): | ||
s = '"%s",%0.2f,"%s","%s",%0.2f,%0.2f,%0.2f,%0.2f,%d' % tuple(fields) | ||
return s | ||
|
||
class StockTrack(object): | ||
def __init__(self,name): | ||
self.name = name | ||
self.history = [] | ||
self.price = 0 | ||
self.time = 0 | ||
self.index = 0 | ||
self.open = 0 | ||
self.low = 0 | ||
self.high = 0 | ||
self.volume = 0 | ||
self.initial = 0 | ||
self.change = 0 | ||
self.date = "" | ||
def add_data(self,record): | ||
self.history.append(record) | ||
def reset(self,time): | ||
self.time = time | ||
# Sort the history by time | ||
self.history.sort(key=lambda t:t[3]) | ||
# Find the first entry who's time is behind the given time | ||
self.index = 0 | ||
while self.index < len(self.history): | ||
if self.history[self.index][3] > time: | ||
break | ||
self.index += 1 | ||
self.open = self.history[0][5] | ||
self.initial = self.history[0][1] - self.history[0][4] | ||
self.date = self.history[0][2] | ||
self.update() | ||
self.low = self.price | ||
self.high = self.price | ||
|
||
# Calculate interpolated value of a given field based on | ||
# current time | ||
def interpolate(self,field): | ||
first = self.history[self.index][field] | ||
next = self.history[self.index+1][field] | ||
first_t = self.history[self.index][3] | ||
next_t = self.history[self.index+1][3] | ||
try: | ||
slope = (next - first)/(next_t-first_t) | ||
return first + slope*(self.time - first_t) | ||
except ZeroDivisionError: | ||
return first | ||
|
||
# Update all computed values | ||
def update(self): | ||
self.price = round(self.interpolate(1),2) | ||
self.volume = int(self.interpolate(-1)) | ||
if self.price < self.low: | ||
self.low = self.price | ||
if self.price >= self.high: | ||
self.high = self.price | ||
self.change = self.price - self.initial | ||
|
||
# Increment the time by a delta | ||
def incr(self,dt): | ||
self.time += dt | ||
if self.index < (len(self.history) - 2): | ||
while self.index < (len(self.history) - 2) and self.time >= self.history[self.index+1][3]: | ||
self.index += 1 | ||
self.update() | ||
|
||
def make_record(self): | ||
return [self.name,round(self.price,2),self.date,minutes_to_str(self.time),round(self.change,2),self.open,round(self.high,2), | ||
round(self.low,2),self.volume] | ||
|
||
class MarketSimulator(object): | ||
def __init__(self): | ||
self.stocks = { } | ||
self.prices = { } | ||
self.time = 0 | ||
self.observers = [] | ||
def register(self,observer): | ||
self.observers.append(observer) | ||
|
||
def publish(self,record): | ||
for obj in self.observers: | ||
obj.update(record) | ||
def add_history(self,filename): | ||
hist = read_history(filename) | ||
for record in hist: | ||
if record[0] not in self.stocks: | ||
self.stocks[record[0]] = StockTrack(record[0]) | ||
self.stocks[record[0]].add_data(record) | ||
|
||
def reset(self,time): | ||
self.time = time | ||
for s in list(self.stocks.values()): | ||
s.reset(time) | ||
|
||
# Run forever. Dt is in seconds | ||
def run(self,dt): | ||
for s in self.stocks: | ||
self.prices[s] = self.stocks[s].price | ||
self.publish(self.stocks[s].make_record()) | ||
while self.time < 1000: | ||
for s in self.stocks: | ||
self.stocks[s].incr(dt/60.0) # Increment is in minutes | ||
if self.stocks[s].price != self.prices[s]: | ||
self.prices[s] = self.stocks[s].price | ||
self.publish(self.stocks[s].make_record()) | ||
time.sleep(dt) | ||
self.time += (dt/60.0) | ||
|
||
|
||
class BasicPrinter(object): | ||
def update(self,record): | ||
print(csv_record(record)) | ||
|
||
class LogPrinter(object): | ||
def __init__(self,filename): | ||
self.f = open(filename,"w") | ||
def update(self,record): | ||
self.f.write(csv_record(record)+"\n") | ||
self.f.flush() | ||
|
||
m = MarketSimulator() | ||
m.add_history(history_file) | ||
m.reset(minutes("9:30am")) | ||
m.register(BasicPrinter()) | ||
m.register(LogPrinter("stocklog.csv")) | ||
m.run(1) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
look into my eyes | ||
look into my eyes | ||
the eyes the eyes the eyes | ||
not around the eyes | ||
don't look around the eyes | ||
look into my eyes you're under |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# Advanced Python Mastery | ||
|
||
Copyright (C) 2007-2023 | ||
David Beazley ([email protected]) | ||
http://www.dabeaz.com | ||
|
||
Welcome to the Python Mastery course. This | ||
directory, `pythonmaster` is where you find support files | ||
related to the class exercises. It is also where you will be doing | ||
your work. | ||
|
||
This course requires the use of Python 3.6 or newer. If you are | ||
using Python 2, most of the material still applies, but you will | ||
have to make minor code modifications here and there. | ||
|
||
- link:PythonMastery.pdf[`PythonMastery.pdf`] is a PDF that contains | ||
all of the presentation slides. | ||
|
||
- The link:Exercises/index.html[`Exercises/`] folder is where you | ||
find all the class exercises. | ||
|
||
- The `Data/` folder is where you find data files, scripts, and | ||
other files used by the exercises. | ||
|
||
- The `Solutions/` folder contains complete solution code for | ||
various exercises. Each problem has its own directory. For example, | ||
the solution to exercise 3.2 can be found in the `Solution/3_2/` directory. | ||
|
||
Every attempt has been made to make sure exercises work. However, it's | ||
possible that you will find typos or minor mistakes. If you find any | ||
errors, please let me know so that I can fix them for future editions | ||
of the course. | ||
|
||
|
||
|
||
|
||
|
Oops, something went wrong.