Conditions | 19 |
Total Lines | 82 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | 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 ListMessages() 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 | from core.helpers import catch_errors, pad_title, try_convert, redirect |
||
30 | @route(PLUGIN_PREFIX + '/messages/list') |
||
31 | @catch_errors |
||
32 | def ListMessages(days=14, version='latest', viewed=False, *args, **kwargs): |
||
33 | # Cast `viewed` to boolean |
||
34 | if type(viewed) is str: |
||
35 | if viewed == 'None': |
||
36 | viewed = None |
||
37 | else: |
||
38 | viewed = viewed == 'True' |
||
39 | |||
40 | # Retrieve messages |
||
41 | messages = list(List( |
||
42 | days=try_convert(days, int), |
||
43 | version=version, |
||
44 | viewed=viewed |
||
45 | ).order_by( |
||
46 | Message.last_logged_at.desc() |
||
47 | ).limit(50)) |
||
48 | |||
49 | total_messages = List( |
||
50 | days=try_convert(days, int), |
||
51 | version=version, |
||
52 | ).count() |
||
53 | |||
54 | # Construct container |
||
55 | oc = ObjectContainer( |
||
56 | title2=_("Messages") |
||
57 | ) |
||
58 | |||
59 | # Add "Dismiss All" button |
||
60 | if viewed is False and len(messages) > 1: |
||
61 | oc.add(DirectoryObject( |
||
62 | key=Callback(DismissMessages), |
||
63 | title=pad_title(_("Dismiss all")) |
||
64 | )) |
||
65 | |||
66 | # Add interface messages |
||
67 | for record in InterfaceMessages.records: |
||
68 | # Pick object thumb |
||
69 | if record.level >= logging.WARNING: |
||
70 | thumb = R("icon-error.png") |
||
71 | else: |
||
72 | thumb = R("icon-notification.png") |
||
73 | |||
74 | # Add object |
||
75 | oc.add(DirectoryObject( |
||
76 | key=PLUGIN_PREFIX + '/messages/list', |
||
77 | title=pad_title('[%s] %s' % (logging.getLevelName(record.level).capitalize(), record.message)), |
||
78 | thumb=thumb |
||
79 | )) |
||
80 | |||
81 | # Add stored messages |
||
82 | for m in messages: |
||
83 | if m.type is None or\ |
||
84 | m.summary is None: |
||
85 | continue |
||
86 | |||
87 | # Pick thumb |
||
88 | if m.type == Message.Type.Exception: |
||
89 | thumb = R("icon-exception-viewed.png") if m.viewed else R("icon-exception.png") |
||
90 | elif m.type == Message.Type.Info: |
||
91 | thumb = R("icon-notification-viewed.png") if m.viewed else R("icon-notification.png") |
||
92 | elif m.type in CONNECTION_TYPES: |
||
93 | thumb = R("icon-connection-viewed.png") if m.viewed else R("icon-connection.png") |
||
94 | else: |
||
95 | thumb = R("icon-error-viewed.png") if m.viewed else R("icon-error.png") |
||
96 | |||
97 | # Add object |
||
98 | oc.add(DirectoryObject( |
||
99 | key=Callback(ViewMessage, error_id=m.id), |
||
100 | title=pad_title('[%s] %s' % (Message.Type.title(m.type), m.summary)), |
||
101 | thumb=thumb |
||
102 | )) |
||
103 | |||
104 | # Append "View All" button |
||
105 | if len(messages) != 50 and len(messages) < total_messages: |
||
106 | oc.add(DirectoryObject( |
||
107 | key=Callback(ListMessages, days=None, viewed=None), |
||
108 | title=pad_title(_("View All")) |
||
109 | )) |
||
110 | |||
111 | return oc |
||
112 | |||
290 |
It is generally discouraged to redefine built-ins as this makes code very hard to read.