forked from robcarver17/pysystemtrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroll_calendars.py
102 lines (78 loc) · 3.2 KB
/
roll_calendars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from sysobjects.roll_calendars import rollCalendar
from syslogdiag.log import logtoscreen
USE_CHILD_CLASS_ROLL_CALENDAR_ERROR = (
"You need to use a child class of rollCalendarData"
)
class rollCalendarData(object):
"""
Class to read / write roll calendars
We wouldn't normally use this base class, but inherit from it for a specific data source eg Arctic
"""
def __init__(self, log=logtoscreen):
self._log = log
@property
def log(self):
return self._log
def __repr__(self):
return "rollCalendarData base class - DO NOT USE"
def keys(self):
return self.get_list_of_instruments()
def __getitem__(self, instrument_code):
return self.get_roll_calendar(instrument_code)
def get_list_of_instruments(self):
raise NotImplementedError(USE_CHILD_CLASS_ROLL_CALENDAR_ERROR)
def get_roll_calendar(self, instrument_code):
if self.is_code_in_data(instrument_code):
return self._get_roll_calendar_without_checking(instrument_code)
else:
return rollCalendar.create_empty()
def _get_roll_calendar_without_checking(self, instrument_code):
raise NotImplementedError(USE_CHILD_CLASS_ROLL_CALENDAR_ERROR)
def delete_roll_calendar(self, instrument_code, are_you_sure=False):
self.log.label(instrument_code=instrument_code)
if are_you_sure:
if self.is_code_in_data(instrument_code):
self._delete_roll_calendar_data_without_any_warning_be_careful(
instrument_code
)
self.log.terse(
"Deleted roll calendar for %s" %
instrument_code)
else:
# doesn't exist anyway
self.log.warn(
"Tried to delete roll calendar for non existent instrument code %s" %
instrument_code)
else:
self.log.error(
"You need to call delete_roll_calendar with a flag to be sure"
)
def _delete_roll_calendar_data_without_any_warning_be_careful(self,
instrument_code):
raise NotImplementedError(USE_CHILD_CLASS_ROLL_CALENDAR_ERROR)
def is_code_in_data(self, instrument_code):
if instrument_code in self.get_list_of_instruments():
return True
else:
return False
def add_roll_calendar(
self, roll_calendar, instrument_code, ignore_duplication=False
):
self.log.label(instrument_code=instrument_code)
if self.is_code_in_data(instrument_code):
if ignore_duplication:
pass
else:
raise self.log.warn(
"There is already %s in the data, you have to delete it first" %
instrument_code)
self._add_roll_calendar_without_checking_for_existing_entry(
roll_calendar, instrument_code
)
self.log.terse(
"Added roll calendar for instrument %s" %
instrument_code)
def _add_roll_calendar_without_checking_for_existing_entry(
self, roll_calendar, instrument_code
):
raise NotImplementedError(USE_CHILD_CLASS_ROLL_CALENDAR_ERROR)