| Conditions | 7 |
| Total Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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:
| 1 | import copy |
||
| 10 | def fill_settings(sections, acquire_settings, log_printer): |
||
| 11 | """ |
||
| 12 | Retrieves all bears and requests missing settings via the given |
||
| 13 | acquire_settings method. |
||
| 14 | |||
| 15 | :param sections: The sections to fill up, modified in place. |
||
| 16 | :param acquire_settings: The method to use for requesting settings. It will |
||
| 17 | get a parameter which is a dictionary with the |
||
| 18 | settings name as key and a list containing a |
||
| 19 | description in [0] and the names of the bears |
||
| 20 | who need this setting in all following indexes. |
||
| 21 | :param log_printer: The log printer to use for logging. |
||
| 22 | :return: A tuple containing (local_bears, global_bears), |
||
| 23 | each of them being a dictionary with the section |
||
| 24 | name as key and as value the bears as a list. |
||
| 25 | """ |
||
| 26 | local_bears = {} |
||
| 27 | global_bears = {} |
||
| 28 | |||
| 29 | bears_by_section = {} |
||
| 30 | bear_dirs_by_section = {} |
||
| 31 | |||
| 32 | for section_name, section in sections.items(): |
||
| 33 | bear_dirs_by_section[section_name] = section.bear_dirs() |
||
| 34 | bears_by_section[section_name] = list(section.get("bears", "")) |
||
| 35 | |||
| 36 | bears_to_collect = set(chain.from_iterable(bears_by_section.values())) |
||
| 37 | bear_dirs_to_collect = set(chain(*bear_dirs_by_section.values())) |
||
| 38 | |||
| 39 | all_local_bears, all_global_bears = collect_bears( |
||
| 40 | bear_dirs_to_collect, |
||
| 41 | bears_to_collect, |
||
| 42 | [BEAR_KIND.LOCAL, BEAR_KIND.GLOBAL], |
||
| 43 | log_printer) |
||
| 44 | all_bears = copy.deepcopy(all_local_bears) |
||
| 45 | all_bears.extend(all_global_bears) |
||
| 46 | |||
| 47 | for section_name, section in sections.items(): |
||
| 48 | section_bears = [] |
||
| 49 | section_local_bears = [] |
||
| 50 | section_global_bears = [] |
||
| 51 | |||
| 52 | for bear in all_bears: |
||
| 53 | if bear.name in bears_by_section[section_name]: |
||
| 54 | section_bears.append(bear) |
||
| 55 | |||
| 56 | if bear.kind == BEAR_KIND.LOCAL: |
||
| 57 | section_local_bears.append(bear) |
||
| 58 | elif bear.kind == BEAR_KIND.GLOBAL: |
||
| 59 | section_global_bears.append(bear) |
||
| 60 | fill_section(section, acquire_settings, log_printer, section_bears) |
||
| 61 | |||
| 62 | local_bears[section_name] = section_local_bears |
||
| 63 | global_bears[section_name] = section_global_bears |
||
| 64 | |||
| 65 | return local_bears, global_bears |
||
| 66 | |||
| 111 |