Completed
Push — master ( 7d146f...c413e2 )
by Rafael S.
01:21
created

Accumulator.__init__()   A

Complexity

Conditions 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 9
rs 9.6666
c 8
b 0
f 0
1
"""Accumulator
2
3
A framework for the accumulation of occurrences with subjects.
4
5
https://github.com/rochars/accumulator
6
License: MIT
7
8
Copyright (c) 2015-2017 Rafael da Silva Rocha
9
10
Permission is hereby granted, free of charge, to any person obtaining a copy
11
of this software and associated documentation files (the "Software"), to deal
12
in the Software without restriction, including without limitation the rights
13
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
copies of the Software, and to permit persons to whom the Software is
15
furnished to do so, subject to the following conditions:
16
17
The above copyright notice and this permission notice shall be included in
18
all copies or substantial portions of the Software.
19
20
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26
THE SOFTWARE.
27
"""
28
29
import copy
30
31
32
class Subject(object):
33
    """A subject of an occurrence.
34
35
    Attributes:
36
        symbol: A string representing the symbol of the subject.
37
        name: A string representing the name of the subject.
38
        expiration_date: A string 'YYYY-mm-dd' representing the
39
            expiration date of the subject, if any.
40
        default_state: a dictionary with the default state
41
            of this subject.
42
    """
43
44
    default_state = {}
45
46
    def __init__(self, symbol=None, name=None, expiration_date=None):
47
        self.symbol = symbol
48
        self.name = name
49
        self.expiration_date = expiration_date
50
51
    def get_default_state(self):
52
        """Returns the default state of the subject class.
53
54
        Every time an Accumulator object is created it calls this
55
        method from the Asset object it is accumulating data from.
56
        """
57
        return copy.deepcopy(self.default_state)
58
59
    def expire(self, accumulator):
60
        """Updates the accumulator with the expiration of this subject."""
61
        accumulator.state = copy.deepcopy(self.default_state)
62
63
64
class Occurrence(object):
65
    """An occurrence with an subject in a date.
66
67
    Occurrences are events involving an subject.
68
69
    Attributes:
70
        subject: An Asset object.
71
        date: A string 'YYYY-mm-dd'.
72
    """
73
74
    def __init__(self, subject, date):
75
        self.subject = subject
76
        self.date = date
77
78
    def update_portfolio(self, portfolio):
79
        """Should udpate the portfolio state.
80
81
        This is method is called by the Portfolio object when it accumulates
82
        the Occurrence. It is called before the accumulation occurs, so the
83
        Occurrence is free to manipulate the Portfolio data before it is passed
84
        to its subject corresponding Accumulator.
85
        """
86
        pass
87
88
    def update_accumulator(self, accumulator):
89
        """Should udpate the accumulator state.
90
91
        This method is called before the Accumulator log its current state.
92
        """
93
        pass
94
95
96
class Accumulator(object):
97
    """An accumulator of occurrences with an subject.
98
99
    It can accumulate a series of occurrence objects and update its
100
    state based on the occurrences it accumulates.
101
102
    The update of the accumulator object state is responsibility
103
    of the occurrence it accumulates.
104
105
    It accumualates occurrences of a single subject.
106
107
    Attributes:
108
        subject: An subject instance, the subject whose data is being
109
            accumulated.
110
        date: A string 'YYYY-mm-dd' representing the date of the last
111
            status change of the accumulator.
112
        state: A dictionary with the initial state of this accumulator.
113
        logging: A boolean indicating if the accumulator should log
114
            the data passed to accumulate().
115
        log: A dict with all the occurrences performed with the subject,
116
            provided that self.logging is True.
117
    """
118
119
    def __init__(self, subject, state=None, logging=True):
120
        self.log = {}
121
        self.date = None
122
        if state:
123
            self.set_initial_state(state)
124
        else:
125
            self.state = subject.get_default_state()
126
        self.subject = subject
127
        self.logging = logging
128
129
    def set_initial_state(self, state):
130
        """Set the initial state of the Accumulator object."""
131
        self.state = copy.deepcopy(state)
132
        if 'date' in state:
133
            self.state.pop('date', None)
134
            self.date = state['date']
135
            self.log[state['date']] = {}
136
            for key in [x for x in state.keys() if x != 'date']:
137
                self.log[state['date']][key] = state[key]
138
139
140
    def accumulate(self, occurrence):
141
        """Accumulates an occurrence."""
142
        occurrence.update_accumulator(self)
143
        if self.logging:
144
            self.log_occurrence(occurrence)
145
146
    def log_occurrence(self, operation):
147
        """Log the state of the accumulator.
148
149
        If logging, this method is called behind the scenes every
150
        time accumulate() is called. The states are logged by day
151
        like this:
152
        {
153
            'YYYY-mm-dd': state,
154
            ...
155
        }
156
        """
157
        self.log[operation.date] = copy.deepcopy(self.state)
158
159
160
class Portfolio(object):
161
    """A portfolio of subject accumulators.
162
163
    It receives Occurrence objects and update its accumulators
164
    with them.
165
166
    Attributes:
167
        subjects: A dict {Asset.symbol: Accumulator}.
168
    """
169
170
    def __init__(self, state=None):
171
        self.subjects = {}
172
        if state:
173
            for subject, subject_state in state.items():
174
                self.subjects[subject.symbol] = Accumulator(
175
                    subject,
176
                    subject_state
177
                )
178
179
    def accumulate(self, occurrence):
180
        """Update the state of the Portfolio with an occurrence."""
181
        occurrence.update_portfolio(self)
182
        self.accumulate_occurrence(occurrence)
183
184
    def accumulate_occurrence(self, occurrence):
185
        """Accumulates an occurrence on its corresponding accumulator."""
186
        if occurrence.subject.symbol not in self.subjects:
187
            self.subjects[occurrence.subject.symbol] = Accumulator(
188
                occurrence.subject
189
            )
190
        self.subjects[occurrence.subject.symbol].accumulate(occurrence)
191