Conditions | 14 |
Total Lines | 65 |
Code Lines | 50 |
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:
Complex classes like school_api.client.api.place_schedule.PlaceSchedule.get_schedule() 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.
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | # -*- coding: utf-8 -*- |
||
19 | def get_schedule(self, |
||
20 | campus_list=None, |
||
21 | building_list=None, |
||
22 | classroom_type_list=None, |
||
23 | classroom_name_list=None, |
||
24 | filter_campus_list=None, |
||
25 | filter_building_list=None, |
||
26 | filter_classroom_type_list=None, |
||
27 | **kwargs): |
||
28 | ''' 课表信息 获取入口 |
||
29 | 返回一个生成器,生成器一旦报错则将退出:https://codeday.me/bug/20180125/124136.html |
||
30 | 除了解析异常其他报错均不抛出 |
||
31 | ''' |
||
32 | self.schedule_url = self.school_url['PLACE_SCHEDULE_URL'] + \ |
||
33 | urlparse.quote(self.account.encode('gb2312')) |
||
34 | |||
35 | if not self._update_payload(**kwargs): |
||
36 | yield {'error': "获取教学场地课表失败"} |
||
37 | else: |
||
38 | # 遍历校区 |
||
39 | for campus in self.payload['campus_list']: |
||
40 | if self._is_skip(campus["name"], campus_list, filter_name_list=filter_campus_list): |
||
41 | continue |
||
42 | if not self._update_payload(campus, **kwargs): |
||
43 | continue |
||
44 | |||
45 | # 遍历楼号 |
||
46 | for building in self.payload['building_list']: |
||
47 | kwargs['building'] = building |
||
48 | if self._is_skip(building["name"], building_list, filter_name_list=filter_building_list): |
||
49 | continue |
||
50 | if not self._update_payload(campus, **kwargs): |
||
51 | continue |
||
52 | |||
53 | # 遍历教室类别 |
||
54 | for classroom_type in self.payload['classroom_type_list']: |
||
55 | kwargs['classroom_type'] = classroom_type |
||
56 | if self._is_skip(classroom_type["name"], classroom_type_list, filter_name_list=filter_classroom_type_list): |
||
57 | continue |
||
58 | if not self._update_payload(campus, **kwargs): |
||
59 | continue |
||
60 | # 遍历教室名称 |
||
61 | for classroom_name in self.payload['classroom_name_list']: |
||
62 | kwargs['classroom_name'] = classroom_name |
||
63 | if self._is_skip(classroom_name["name"], classroom_name_list): |
||
64 | continue |
||
65 | try: |
||
66 | res = self._get_api(campus, **kwargs) |
||
67 | except ScheduleException: |
||
68 | continue |
||
69 | |||
70 | schedule = ScheduleParse( |
||
71 | res.content.decode('GB18030'), |
||
72 | self.time_list, |
||
73 | ScheduleType.CLASS |
||
74 | ).get_schedule_dict() |
||
75 | |||
76 | data = { |
||
77 | "campus": campus["name"], |
||
78 | "building": building["name"], |
||
79 | "classroom_type": classroom_type["name"], |
||
80 | "classroom_name": classroom_name["name"] |
||
81 | } |
||
82 | data.update(schedule) |
||
83 | yield data |
||
84 | |||
158 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.