Conditions | 10 |
Total Lines | 77 |
Code Lines | 49 |
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 isomer.ui.store.inventory.inspect_wheel() 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 |
||
69 | def inspect_wheel(wheel: str): |
||
70 | """Inspeggtor reporting for duty! |
||
71 | |||
72 | This thing opens up Python Eggs to analyze their content in an isomeric way. |
||
73 | Some data pulled out is specific to isomer modules but could be used for general |
||
74 | structures. Specifically, a `docs/README.rst` and a `docs/preview.png` is looked |
||
75 | for. |
||
76 | |||
77 | :param wheel: Filename of the python egg to inspect |
||
78 | :return: Dictionary with |
||
79 | `info` - content of docs/README.rst if found |
||
80 | `preview` - content of docs/preview.png if found |
||
81 | `date` - publishing date according to EGG-INFO/PKG-INFO timestamp |
||
82 | `requires` - any required external python packages |
||
83 | :rtype: dict |
||
84 | """ |
||
85 | archive = zipfile.ZipFile(wheel) |
||
86 | |||
87 | meta_name = os.path.basename(wheel).split("-py")[0] + ".dist-info/METADATA" |
||
88 | |||
89 | try: |
||
90 | info = archive.read("docs/README.rst").decode('utf-8') |
||
91 | |||
92 | info = core.publish_parts(info, writer_name="html")["html_body"] |
||
93 | except KeyError: |
||
94 | info = "No information provided" |
||
95 | |||
96 | try: |
||
97 | preview = base64.b64encode(archive.read("docs/preview.png")).decode('utf-8') |
||
98 | except KeyError: |
||
99 | preview = "" |
||
100 | |||
101 | pkg_info_info = archive.getinfo(meta_name) |
||
102 | |||
103 | date = std_datetime(pkg_info_info.date_time) |
||
104 | |||
105 | requires = [] |
||
106 | |||
107 | homepage = "" |
||
108 | author = "" |
||
109 | contact = "" |
||
110 | package_license = "" |
||
111 | |||
112 | try: |
||
113 | lines = str(archive.read(meta_name), encoding="ascii").split("\n") |
||
114 | |||
115 | for line in lines: |
||
116 | if line.startswith("Home-page:"): |
||
117 | homepage = line.split("Home-page: ")[1] |
||
118 | if line.startswith("Author:"): |
||
119 | author = line.split("Author: ")[1] |
||
120 | if line.startswith("Author-email:"): |
||
121 | contact = line.split("Author-email: ")[1] |
||
122 | if line.startswith("License:"): |
||
123 | package_license = line.split("License: ")[1] |
||
124 | if line.startswith("Requires-Dist:"): |
||
125 | req = line.split("Requires-Dist: ")[1] |
||
126 | req = req.replace(" (", "").replace(")", "") |
||
127 | requires.append(req) |
||
128 | except KeyError: |
||
129 | log("No metadata found") |
||
130 | |||
131 | result = { |
||
132 | 'info': info, |
||
133 | 'preview': preview, |
||
134 | 'date': date, |
||
135 | 'requires': requires, |
||
136 | 'homepage': homepage, |
||
137 | 'author': author, |
||
138 | 'contact': contact, |
||
139 | 'license': package_license, |
||
140 | 'downloads': "-", |
||
141 | 'stars': "-" |
||
142 | } |
||
143 | log(result, pretty=True) |
||
144 | |||
145 | return result |
||
146 | |||
186 |