| Conditions | 17 |
| Total Lines | 66 |
| Code Lines | 52 |
| 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 tests.functions.print_data_by_word() 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 -*- |
||
| 9 | def print_data_by_word(name: str): |
||
| 10 | |||
| 11 | # get cpx |
||
| 12 | word = Word.by_name(name).first() |
||
| 13 | sources = word.parents |
||
| 14 | |||
| 15 | affixes = [] |
||
| 16 | definitions = [] |
||
| 17 | types = [] |
||
| 18 | events = [] |
||
| 19 | authors = [] |
||
| 20 | keys = [] |
||
| 21 | |||
| 22 | relationship_authors = [] |
||
| 23 | relationship_keys = [] |
||
| 24 | relationship_words = [] |
||
| 25 | |||
| 26 | for s in sources: |
||
| 27 | for a in s.affixes: |
||
| 28 | relationship_words.append((s.id, a.id)) |
||
| 29 | affixes.append(a) |
||
| 30 | relationship_words.append((s.id, word.id)) |
||
| 31 | words = [word, ] + sources + affixes |
||
| 32 | |||
| 33 | for w in words: |
||
| 34 | for d in w.definitions: |
||
| 35 | definitions.append(d) |
||
| 36 | |||
| 37 | for a in w.authors: |
||
| 38 | relationship_authors.append((a.id, w.id)) |
||
| 39 | if a not in authors: |
||
| 40 | authors.append(a) |
||
| 41 | |||
| 42 | if w.type not in types: |
||
| 43 | types.append(w.type) |
||
| 44 | |||
| 45 | if w.event_start not in events and w.event_start: |
||
| 46 | events.append(w.event_start) |
||
| 47 | |||
| 48 | if w.event_end not in events and w.event_end: |
||
| 49 | events.append(w.event_end) |
||
| 50 | |||
| 51 | for d in definitions: |
||
| 52 | for k in d.keys: |
||
| 53 | relationship_keys.append((k.id, d.id)) |
||
| 54 | if k not in keys: |
||
| 55 | keys.append(k) |
||
| 56 | |||
| 57 | all_objects = [ |
||
| 58 | (Key, keys), (Event, events), (Author, authors), |
||
| 59 | (Type, types), (Word, words), (Definition, definitions), ] |
||
| 60 | |||
| 61 | for type_object, objects in all_objects: |
||
| 62 | type_object = str(type_object.__name__).lower() |
||
| 63 | print(f"# {'='*5} {type_object.upper()}S {'='*(70-len(type_object))}") |
||
| 64 | for i, o in enumerate(objects, 1): |
||
| 65 | print(f"{type_object}_{i} = {o.__str__()} ") |
||
| 66 | list_of_items = [type_object + '_' + str(i) for i in range(1, len(objects) + 1)] |
||
| 67 | print(f"{type_object}s = {list_of_items}\n".replace("'", "")) |
||
| 68 | |||
| 69 | types = str([(to.__name__, to.__name__.lower() + 's') for to, _ in all_objects]).replace("'", "") |
||
| 70 | print(f"all_objects = {types}\n") |
||
| 71 | print(f"# {'=' * 5} CONNECTIONS {'=' * (71 - len('CONNECTIONS'))}") |
||
| 72 | print(f"connect_authors = {relationship_authors} # (AID, WID)") |
||
| 73 | print(f"connect_keys = {relationship_keys} # (KID, DID)") |
||
| 74 | print(f"connect_words = {relationship_words} # (parent_id, child_id)") |
||
| 75 | |||
| 119 |