1 | """Context |
||
2 | |||
3 | A class to group occurrences and contextualize them. |
||
4 | |||
5 | https://github.com/rochars/trade |
||
6 | License: MIT |
||
7 | |||
8 | Copyright (c) 2015-2018 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 | from __future__ import absolute_import |
||
30 | from __future__ import division |
||
31 | |||
32 | import copy |
||
33 | |||
34 | |||
35 | class Context(object): |
||
0 ignored issues
–
show
Unused Code
introduced
by
Loading history...
|
|||
36 | """A class to group occurrences and contextualize them. |
||
37 | |||
38 | To contextualize a group of occurrences could be: |
||
39 | - To identify daytrades and separate them into different occurrences |
||
40 | for taxation purposes |
||
41 | - Group occurrences with the same subject into one occurrence |
||
42 | - Apply costs and other deductions to the value to the occurrences |
||
43 | - Calculate taxes based on the occurrences in the context |
||
44 | |||
45 | Objects from this class are created with both a list of occurrences and a |
||
46 | list of functions (called "context tasks"). |
||
47 | |||
48 | The context tasks are run by calling Context.apply(). Context tasks |
||
49 | run in the order they are listed. |
||
50 | |||
51 | Every context task function receives one argument: the context itself. It |
||
52 | is then free to manipulate the raw occurrence data stored in the |
||
53 | Context.occurrences list. |
||
54 | |||
55 | The contextualized occurrence data is then stored in the |
||
56 | Context.contextualized list. |
||
57 | |||
58 | The occurrences in Context.contextualized should be informed to a Holder |
||
59 | as regular occurrences. |
||
60 | |||
61 | The original Occurrence list is not modified. |
||
62 | |||
63 | Attributes: |
||
64 | occurrences: A list of Occurrence objects. |
||
65 | tasks: A list of functions. |
||
66 | contextualized: A list of Occurrence objects. |
||
67 | """ |
||
68 | |||
69 | def __init__(self, occurrences, tasks): |
||
70 | self.occurrences = occurrences |
||
71 | self.tasks = tasks |
||
72 | self.contextualized = [] |
||
73 | |||
74 | def apply(self): |
||
75 | """Apply the rules of the context to its occurrences. |
||
76 | |||
77 | This method executes all the functions defined in |
||
78 | self.tasks in the order they are listed. |
||
79 | |||
80 | Every function that acts as a context task receives the |
||
81 | Context object itself as its only argument. |
||
82 | |||
83 | The contextualized occurrences are then stored in |
||
84 | Context.contextualized. |
||
85 | |||
86 | The original Occurrence instances are not modified. |
||
87 | """ |
||
88 | raw_operations = copy.deepcopy(self.occurrences) |
||
89 | for task in self.tasks: |
||
90 | task(self) |
||
91 | self.occurrences = raw_operations |
||
92 |