Conditions | 1 |
Total Lines | 51 |
Code Lines | 22 |
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 | # SPDX-FileCopyrightText: Copyright 2020-2023, Contributors to typed-dfs |
||
41 | def test_to_json(self): |
||
42 | assert JsonTools.encoder().as_str("hi") == '"hi"\n' |
||
43 | assert JsonTools.encoder().as_str(["hi", "bye"]) == '[\n "hi",\n "bye"\n]\n' |
||
44 | data = { |
||
45 | "list": [ |
||
46 | { |
||
47 | "numbers": { |
||
48 | 1: np.asarray([float("inf"), 0]), |
||
49 | 2: np.asarray([1, 1]), |
||
50 | 3: np.half(float("inf")), |
||
51 | 4: np.half(float("-inf")), |
||
52 | 5: float("inf"), |
||
53 | 6: float("-inf"), |
||
54 | 7: 1, |
||
55 | }, |
||
56 | }, |
||
57 | ], |
||
58 | } |
||
59 | encoder = JsonTools.encoder( |
||
60 | inf_handling=NanInfHandling.convert_to_str, |
||
61 | nan_handling=NanInfHandling.convert_to_str, |
||
62 | ) |
||
63 | x = encoder.as_str(data) |
||
64 | assert ( |
||
65 | x |
||
66 | == inspect.cleandoc( |
||
67 | """ |
||
68 | { |
||
69 | "list": [ |
||
70 | { |
||
71 | "numbers": { |
||
72 | "1": [ |
||
73 | "inf", |
||
74 | "0.0" |
||
75 | ], |
||
76 | "2": [ |
||
77 | 1, |
||
78 | 1 |
||
79 | ], |
||
80 | "3": "inf", |
||
81 | "4": "-inf", |
||
82 | "5": "inf", |
||
83 | "6": "-inf", |
||
84 | "7": 1 |
||
85 | } |
||
86 | } |
||
87 | ] |
||
88 | } |
||
89 | """, |
||
90 | ) |
||
91 | + "\n" |
||
92 | ) |
||
97 |