Conditions | 14 |
Total Lines | 63 |
Code Lines | 48 |
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 jrnl.DayOneJournal.DayOne.write() 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 |
||
117 | def write(self): |
||
118 | """Writes only the entries that have been modified into plist files.""" |
||
119 | for entry in self.entries: |
||
120 | if entry.modified: |
||
121 | utc_time = datetime.utcfromtimestamp( |
||
122 | time.mktime(entry.date.timetuple()) |
||
123 | ) |
||
124 | |||
125 | if not hasattr(entry, "uuid"): |
||
126 | entry.uuid = uuid.uuid1().hex |
||
127 | if not hasattr(entry, "creator_device_agent"): |
||
128 | entry.creator_device_agent = "" # iPhone/iPhone5,3 |
||
129 | if not hasattr(entry, "creator_generation_date"): |
||
130 | entry.creator_generation_date = utc_time |
||
131 | if not hasattr(entry, "creator_host_name"): |
||
132 | entry.creator_host_name = socket.gethostname() |
||
133 | if not hasattr(entry, "creator_os_agent"): |
||
134 | entry.creator_os_agent = "{}/{}".format( |
||
135 | platform.system(), platform.release() |
||
136 | ) |
||
137 | if not hasattr(entry, "creator_software_agent"): |
||
138 | entry.creator_software_agent = "{}/{}".format( |
||
139 | __title__, __version__ |
||
140 | ) |
||
141 | |||
142 | fn = ( |
||
143 | Path(self.config["journal"]) |
||
144 | / "entries" |
||
145 | / (entry.uuid.upper() + ".doentry") |
||
146 | ) |
||
147 | |||
148 | entry_plist = { |
||
149 | "Creation Date": utc_time, |
||
150 | "Starred": entry.starred if hasattr(entry, "starred") else False, |
||
151 | "Entry Text": entry.title + "\n" + entry.body, |
||
152 | "Time Zone": str(tzlocal.get_localzone()), |
||
153 | "UUID": entry.uuid.upper(), |
||
154 | "Tags": [ |
||
155 | tag.strip(self.config["tagsymbols"]).replace("_", " ") |
||
156 | for tag in entry.tags |
||
157 | ], |
||
158 | "Creator": { |
||
159 | "Device Agent": entry.creator_device_agent, |
||
160 | "Generation Date": entry.creator_generation_date, |
||
161 | "Host Name": entry.creator_host_name, |
||
162 | "OS Agent": entry.creator_os_agent, |
||
163 | "Software Agent": entry.creator_software_agent, |
||
164 | }, |
||
165 | } |
||
166 | if hasattr(entry, "location"): |
||
167 | entry_plist["Location"] = entry.location |
||
168 | if hasattr(entry, "weather"): |
||
169 | entry_plist["Weather"] = entry.weather |
||
170 | |||
171 | # plistlib expects a binary object |
||
172 | with fn.open(mode="wb") as f: |
||
173 | plistlib.dump(entry_plist, f, fmt=plistlib.FMT_XML, sort_keys=False) |
||
174 | |||
175 | for entry in self._deleted_entries: |
||
176 | filename = os.path.join( |
||
177 | self.config["journal"], "entries", entry.uuid + ".doentry" |
||
178 | ) |
||
179 | os.remove(filename) |
||
180 | |||
226 |