Conditions | 11 |
Total Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | Features | 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:
Complex classes like Simkl.mark_as_watched() often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | #!/usr/bin/python |
||
92 | def mark_as_watched(self, item): |
||
93 | if not item: return False |
||
94 | |||
95 | log("MARK: {0}".format(item)) |
||
96 | _watched_at = time.strftime('%Y-%m-%d %H:%M:%S') |
||
97 | _count = 0 |
||
98 | |||
99 | s_data = {} |
||
100 | if item["type"] == "episodes": |
||
101 | s_data[item["type"]] = [{ |
||
102 | "watched_at": _watched_at, |
||
103 | "ids": { |
||
104 | "simkl": item["simkl"] |
||
105 | } |
||
106 | }] |
||
107 | elif item["type"] == "shows": |
||
108 | # TESTED |
||
109 | s_data[item["type"]] = [{ |
||
110 | "title": item["title"], |
||
111 | "ids": { |
||
112 | "tvdb": item["tvdb"] |
||
113 | }, |
||
114 | "seasons": [{ |
||
115 | "number": item['season'], |
||
116 | "episodes": [{ |
||
117 | "number": item['episode'] |
||
118 | }] |
||
119 | }] |
||
120 | }] |
||
121 | elif item["type"] == "movies": |
||
122 | _prep = { |
||
123 | "title": item["title"], |
||
124 | "year": item["year"], |
||
125 | } |
||
126 | if "simkl" in item: |
||
127 | _prep["ids"] = {"simkl": item["simkl"]} |
||
128 | elif "imdb" in item: |
||
129 | _prep["ids"] = {"imdb": item["imdb"]} |
||
130 | |||
131 | s_data[item["type"]] = [_prep] |
||
132 | |||
133 | log("Send: {0}".format(json.dumps(s_data))) |
||
134 | while True and s_data: |
||
135 | r = self._http("/sync/history/", body=json.dumps(s_data), headers=self.headers) |
||
136 | |||
137 | #retry 3 times |
||
138 | if r is None: |
||
139 | _count += 1 |
||
140 | if _count <= 3: |
||
141 | notify(get_str(32029).format(_count)) |
||
142 | time.sleep(10) |
||
143 | continue |
||
144 | notify(get_str(32027)) |
||
145 | return False |
||
146 | break |
||
147 | return True |
||
148 | |||
162 |