Conditions | 11 |
Total Lines | 70 |
Code Lines | 33 |
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:
Complex classes like pandoc_beamer_multigraphics._main.image() 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 | #!/usr/bin/env python |
||
18 | def image(elem: Element, doc: Doc) -> RawInline | None: |
||
19 | """ |
||
20 | Transform image element. |
||
21 | |||
22 | Arguments |
||
23 | --------- |
||
24 | elem |
||
25 | current element |
||
26 | doc |
||
27 | pandoc document |
||
28 | |||
29 | Returns |
||
30 | ------- |
||
31 | RawInline | None |
||
32 | RawInline or None |
||
33 | """ |
||
34 | if doc.format == "beamer" and isinstance(elem, Image): |
||
35 | classes = frozenset(elem.classes) |
||
36 | |||
37 | # Loop on all multi-graphics definition |
||
38 | for definition in doc.defined: |
||
39 | # Are the classes correct? |
||
40 | if classes >= definition["classes"]: |
||
41 | graphics: list[str] = [] |
||
42 | |||
43 | if ( |
||
44 | "height" in elem.attributes |
||
45 | or "width" in elem.attributes |
||
46 | or "height" in definition |
||
47 | or "width" in definition |
||
48 | ): |
||
49 | graphics.extend( |
||
50 | ( |
||
51 | "height=%s" |
||
52 | % str( |
||
53 | elem.attributes.get( |
||
54 | "height", definition.get("height", "\\textheight") |
||
55 | ) |
||
56 | ), |
||
57 | "width=%s" |
||
58 | % str( |
||
59 | elem.attributes.get( |
||
60 | "width", definition.get("width", "\\textwidth") |
||
61 | ) |
||
62 | ), |
||
63 | ), |
||
64 | ) |
||
65 | |||
66 | options = [] |
||
67 | |||
68 | if "start" in elem.attributes: |
||
69 | options.append(f"start={int(elem.attributes['start']):d}") |
||
70 | |||
71 | if "end" in elem.attributes: |
||
72 | options.append(f"end={int(elem.attributes['end']):d}") |
||
73 | |||
74 | options.append( |
||
75 | f"format={elem.attributes.get('format', definition['format'])}" |
||
76 | ) |
||
77 | |||
78 | return RawInline( |
||
79 | ( |
||
80 | f"\\multiinclude" |
||
81 | f"[graphics={{{','.join(graphics)}}},{','.join(options)}]" |
||
82 | f"{{{elem.url}}}" |
||
83 | ), |
||
84 | "tex", |
||
85 | ) |
||
86 | |||
87 | return None |
||
88 | |||
164 |