|
1
|
|
|
"""Holder |
|
2
|
|
|
|
|
3
|
|
|
Copyright (c) 2015-2018 Rafael da Silva Rocha |
|
4
|
|
|
|
|
5
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6
|
|
|
of this software and associated documentation files (the "Software"), to deal |
|
7
|
|
|
in the Software without restriction, including without limitation the rights |
|
8
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9
|
|
|
copies of the Software, and to permit persons to whom the Software is |
|
10
|
|
|
furnished to do so, subject to the following conditions: |
|
11
|
|
|
|
|
12
|
|
|
The above copyright notice and this permission notice shall be included in |
|
13
|
|
|
all copies or substantial portions of the Software. |
|
14
|
|
|
|
|
15
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
21
|
|
|
THE SOFTWARE. |
|
22
|
|
|
""" |
|
23
|
|
|
|
|
24
|
|
|
import copy |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
class Holder(object): |
|
|
|
|
|
|
29
|
|
|
"""A holder of subjects. |
|
30
|
|
|
|
|
31
|
|
|
It receives Occurrence objects and update its accumulators |
|
32
|
|
|
with them. The state of each subject in the Portfolio accumulators |
|
33
|
|
|
are updated according to the occurrence. |
|
34
|
|
|
|
|
35
|
|
|
Attributes: |
|
36
|
|
|
subjects: A dict {Subject.symbol: Accumulator}. |
|
37
|
|
|
""" |
|
38
|
|
|
|
|
39
|
|
|
def __init__(self, state=None): |
|
40
|
|
|
self.subjects = {} |
|
41
|
|
|
if state: |
|
42
|
|
|
for subject, subject_state in state.items(): |
|
43
|
|
|
self.subjects[subject.symbol] = Accumulator( |
|
44
|
|
|
subject, |
|
45
|
|
|
subject_state |
|
46
|
|
|
) |
|
47
|
|
|
|
|
48
|
|
|
def accumulate(self, occurrence): |
|
49
|
|
|
"""Update the state of the Portfolio with an occurrence.""" |
|
50
|
|
|
occurrence.update_portfolio(self) |
|
51
|
|
|
self.accumulate_occurrence(occurrence) |
|
52
|
|
|
|
|
53
|
|
|
def accumulate_occurrence(self, occurrence): |
|
54
|
|
|
"""Accumulates an occurrence on its corresponding accumulator.""" |
|
55
|
|
|
if occurrence.subject.symbol not in self.subjects: |
|
56
|
|
|
self.subjects[occurrence.subject.symbol] = Accumulator( |
|
57
|
|
|
occurrence.subject |
|
58
|
|
|
) |
|
59
|
|
|
self.subjects[occurrence.subject.symbol].accumulate(occurrence) |
|
60
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
class Accumulator(object): |
|
|
|
|
|
|
64
|
|
|
"""An accumulator of occurrences with an subject. |
|
65
|
|
|
|
|
66
|
|
|
Accumulators represent the situation of a subject belonging to a |
|
67
|
|
|
Holder. |
|
68
|
|
|
|
|
69
|
|
|
A Holder have multiple Accumulator instances on its subjects list, |
|
70
|
|
|
one for each subject the holder have. |
|
71
|
|
|
|
|
72
|
|
|
An Accumulator object accumualates occurrences of a single subject. |
|
73
|
|
|
|
|
74
|
|
|
Attributes: |
|
75
|
|
|
subject: An subject instance, the subject whose data is being |
|
76
|
|
|
accumulated. |
|
77
|
|
|
date: A string 'YYYY-mm-dd' representing the date of the last |
|
78
|
|
|
status change of the accumulator. |
|
79
|
|
|
state: A dictionary with the initial state of this accumulator. |
|
80
|
|
|
logging: A boolean indicating if the accumulator should log |
|
81
|
|
|
the data passed to accumulate(). |
|
82
|
|
|
log: A dict with all the occurrences performed with the subject, |
|
83
|
|
|
provided that self.logging is True. |
|
84
|
|
|
""" |
|
85
|
|
|
|
|
86
|
|
|
def __init__(self, subject, state=None, logging=True): |
|
87
|
|
|
self.log = {} |
|
88
|
|
|
self.date = None |
|
89
|
|
|
if state: |
|
90
|
|
|
self.set_initial_state(state) |
|
91
|
|
|
else: |
|
92
|
|
|
self.state = subject.get_default_state() |
|
93
|
|
|
self.subject = subject |
|
94
|
|
|
self.logging = logging |
|
95
|
|
|
|
|
96
|
|
|
def set_initial_state(self, state): |
|
97
|
|
|
"""Set the initial state of the Accumulator object.""" |
|
98
|
|
|
self.state = copy.deepcopy(state) |
|
99
|
|
|
if 'date' in state: |
|
100
|
|
|
self.set_initial_state_by_date(state) |
|
101
|
|
|
|
|
102
|
|
|
def set_initial_state_by_date(self, state): |
|
103
|
|
|
"""Set the initial state of the Accumulator object by date.""" |
|
104
|
|
|
self.state.pop('date', None) |
|
105
|
|
|
self.date = state['date'] |
|
106
|
|
|
self.log[state['date']] = {} |
|
107
|
|
|
for key in [x for x in state.keys() if x != 'date']: |
|
108
|
|
|
self.log[state['date']][key] = state[key] |
|
109
|
|
|
|
|
110
|
|
|
def accumulate(self, occurrence): |
|
111
|
|
|
"""Accumulates an occurrence.""" |
|
112
|
|
|
occurrence.update_accumulator(self) |
|
113
|
|
|
if self.logging: |
|
114
|
|
|
self.log_occurrence(occurrence) |
|
115
|
|
|
|
|
116
|
|
|
def log_occurrence(self, operation): |
|
117
|
|
|
"""Log the state of the accumulator. |
|
118
|
|
|
|
|
119
|
|
|
If logging, this method is called behind the scenes every |
|
120
|
|
|
time accumulate() is called. The states are logged by day |
|
121
|
|
|
like this: |
|
122
|
|
|
{ |
|
123
|
|
|
'YYYY-mm-dd': state, |
|
124
|
|
|
... |
|
125
|
|
|
} |
|
126
|
|
|
""" |
|
127
|
|
|
self.log[operation.date] = copy.deepcopy(self.state) |
|
128
|
|
|
|