Conditions | 6 |
Total Lines | 65 |
Code Lines | 40 |
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 datetime import datetime, timezone |
||
10 | def default_object_serializer( |
||
11 | obj: object, |
||
12 | *, |
||
13 | key_transformer: Optional[Callable[[str], str]] = None, |
||
14 | strip_nulls: bool = False, |
||
15 | strip_privates: bool = False, |
||
16 | strip_properties: bool = False, |
||
17 | strip_class_variables: bool = False, |
||
18 | strip_attr: Union[str, MutableSequence[str], Tuple[str]] = None, |
||
19 | verbose: Union[Verbosity, bool] = False, |
||
20 | **kwargs) -> dict: |
||
21 | """ |
||
22 | Serialize the given ``obj`` to a dict. All values within ``obj`` are also |
||
23 | serialized. If ``key_transformer`` is given, it will be used to transform |
||
24 | the casing (e.g. snake_case) to a different format (e.g. camelCase). |
||
25 | :param obj: the object that is to be serialized. |
||
26 | :param key_transformer: a function that will be applied to all keys in the |
||
27 | resulting dict. |
||
28 | :param strip_nulls: if ``True`` the resulting dict will not contain null |
||
29 | values. |
||
30 | :param strip_privates: if ``True`` the resulting dict will not contain |
||
31 | private attributes (i.e. attributes that start with an underscore). |
||
32 | :param strip_properties: if ``True`` the resulting dict will not contain |
||
33 | values from @properties. |
||
34 | :param strip_class_variables: if ``True`` the resulting dict will not |
||
35 | contain values from class variables. |
||
36 | :param strip_attr: can be a name or a collection of names of attributes |
||
37 | that are not to be included in the dump. |
||
38 | dict will not contain attributes with |
||
39 | :param verbose: if ``True`` the resulting dict will contain meta |
||
40 | information (e.g. on how to deserialize). |
||
41 | :param kwargs: any keyword arguments that are to be passed to the |
||
42 | serializer functions. |
||
43 | :return: a Python dict holding the values of ``obj``. |
||
44 | """ |
||
45 | if obj is None: |
||
46 | return obj |
||
47 | strip_attr = strip_attr or [] |
||
48 | if (not isinstance(strip_attr, MutableSequence) |
||
49 | and not isinstance(strip_attr, tuple)): |
||
50 | strip_attr = (strip_attr,) |
||
51 | cls = kwargs['cls'] or obj.__class__ |
||
52 | obj_dict = _get_dict_from_obj(obj, strip_privates, strip_properties, |
||
53 | strip_class_variables, strip_attr, **kwargs) |
||
54 | kwargs_ = {**kwargs, 'verbose': verbose} |
||
55 | verbose = Verbosity.from_value(verbose) |
||
56 | if Verbosity.WITH_CLASS_INFO in verbose: |
||
57 | kwargs_['_store_cls'] = True |
||
58 | result = default_dict_serializer( |
||
59 | obj_dict, |
||
60 | key_transformer=key_transformer, |
||
61 | strip_nulls=strip_nulls, |
||
62 | strip_privates=strip_privates, |
||
63 | strip_properties=strip_properties, |
||
64 | strip_class_variables=strip_class_variables, |
||
65 | strip_attr=strip_attr, |
||
66 | **kwargs_) |
||
67 | cls_name = get_class_name(cls, fully_qualified=True, |
||
68 | fork_inst=kwargs['fork_inst']) |
||
69 | if kwargs.get('_store_cls'): |
||
70 | result['-cls'] = cls_name |
||
71 | else: |
||
72 | result = _get_dict_with_meta(result, cls_name, verbose, |
||
73 | kwargs['fork_inst']) |
||
74 | return result |
||
75 | |||
158 |