Skip to content

Commit

Permalink
Streamline dict access patterns (#4017)
Browse files Browse the repository at this point in the history
* 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 <ferdnyc@gmail.com>
  • Loading branch information
MartinThoma and ferdnyc authored Mar 24, 2021
1 parent 146189d commit fc6380a
Show file tree
Hide file tree
Showing 10 changed files with 24 additions and 43 deletions.
2 changes: 1 addition & 1 deletion installer/fix_qt5_rpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def fix_rpath(PATH):
# Loop through all dependencies of each library/executable
raw_output = subprocess.Popen(["oTool", "-L", file_path], stdout=subprocess.PIPE).communicate()[0].decode('utf-8')
for output in raw_output.split("\n")[1:-1]:
if output and not "is not an object file" in output and ".o):" not in output:
if output and "is not an object file" not in output and ".o):" not in output:
dependency_path = output.split('\t')[1].split(' ')[0]
dependency_version = output.split('\t')[1].split(' (')[1].replace(')', '')
dependency_base_path, dependency_name = os.path.split(dependency_path)
Expand Down
6 changes: 3 additions & 3 deletions src/classes/project_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def get(self, key):
# True until something disqualifies this as a match
match = True
# 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
for subkey in key_part.keys():
for subkey in key_part:
# Get each key in dictionary (i.e. "id", "layer", etc...)
subkey = subkey.lower()
# If object is missing the key or the values differ, then it doesn't match.
Expand Down Expand Up @@ -512,7 +512,7 @@ def read_legacy_project_file(self, file_path):
for track in reversed(sequence.tracks):
for clip in track.clips:
# Get associated file for this clip
if clip.file_object.unique_id in file_lookup.keys():
if clip.file_object.unique_id in file_lookup:
file = file_lookup[clip.file_object.unique_id]
else:
# Skip missing file
Expand Down Expand Up @@ -840,7 +840,7 @@ def move_temp_paths_to_project_folder(self, file_path, previous_path=None):
log.info("Checking clip {} path for file {}".format(clip["id"], file_id))
# Update paths to files stored in our working space or old path structure
# (should have already been copied during previous File stage)
if file_id and file_id in reader_paths.keys():
if file_id and file_id in reader_paths:
clip["reader"]["path"] = reader_paths[file_id]
log.info("Updated clip {} path for file {}".format(clip["id"], file_id))

Expand Down
4 changes: 2 additions & 2 deletions src/language/generate_translations.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@
# Loop through props
for key in props.keys():
object = props[key]
if "name" in object.keys():
if "name" in object:
effects_text[object["name"]] = "libopenshot (Clip Properties)"
if "choices" in object.keys():
if "choices" in object:
for choice in object["choices"]:
effects_text[choice["name"]] = "libopenshot (Clip Properties)"

Expand Down
4 changes: 2 additions & 2 deletions src/windows/add_to_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,10 @@ def accept(self):
start_time = 0
end_time = new_clip["reader"]["duration"]

if 'start' in file.data.keys():
if 'start' in file.data:
start_time = file.data['start']
new_clip["start"] = start_time
if 'end' in file.data.keys():
if 'end' in file.data:
end_time = file.data['end']
new_clip["end"] = end_time

Expand Down
4 changes: 2 additions & 2 deletions src/windows/cutting.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ def __init__(self, file=None, preview=False):

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

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

# Remove unneeded attributes
if 'name' in self.file.data.keys():
if 'name' in self.file.data:
self.file.data.pop('name')

# Save new file
Expand Down
8 changes: 2 additions & 6 deletions src/windows/file_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,8 @@ def __init__(self, file):
file_extension = os.path.splitext(filename)[1]
fps_float = float(self.file.data["fps"]["num"]) / float(self.file.data["fps"]["den"])

tags = ""
if "tags" in self.file.data.keys():
tags = self.file.data["tags"]
name = filename
if "name" in self.file.data.keys():
name = self.file.data["name"]
tags = self.file.data.get("tags", "")
name = self.file.data.get("name", filename)

# Populate fields
self.txtFileName.setText(name)
Expand Down
4 changes: 1 addition & 3 deletions src/windows/models/add_to_timeline_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ def update_model(self, files=[], clear=True):
row = []

# Look for friendly name attribute (optional)
name = filename
if 'name' in file.data.keys():
name = file.data['name']
name = file.data.get("name", filename)

# Append thumbnail
col = QStandardItem()
Expand Down
14 changes: 4 additions & 10 deletions src/windows/models/files_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,16 @@ def update_model(self, clear=True, delete_file_id=None):
continue

path, filename = os.path.split(file.data["path"])
tags = ""
if "tags" in file.data.keys():
tags = file.data["tags"]
name = filename
if "name" in file.data.keys():
name = file.data["name"]
tags = file.data.get("tags", "")
name = file.data.get("name", filename)

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

# Generate thumbnail for file (if needed)
if media_type in ["video", "image"]:
# Check for start and end attributes (optional)
thumbnail_frame = 1
if 'start' in file.data.keys():
if 'start' in file.data:
fps = file.data["fps"]
fps_float = float(fps["num"]) / float(fps["den"])
thumbnail_frame = round(float(file.data['start']) * fps_float) + 1
Expand Down Expand Up @@ -492,9 +488,7 @@ def update_file_thumbnail(self, file_id):
"""Update/re-generate the thumbnail of a specific file"""
file = File.get(id=file_id)
path, filename = os.path.split(file.data["path"])
name = filename
if "name" in file.data.keys():
name = file.data["name"]
name = file.data.get("name", filename)

# Refresh thumbnail for updated file
self.ignore_updates = True
Expand Down
13 changes: 3 additions & 10 deletions src/windows/views/files_treeview.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,9 @@ def value_updated(self, item):

# Get file object and update friendly name and tags attribute
f = File.get(id=file_id)
if name and name != os.path.basename(f.data["path"]):
f.data["name"] = name
else:
f.data["name"] = os.path.basename(f.data["path"])

if "tags" in f.data.keys():
if tags != f.data["tags"]:
f.data["tags"] = tags
elif tags:
f.data["tags"] = tags
f.data.update({"name": name or os.path.basename(f.data.get("path"))})
if "tags" in f.data or tags:
f.data.update({"tags": tags})

# Save File
f.save()
Expand Down
8 changes: 4 additions & 4 deletions src/windows/views/webview.py
Original file line number Diff line number Diff line change
Expand Up @@ -2227,7 +2227,7 @@ def Time_Triggered(self, action, clip_ids, speed="1X", playhead_position=0.0):
continue

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

original_duration = clip.data["duration"]
if "original_data" in clip.data.keys():
if "original_data" in clip.data:
original_duration = clip.data["original_data"]["duration"]

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

# Check for optional start and end attributes
if 'start' in file.data.keys():
if 'start' in file.data:
new_clip["start"] = file.data['start']
if 'end' in file.data.keys():
if 'end' in file.data:
new_clip["end"] = file.data['end']

# Set position and closet track
Expand Down

0 comments on commit fc6380a

Please sign in to comment.