Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 480cf94

Browse files
author
Siddharta Govindaraj
committed
Added processor and reader modules which are used in the doctests
1 parent 5cd3f73 commit 480cf94

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

stock_alerter/processor.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Processor:
2+
def __init__(self, reader, exchange):
3+
self.reader = reader
4+
self.exchange = exchange
5+
6+
def process(self):
7+
for symbol, timestamp, price in self.reader.get_updates():
8+
stock = self.exchange[symbol]
9+
stock.update(timestamp, price)

stock_alerter/reader.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from datetime import datetime
2+
3+
4+
class ListReader:
5+
"""Reads a series of updates from a list"""
6+
def __init__(self, updates):
7+
self.updates = updates
8+
9+
def get_updates(self):
10+
for update in self.updates:
11+
yield update
12+
13+
14+
class FileReader:
15+
"""Reads a series of stock updates from a file"""
16+
def __init__(self, filename):
17+
self.filename = filename
18+
19+
def get_updates(self):
20+
"""Returns the next update everytime the method is called"""
21+
with open(self.filename, "r") as fp:
22+
data = fp.read()
23+
lines = data.split()
24+
for line in lines:
25+
symbol, timestamp, price = line.split(",")
26+
yield (symbol,
27+
datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f"),
28+
int(price))

0 commit comments

Comments
 (0)