Conditions | 5 |
Total Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | """Occurrence |
||
49 | def update_holder(self, holder): |
||
50 | """Udpate the Holder state according to the occurrence. |
||
51 | |||
52 | This implementation is a example of how a Occurrence object |
||
53 | can update the Holder state; this method should be overriden |
||
54 | by classes that inherit from the Occurrence class. |
||
55 | |||
56 | This sample implementation simply update the quantity and the average |
||
57 | price of the Subject in the Holder's possession every time objects |
||
58 | from this class are passed to Holder.trade(). |
||
59 | |||
60 | This sample implementation considers the following signature for |
||
61 | the Holder.state dictionary: |
||
62 | { |
||
63 | "SUBJECT SYMBOL": { |
||
64 | "quantity": 0, |
||
65 | "value": 0 |
||
66 | }, |
||
67 | ... |
||
68 | } |
||
69 | """ |
||
70 | |||
71 | subject_symbol = self.subject.symbol |
||
72 | |||
73 | # If the Holder already have a state regarding this Subject, |
||
74 | # update that state |
||
75 | if subject_symbol in holder.state: |
||
76 | |||
77 | # If the Holder have zero units of this subject, the average |
||
78 | # value paid/received for the subject is the value of the trade itself |
||
79 | if not holder.state[subject_symbol]['quantity']: |
||
80 | holder.state[subject_symbol]['value'] = self.details['value'] |
||
81 | |||
82 | # If the Holder owns units of this subject then the average value |
||
83 | # paid/received for the subject may need to be updated with |
||
84 | # this occurrence details |
||
85 | elif same_sign( |
||
86 | holder.state[subject_symbol]['quantity'], |
||
87 | self.details['quantity']): |
||
88 | holder.state[subject_symbol]['value'] = average_price( |
||
89 | holder.state[subject_symbol]['quantity'], |
||
90 | holder.state[subject_symbol]['value'], |
||
91 | self.details['quantity'], |
||
92 | self.details['value'] |
||
93 | ) |
||
94 | |||
95 | # Update the quantity of the subject in the Holder's posession |
||
96 | holder.state[subject_symbol]['quantity'] += self.details['quantity'] |
||
97 | |||
98 | # If the Holder don't have a state with this occurrence's Subject, |
||
99 | # then register this occurrence as the first state of the Subject |
||
100 | # in the Holder's possession |
||
101 | else: |
||
102 | holder.state[subject_symbol] = { |
||
103 | 'quantity': self.details['quantity'], |
||
104 | 'value': self.details['value'] |
||
105 | } |
||
106 | |||
107 | # If the Holder knows about this Subject but don't have any unit |
||
108 | # of it, the paid value of the subject in the Holder state should |
||
109 | # be zero. |
||
110 | if not holder.state[subject_symbol]['quantity']: |
||
111 | holder.state[subject_symbol]['value'] = 0 |
||
112 | |||
121 |