| Conditions | 10 |
| Total Lines | 78 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 15 |
| CRAP Score | 11.5625 |
| Changes | 5 | ||
| 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 Element.factory() 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 | """StarStruct element class.""" |
||
| 26 | 1 | @classmethod |
|
| 27 | 1 | def factory(cls, field: tuple, mode: Optional[Mode]=Mode.Native, alignment: Optional[int]=1): |
|
| 28 | """ |
||
| 29 | Initialize a StarStruct element object based on the type of element |
||
| 30 | parameters provided. |
||
| 31 | |||
| 32 | |||
| 33 | Where the values in the tuple determine the type of element. |
||
| 34 | |||
| 35 | These are the possible element types: |
||
| 36 | 1. Normal (base): a standard python struct format character, and a |
||
| 37 | field name are provided. The optional element should not provided. |
||
| 38 | |||
| 39 | 2. Enum: a standard python struct format character and field name are |
||
| 40 | provided, but the 3rd optional element is provided which is a |
||
| 41 | subclass of enum.Enum. |
||
| 42 | |||
| 43 | 3. Length: a standard python struct format character that represents |
||
| 44 | an unsigned numeric value, and the field name are provided, but the |
||
| 45 | 3rd optional element is provided and is a string. In this case the |
||
| 46 | string is assumed to be another field which is the name of a |
||
| 47 | Variable element. |
||
| 48 | |||
| 49 | 4. Variable: a variable length element that accommodates 0 or more of |
||
| 50 | another StarStruct.message. The format field should be a valid |
||
| 51 | StarStruct.message, the optional 3rd element must be provided and |
||
| 52 | should be the name of a valid Length element or an int. The |
||
| 53 | validity of the referenced element must be checked after the |
||
| 54 | creation of the entire message with the Message.validate() function. |
||
| 55 | |||
| 56 | 5. Discriminated: a message element that can have multiple formats |
||
| 57 | such as a C union. The format field should be a dictionary where |
||
| 58 | the keys represent values of a referenced enumeration field, and |
||
| 59 | the value for each entry is a valid StarStruct.message, or None. |
||
| 60 | The optional 3rd element must be provided and should be the name of |
||
| 61 | a valid Enum element. The validity of the referenced element must |
||
| 62 | be checked after the creation of the entire message with the |
||
| 63 | Message.validate() function. |
||
| 64 | |||
| 65 | |||
| 66 | :param field: The field must be a tuple of the following form:: |
||
| 67 | |||
| 68 | (name, format, <optional>) |
||
| 69 | |||
| 70 | :param mode: The mode in which to pack the information. |
||
| 71 | :param alignment: The number of bytes to align objects with. |
||
| 72 | :returns: An element whose fields match those passed in |
||
| 73 | """ |
||
| 74 | |||
| 75 | 1 | if not isinstance(mode, Mode): |
|
| 76 | raise TypeError('invalid mode: {}'.format(mode)) |
||
| 77 | |||
| 78 | # The field parameter is a single field tuple: |
||
| 79 | # ('name', 'format', <optional>) |
||
| 80 | 1 | if not isinstance(field, tuple): |
|
| 81 | raise TypeError('invalid element: {}'.format(field)) |
||
| 82 | |||
| 83 | # The name of the element must be a non-null string or bytes |
||
| 84 | # provided in as the first part of the field tuple |
||
| 85 | 1 | if not field[0] or not isinstance(field[0], (str, bytes)): |
|
| 86 | raise TypeError('invalid name: {}'.format(field[0])) |
||
| 87 | |||
| 88 | 1 | valid_elems = [] |
|
| 89 | 1 | for elem in cls.elementtypes: |
|
| 90 | 1 | try: |
|
| 91 | 1 | if elem.valid(field): |
|
| 92 | 1 | valid_elems.append(elem) |
|
| 93 | 1 | except (TypeError, KeyError): |
|
| 94 | 1 | continue |
|
| 95 | |||
| 96 | 1 | if len(valid_elems) > 1: |
|
| 97 | raise ValueError('More than one elemn was valid.\n\tField: {0}\n\tElems: {1}'.format( |
||
| 98 | field, valid_elems)) |
||
| 99 | 1 | elif len(valid_elems) == 1: |
|
| 100 | 1 | return valid_elems[0](field, mode, alignment) |
|
| 101 | |||
| 102 | # If the function made it this far, the field specification is not valid |
||
| 103 | raise TypeError('invalid field: {}'.format(field)) |
||
| 104 | |||
| 167 |
This can be caused by one of the following:
1. Missing Dependencies
This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.
2. Missing __init__.py files
This error could also result from missing
__init__.pyfiles in your module folders. Make sure that you place one file in each sub-folder.