我有一个这样的课程(按比例缩小)
class FileAndWriter(object):
def __init__(self, symbol, start_dt_str):
self.data = []
self.fileName = symbol.replace("/", "") + '-' + start_dt_str + '.csv'
self.file = open(self.fileName, 'ab')
self.csvWiter = csv.writer(self.file, delimiter=',') # , quoting=csv. QUOTE_ALL
def write_rows(self):
self.csvWiter.writerows(self.data)
self.file.flush()
def appendData(dataMsg):
try:
self.data.append(dataMsg)
if len(self.data) > 100:
self.write_rows(self.data)
self.data = []
except:
print "appendData error:", sys.exc_info()[0]
我有另一个类就像这样组成了这个类。 update
是Observer的回调函数:
class SymbolWriter(Observer):
def __init__(self, symbol, sd, ed):
self.writers = {}
self.symbol = symbol
self.start_date = sd
self.end_date = ed
for single_date in daterange(self.start_date, self.end_date):
single_date_str = single_date.strftime('%Y%m%d')
faw = FileAndWriter(self.symbol, single_date_str)
print 'FAW ', single_date_str
self.writers[single_date_str] = faw
def update(self, dataMsg):
try:
today = datetime.datetime.utcnow()
today_str = today.strftime('%Y%m%d')
print today_str
if today_str in self.writers:
#print 'Writting'
faw = self.writers[today_str]
if faw is not None:
faw.appendData(dataMsg)
except:
print "update error:", sys.exc_info()[0]
faw = self.writers[today_str]
update error: <type 'exceptions.TypeError'>
如何在字典中存储对象并调用成员函数?或者还有其他我没有注意到的东西?