Conditions | 3 |
Total Lines | 74 |
Code Lines | 26 |
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:
1 | """ |
||
25 | def load( |
||
26 | json_obj: object, |
||
27 | cls: Optional[Type[T]] = None, |
||
28 | *, |
||
29 | strict: bool = False, |
||
30 | fork_inst: Optional[type] = StateHolder, |
||
31 | attr_getters: Optional[Dict[str, Callable[[], object]]] = None, |
||
32 | **kwargs) -> T: |
||
33 | """ |
||
34 | Deserialize the given ``json_obj`` to an object of type ``cls``. If the |
||
35 | contents of ``json_obj`` do not match the interface of ``cls``, a |
||
36 | DeserializationError is raised. |
||
37 | |||
38 | If ``json_obj`` contains a value that belongs to a custom class, there must |
||
39 | be a type hint present for that value in ``cls`` to let this function know |
||
40 | what type it should deserialize that value to. |
||
41 | |||
42 | |||
43 | **Example**: |
||
44 | |||
45 | >>> from typing import List |
||
46 | >>> import jsons |
||
47 | >>> class Person: |
||
48 | ... # No type hint required for name |
||
49 | ... def __init__(self, name): |
||
50 | ... self.name = name |
||
51 | >>> class Family: |
||
52 | ... # Person is a custom class, use a type hint |
||
53 | ... def __init__(self, persons: List[Person]): |
||
54 | ... self.persons = persons |
||
55 | >>> loaded = jsons.load({'persons': [{'name': 'John'}]}, Family) |
||
56 | >>> loaded.persons[0].name |
||
57 | 'John' |
||
58 | |||
59 | If no ``cls`` is given, a dict is simply returned, but contained values |
||
60 | (e.g. serialized ``datetime`` values) are still deserialized. |
||
61 | |||
62 | If `strict` mode is off and the type of `json_obj` exactly matches `cls` |
||
63 | then `json_obj` is simply returned. |
||
64 | |||
65 | :param json_obj: the dict that is to be deserialized. |
||
66 | :param cls: a matching class of which an instance should be returned. |
||
67 | :param strict: a bool to determine if the deserializer should be strict |
||
68 | (i.e. fail on a partially deserialized `json_obj` or on `None`). |
||
69 | :param fork_inst: if given, it uses this fork of ``JsonSerializable``. |
||
70 | :param attr_getters: a ``dict`` that may hold callables that return values |
||
71 | for certain attributes. |
||
72 | :param kwargs: the keyword args are passed on to the deserializer function. |
||
73 | :return: an instance of ``cls`` if given, a dict otherwise. |
||
74 | """ |
||
75 | _check_for_none(json_obj, cls) |
||
76 | if _should_skip(json_obj, cls, strict): |
||
77 | validate(json_obj, cls, fork_inst) |
||
78 | return json_obj |
||
79 | if isinstance(cls, str): |
||
80 | cls = get_cls_from_str(cls, json_obj, fork_inst) |
||
81 | cls, meta_hints = _check_and_get_cls_and_meta_hints( |
||
82 | json_obj, cls, fork_inst, kwargs.get('_inferred_cls', False)) |
||
83 | |||
84 | deserializer = get_deserializer(cls, fork_inst) |
||
85 | |||
86 | # Is this the initial call or a nested? |
||
87 | initial = kwargs.get('_initial', True) |
||
88 | |||
89 | kwargs_ = { |
||
90 | 'strict': strict, |
||
91 | 'fork_inst': fork_inst, |
||
92 | 'attr_getters': attr_getters, |
||
93 | 'meta_hints': meta_hints, |
||
94 | '_initial': False, |
||
95 | **kwargs |
||
96 | } |
||
97 | |||
98 | return _do_load(json_obj, deserializer, cls, initial, **kwargs_) |
||
99 | |||
219 |