Skip to content

Commit fc6380a

Browse files
MartinThomaferdnyc
andauthored
Streamline dict access patterns (#4017)
* Use 'key in dict' instead of 'key in dict.keys()' * Thank you Frank Dana for the suggestions! * Use get * Fix member variable reference Co-authored-by: Frank Dana <[email protected]>
1 parent 146189d commit fc6380a

10 files changed

+24
-43
lines changed

installer/fix_qt5_rpath.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def fix_rpath(PATH):
4141
# Loop through all dependencies of each library/executable
4242
raw_output = subprocess.Popen(["oTool", "-L", file_path], stdout=subprocess.PIPE).communicate()[0].decode('utf-8')
4343
for output in raw_output.split("\n")[1:-1]:
44-
if output and not "is not an object file" in output and ".o):" not in output:
44+
if output and "is not an object file" not in output and ".o):" not in output:
4545
dependency_path = output.split('\t')[1].split(' ')[0]
4646
dependency_version = output.split('\t')[1].split(' (')[1].replace(')', '')
4747
dependency_base_path, dependency_name = os.path.split(dependency_path)

src/classes/project_data.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def get(self, key):
9898
# True until something disqualifies this as a match
9999
match = True
100100
# Check each key in key_part dictionary and if not found to be equal as a property in item, move on to next item in list
101-
for subkey in key_part.keys():
101+
for subkey in key_part:
102102
# Get each key in dictionary (i.e. "id", "layer", etc...)
103103
subkey = subkey.lower()
104104
# If object is missing the key or the values differ, then it doesn't match.
@@ -512,7 +512,7 @@ def read_legacy_project_file(self, file_path):
512512
for track in reversed(sequence.tracks):
513513
for clip in track.clips:
514514
# Get associated file for this clip
515-
if clip.file_object.unique_id in file_lookup.keys():
515+
if clip.file_object.unique_id in file_lookup:
516516
file = file_lookup[clip.file_object.unique_id]
517517
else:
518518
# Skip missing file
@@ -840,7 +840,7 @@ def move_temp_paths_to_project_folder(self, file_path, previous_path=None):
840840
log.info("Checking clip {} path for file {}".format(clip["id"], file_id))
841841
# Update paths to files stored in our working space or old path structure
842842
# (should have already been copied during previous File stage)
843-
if file_id and file_id in reader_paths.keys():
843+
if file_id and file_id in reader_paths:
844844
clip["reader"]["path"] = reader_paths[file_id]
845845
log.info("Updated clip {} path for file {}".format(clip["id"], file_id))
846846

src/language/generate_translations.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,9 @@
189189
# Loop through props
190190
for key in props.keys():
191191
object = props[key]
192-
if "name" in object.keys():
192+
if "name" in object:
193193
effects_text[object["name"]] = "libopenshot (Clip Properties)"
194-
if "choices" in object.keys():
194+
if "choices" in object:
195195
for choice in object["choices"]:
196196
effects_text[choice["name"]] = "libopenshot (Clip Properties)"
197197

src/windows/add_to_timeline.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -216,10 +216,10 @@ def accept(self):
216216
start_time = 0
217217
end_time = new_clip["reader"]["duration"]
218218

219-
if 'start' in file.data.keys():
219+
if 'start' in file.data:
220220
start_time = file.data['start']
221221
new_clip["start"] = start_time
222-
if 'end' in file.data.keys():
222+
if 'end' in file.data:
223223
end_time = file.data['end']
224224
new_clip["end"] = end_time
225225

src/windows/cutting.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ def __init__(self, file=None, preview=False):
162162

163163
# Determine if a start or end attribute is in this file
164164
start_frame = 1
165-
if 'start' in self.file.data.keys():
165+
if 'start' in self.file.data:
166166
start_frame = (float(self.file.data['start']) * self.fps) + 1
167167

168168
# Display start frame (and then the previous frame)
@@ -334,7 +334,7 @@ def btnAddClip_clicked(self):
334334
log.info('btnAddClip_clicked')
335335

336336
# Remove unneeded attributes
337-
if 'name' in self.file.data.keys():
337+
if 'name' in self.file.data:
338338
self.file.data.pop('name')
339339

340340
# Save new file

src/windows/file_properties.py

+2-6
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,8 @@ def __init__(self, file):
8282
file_extension = os.path.splitext(filename)[1]
8383
fps_float = float(self.file.data["fps"]["num"]) / float(self.file.data["fps"]["den"])
8484

85-
tags = ""
86-
if "tags" in self.file.data.keys():
87-
tags = self.file.data["tags"]
88-
name = filename
89-
if "name" in self.file.data.keys():
90-
name = self.file.data["name"]
85+
tags = self.file.data.get("tags", "")
86+
name = self.file.data.get("name", filename)
9187

9288
# Populate fields
9389
self.txtFileName.setText(name)

src/windows/models/add_to_timeline_model.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ def update_model(self, files=[], clear=True):
7272
row = []
7373

7474
# Look for friendly name attribute (optional)
75-
name = filename
76-
if 'name' in file.data.keys():
77-
name = file.data['name']
75+
name = file.data.get("name", filename)
7876

7977
# Append thumbnail
8078
col = QStandardItem()

src/windows/models/files_model.py

+4-10
Original file line numberDiff line numberDiff line change
@@ -170,20 +170,16 @@ def update_model(self, clear=True, delete_file_id=None):
170170
continue
171171

172172
path, filename = os.path.split(file.data["path"])
173-
tags = ""
174-
if "tags" in file.data.keys():
175-
tags = file.data["tags"]
176-
name = filename
177-
if "name" in file.data.keys():
178-
name = file.data["name"]
173+
tags = file.data.get("tags", "")
174+
name = file.data.get("name", filename)
179175

180176
media_type = file.data.get("media_type")
181177

182178
# Generate thumbnail for file (if needed)
183179
if media_type in ["video", "image"]:
184180
# Check for start and end attributes (optional)
185181
thumbnail_frame = 1
186-
if 'start' in file.data.keys():
182+
if 'start' in file.data:
187183
fps = file.data["fps"]
188184
fps_float = float(fps["num"]) / float(fps["den"])
189185
thumbnail_frame = round(float(file.data['start']) * fps_float) + 1
@@ -492,9 +488,7 @@ def update_file_thumbnail(self, file_id):
492488
"""Update/re-generate the thumbnail of a specific file"""
493489
file = File.get(id=file_id)
494490
path, filename = os.path.split(file.data["path"])
495-
name = filename
496-
if "name" in file.data.keys():
497-
name = file.data["name"]
491+
name = file.data.get("name", filename)
498492

499493
# Refresh thumbnail for updated file
500494
self.ignore_updates = True

src/windows/views/files_treeview.py

+3-10
Original file line numberDiff line numberDiff line change
@@ -183,16 +183,9 @@ def value_updated(self, item):
183183

184184
# Get file object and update friendly name and tags attribute
185185
f = File.get(id=file_id)
186-
if name and name != os.path.basename(f.data["path"]):
187-
f.data["name"] = name
188-
else:
189-
f.data["name"] = os.path.basename(f.data["path"])
190-
191-
if "tags" in f.data.keys():
192-
if tags != f.data["tags"]:
193-
f.data["tags"] = tags
194-
elif tags:
195-
f.data["tags"] = tags
186+
f.data.update({"name": name or os.path.basename(f.data.get("path"))})
187+
if "tags" in f.data or tags:
188+
f.data.update({"tags": tags})
196189

197190
# Save File
198191
f.save()

src/windows/views/webview.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -2227,7 +2227,7 @@ def Time_Triggered(self, action, clip_ids, speed="1X", playhead_position=0.0):
22272227
continue
22282228

22292229
# Keep original 'end' and 'duration'
2230-
if "original_data" not in clip.data.keys():
2230+
if "original_data" not in clip.data:
22312231
clip.data["original_data"] = {
22322232
"end": clip.data["end"],
22332233
"duration": clip.data["duration"],
@@ -2246,7 +2246,7 @@ def Time_Triggered(self, action, clip_ids, speed="1X", playhead_position=0.0):
22462246
freeze_seconds = float(speed)
22472247

22482248
original_duration = clip.data["duration"]
2249-
if "original_data" in clip.data.keys():
2249+
if "original_data" in clip.data:
22502250
original_duration = clip.data["original_data"]["duration"]
22512251

22522252
log.info('Updating timing for clip ID {}, original duration: {}'
@@ -2873,9 +2873,9 @@ def callback(self, data, callback_data):
28732873
return # Do nothing
28742874

28752875
# Check for optional start and end attributes
2876-
if 'start' in file.data.keys():
2876+
if 'start' in file.data:
28772877
new_clip["start"] = file.data['start']
2878-
if 'end' in file.data.keys():
2878+
if 'end' in file.data:
28792879
new_clip["end"] = file.data['end']
28802880

28812881
# Set position and closet track

0 commit comments

Comments
 (0)