| Conditions | 16 |
| Total Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 PortRange.parse() 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 | # -*- coding: utf-8 -*- |
||
| 64 | def parse(self, port_range): |
||
| 65 | """ Parse and normalize a string or iterable into a port range. """ |
||
| 66 | # Any string containing a CIDR separator is parsed as a CIDR-like |
||
| 67 | # notation, others as a range or single port. |
||
| 68 | cidr_notation = False |
||
| 69 | if isinstance(port_range, basestring): |
||
| 70 | cidr_notation = self.CIDR_SEP in port_range |
||
| 71 | separator = self.CIDR_SEP if cidr_notation else self.RANGE_SEP |
||
| 72 | port_range = port_range.split(separator, 1) |
||
| 73 | |||
| 74 | # We expect here a list of elements castable to integers. |
||
| 75 | if not isinstance(port_range, Iterable): |
||
| 76 | port_range = [port_range] |
||
| 77 | try: |
||
| 78 | port_range = list(iter_map(int, port_range)) |
||
| 79 | except TypeError: |
||
| 80 | raise ValueError("Can't parse range as a list of integers.") |
||
| 81 | |||
| 82 | # At this point we should have a list of one or two integers. |
||
| 83 | if not 0 < len(port_range) < 3: |
||
| 84 | raise ValueError("Expecting a list of one or two elements.") |
||
| 85 | |||
| 86 | # Transform CIDR notation into a port range and validates it. |
||
| 87 | if cidr_notation: |
||
| 88 | base, prefix = port_range |
||
| 89 | port_range = self._cidr_to_range(base, prefix) |
||
| 90 | |||
| 91 | # Let the parser fix a reverse-ordered range in non-strict mode. |
||
| 92 | if not self.strict: |
||
| 93 | port_range.sort() |
||
| 94 | |||
| 95 | # Get port range bounds. |
||
| 96 | port_from = port_range[0] |
||
| 97 | # Single port gets their upper bound set to None. |
||
| 98 | port_to = port_range[1] if len(port_range) == 2 else None |
||
| 99 | |||
| 100 | # Validate constraints in strict mode. |
||
| 101 | if self.strict: |
||
| 102 | # Disallow out-of-bounds values. |
||
| 103 | if not (self.port_min <= port_from <= self.port_max) or ( |
||
| 104 | port_to is not None and not ( |
||
| 105 | self.port_min <= port_to <= self.port_max)): |
||
| 106 | raise ValueError("Out of bounds.") |
||
| 107 | # Disallow reversed range. |
||
| 108 | if port_to is not None and port_from > port_to: |
||
| 109 | raise ValueError("Invalid reversed port range.") |
||
| 110 | |||
| 111 | # Clamp down lower bound, then cap it. |
||
| 112 | port_from = min([max([port_from, self.port_min]), self.port_max]) |
||
| 113 | |||
| 114 | # Single port gets its upper bound aligned to its lower one. |
||
| 115 | if port_to is None: |
||
| 116 | port_to = port_from |
||
| 117 | |||
| 118 | # Cap upper bound. |
||
| 119 | port_to = min([port_to, self.port_max]) |
||
| 120 | |||
| 121 | return port_from, port_to |
||
| 122 | |||
| 234 |