Conditions | 6 |
Total Lines | 53 |
Code Lines | 31 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | from typing import Callable, Dict, Optional, Tuple |
||
7 | def default_dict_serializer( |
||
8 | obj: dict, |
||
9 | cls: Optional[type] = None, |
||
10 | *, |
||
11 | strict: bool = False, |
||
12 | strip_nulls: bool = False, |
||
13 | key_transformer: Optional[Callable[[str], str]] = None, |
||
14 | types: Optional[Dict[str, type]] = None, |
||
15 | **kwargs) -> dict: |
||
16 | """ |
||
17 | Serialize the given ``obj`` to a dict of serialized objects. |
||
18 | :param obj: the dict that is to be serialized. |
||
19 | :param cls: not used. |
||
20 | :param strict: if ``True`` the serialization will raise upon any the |
||
21 | failure of any attribute. Otherwise it continues with a warning. |
||
22 | :param strict: a bool to determine if the serializer should be strict |
||
23 | (i.e. only dumping stuff that is known to ``cls``). |
||
24 | :param strip_nulls: if ``True`` the resulting dict will not contain null |
||
25 | values. |
||
26 | :param key_transformer: a function that will be applied to all keys in the |
||
27 | resulting dict. |
||
28 | :param types: a ``dict`` with attribute names (keys) and their types |
||
29 | (values). |
||
30 | :param kwargs: any keyword arguments that may be given to the serialization |
||
31 | process. |
||
32 | :return: a dict of which all elements are serialized. |
||
33 | """ |
||
34 | result = dict() |
||
35 | types = types or dict() |
||
36 | for key in obj: |
||
37 | obj_ = obj[key] |
||
38 | cls_ = types.get(key, None) |
||
39 | |||
40 | # If key is not a valid json type, use the hash as key and store the |
||
41 | # original key in a separate section. |
||
42 | dict_and_key = _store_and_hash(result, key, |
||
43 | key_transformer=key_transformer, |
||
44 | strip_nulls=strip_nulls, strict=strict, |
||
45 | types=types, **kwargs) |
||
46 | if dict_and_key: |
||
47 | result, key = dict_and_key |
||
48 | |||
49 | dumped_elem = dump(obj_, |
||
50 | cls=cls_, |
||
51 | key_transformer=key_transformer, |
||
52 | strip_nulls=strip_nulls, |
||
53 | strict=strict, |
||
54 | **kwargs) |
||
55 | if not (strip_nulls and dumped_elem is None): |
||
56 | if key_transformer: |
||
57 | key = key_transformer(key) |
||
58 | result[key] = dumped_elem |
||
59 | return result |
||
60 | |||
86 |