Passed
Push — master ( 2f219c...d7053c )
by Rafael S.
01:30
created

Context.apply()   A

Complexity

Conditions 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
dl 0
loc 18
rs 9.4285
c 1
b 0
f 0
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
The variable __class__ seems to be unused.
Loading history...
36
    """A class to group occurrences and contextualize them.
37
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
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
64
    def __init__(self, occurrences, tasks):
65
        self.occurrences = occurrences
66
        self.tasks = tasks
67
        self.contextualized = []
68
69
    def apply(self):
70
        """Apply the rules of the context to its occurrences.
71
    
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
72
        This method executes all the functions defined in
73
        self.tasks in the order they are listed.
74
75
        Every function that acts as a context task receives the
76
        Context object itself as its only argument.
77
78
        The contextualized occurrences are then stored in
79
        Context.contextualized.
80
81
        The original Occurrence instances are not modified.
82
        """
83
        raw_operations = copy.deepcopy(self.occurrences)
84
        for task in self.tasks:
85
            task(self)
86
        self.occurrences = raw_operations
87