Conditions | 18 |
Total Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 10 | ||
Bugs | 3 | Features | 2 |
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 _dict_to_rels_itr() 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 | # |
||
77 | def _dict_to_rels_itr(dic, rel_name, level=0, names=None): |
||
78 | """ |
||
79 | Convert nested dict[s] to tuples of relation name and relations of items in |
||
80 | the dict, and yields each pairs. |
||
81 | |||
82 | :param dic: A dict or dict-like object |
||
83 | :param rel_name: Name for relations of items in `dic` |
||
84 | :return: A list of (<relation_name>, [tuple of key and value]) |
||
85 | |||
86 | >>> list(_dict_to_rels_itr(dict(id=0, a=1, b="b"), "ab")) |
||
87 | [('ab', (('id', 0), ('a', 1), ('b', 'b')))] |
||
88 | |||
89 | >>> list(_dict_to_rels_itr(dict(id=0, a=dict(id=1, b=1), d="D"), |
||
90 | ... "A")) # doctest: +NORMALIZE_WHITESPACE |
||
91 | [('A', (('id', 0), ('d', 'D'))), |
||
92 | ('rel_A_a', (('a', 1), ('A', 0))), |
||
93 | ('a', (('id', 1), ('b', 1)))] |
||
94 | """ |
||
95 | if names is None: |
||
96 | names = [] |
||
97 | else: |
||
98 | names.append(rel_name) |
||
99 | |||
100 | lkeys = [k for k, v in dic.items() if m9dicts.utils.is_list_like(v)] |
||
101 | dkeys = [k for k, v in dic.items() if m9dicts.utils.is_dict_like(v)] |
||
102 | items = sorted((k, v) for k, v in dic.items() |
||
103 | if k != "id" and k not in lkeys and k not in dkeys) |
||
104 | pid = dic.get("id", _gen_id(*items)) |
||
105 | yield (rel_name, tuple([("id", pid)] + items)) |
||
106 | |||
107 | level += 1 |
||
108 | kwargs = dict(level=level, names=names) |
||
109 | if lkeys: |
||
110 | for key in sorted(lkeys): |
||
111 | for val in _sorted(dic[key]): |
||
112 | if m9dicts.utils.is_dict_like(val): |
||
113 | # :todo: Avoid name collision. |
||
114 | # if name in val: |
||
115 | # ... |
||
116 | for tpl in _dict_to_rels_itr_0(val, key, rel_name, pid, |
||
117 | **kwargs): |
||
118 | yield tpl |
||
119 | else: |
||
120 | cid = _gen_id(key, val) |
||
121 | name = _rel_name(rel_name, key, **kwargs) |
||
122 | yield (name, (("id", cid), (rel_name, pid), (key, val))) |
||
123 | |||
124 | if dkeys: |
||
125 | for key in sorted(dkeys): |
||
126 | for tpl in _dict_to_rels_itr_0(dic[key], key, rel_name, pid, |
||
127 | **kwargs): |
||
128 | yield tpl |
||
129 | |||
151 |