| Conditions | 19 |
| Total Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
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 Event.matches() 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 | import re |
||
| 71 | def matches(self, line): |
||
| 72 | """ |
||
| 73 | Fills out the event object per line, and returns True or False if the line matches one of our definitions. |
||
| 74 | Args: |
||
| 75 | line: string. The entire incoming line. |
||
| 76 | |||
| 77 | Return: |
||
| 78 | boolean; True or False. |
||
| 79 | """ |
||
| 80 | # perhaps TODO |
||
| 81 | # first try very simply |
||
| 82 | if len(self.definition) and self.definition in line: |
||
| 83 | return True |
||
| 84 | # grab message id. not always present |
||
| 85 | try: |
||
| 86 | temp = line.split(":")[1].split(" ")[1] |
||
| 87 | except IndexError: |
||
| 88 | pass |
||
| 89 | |||
| 90 | if len(self.mode): |
||
| 91 | try: |
||
| 92 | split_line = line.split() |
||
| 93 | temp_verb = split_line[1] # first nick, then verb |
||
| 94 | if self.mode == temp_verb.strip(): |
||
| 95 | return True |
||
| 96 | except IndexError: |
||
| 97 | pass |
||
| 98 | |||
| 99 | try: |
||
| 100 | message_id = int(temp) |
||
| 101 | except (ValueError, UnboundLocalError): |
||
| 102 | message_id = 0 |
||
| 103 | |||
| 104 | try: |
||
| 105 | msg = line.split(":",2)[2] |
||
| 106 | except IndexError: |
||
| 107 | return |
||
| 108 | |||
| 109 | if len(self.msg_definition): |
||
| 110 | if re.search(self.msg_definition, msg): |
||
| 111 | return True |
||
| 112 | |||
| 113 | if len(self.definition): |
||
| 114 | if re.search(self.definition, line): |
||
| 115 | return True |
||
| 116 | |||
| 117 | if len(self.user_definition): |
||
| 118 | if len(line) and "PRIVMSG" in line > 0: |
||
| 119 | line_array = line.split() |
||
| 120 | user_and_mask = line_array[0][1:] |
||
| 121 | user = user_and_mask.split("!")[0] |
||
| 122 | if self.user_definition == user: |
||
| 123 | return True |
||
| 124 | |||
| 125 | if type(self.message_id) is int: |
||
| 126 | if self.message_id == message_id: |
||
| 127 | return True |
||
| 128 | |||
| 129 | return False |
||
| 130 | |||
| 180 |