File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change
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 )
Original file line number Diff line number Diff line change
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 ))
You can’t perform that action at this time.
0 commit comments