Conditions | 5 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 30 |
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 | from plugin.core.helpers.variable import to_integer, merge |
||
67 | def to_dict(self, obj, info, fetch=False): |
||
68 | view_offset = to_integer(info.get('time')) |
||
69 | rating_key = info.get('ratingKey') |
||
70 | |||
71 | result = { |
||
72 | 'view_offset': view_offset, |
||
73 | |||
74 | 'updated_at': datetime.utcnow() |
||
75 | } |
||
76 | |||
77 | if not fetch: |
||
78 | # Return simple update |
||
79 | return merge(result, { |
||
80 | 'progress': self.get_progress(obj.duration, view_offset) |
||
81 | }) |
||
82 | |||
83 | # Retrieve session |
||
84 | # Retrieve metadata and guid |
||
85 | p_metadata, guid = self.get_metadata(rating_key) |
||
86 | |||
87 | if not p_metadata: |
||
88 | log.warn('Unable to retrieve metadata for rating_key %r', rating_key) |
||
89 | return result |
||
90 | |||
91 | if not guid: |
||
92 | return merge(result, { |
||
93 | 'duration': p_metadata.duration, |
||
94 | 'progress': self.get_progress(p_metadata.duration, view_offset) |
||
95 | }) |
||
96 | |||
97 | try: |
||
98 | # Create/Retrieve `Client` for session |
||
99 | result['client'] = ClientManager.get.or_create({ |
||
100 | 'key': info.get('machineIdentifier'), |
||
101 | 'title': info.get('client') |
||
102 | }, fetch=True) |
||
103 | |||
104 | # Create/Retrieve `User` for session |
||
105 | result['user'] = UserManager.get.or_create({ |
||
106 | 'key': to_integer(info.get('user_id')), |
||
107 | 'title': info.get('user_name') |
||
108 | }, fetch=True) |
||
109 | |||
110 | # Pick account from `client` or `user` objects |
||
111 | result['account'] = self.get_account(result) |
||
112 | except FilteredException: |
||
113 | log.debug('Activity has been filtered') |
||
114 | |||
115 | result['client'] = None |
||
116 | result['user'] = None |
||
117 | |||
118 | result['account'] = None |
||
119 | |||
120 | return merge(result, { |
||
121 | 'duration': p_metadata.duration, |
||
122 | 'progress': self.get_progress(p_metadata.duration, view_offset) |
||
123 | }) |
||
131 |