Skip to content

Commit 384e3a9

Browse files
committed
refactor dict usage (#321)
1 parent 2e7a61c commit 384e3a9

File tree

5 files changed

+29
-39
lines changed

5 files changed

+29
-39
lines changed

src/canmatrix/cli/compare.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def compare_db(db1, db2, ignore=None):
8484
if f1 is None:
8585
result.add_child(CompareResult("added", "FRAME", f2))
8686

87-
if "ATTRIBUTE" in ignore and ignore["ATTRIBUTE"] == "*":
87+
if ignore.get("ATTRIBUTE", "") == "*":
8888
pass
8989
else:
9090
result.add_child(compare_attributes(db1, db2, ignore))
@@ -100,7 +100,7 @@ def compare_db(db1, db2, ignore=None):
100100
if ecu1 is None:
101101
result.add_child(CompareResult("added", "ecu", ecu2))
102102

103-
if "DEFINE" in ignore and ignore["DEFINE"] == "*":
103+
if ignore.get("DEFINE", "") == "*":
104104
pass
105105
else:
106106
result.add_child(
@@ -120,7 +120,7 @@ def compare_db(db1, db2, ignore=None):
120120
temp.type = "Signal Defines"
121121
result.add_child(temp)
122122

123-
if "VALUETABLES" in ignore and ignore["VALUETABLES"]:
123+
if ignore.get("VALUETABLES", False):
124124
pass
125125
else:
126126
for vt1 in db1.value_tables:
@@ -242,8 +242,7 @@ def compare_attributes(ele1, ele2, ignore=None):
242242
if ignore is None:
243243
ignore = dict()
244244
result = CompareResult("equal", "ATTRIBUTES", ele1)
245-
if "ATTRIBUTE" in ignore and (
246-
ignore["ATTRIBUTE"] == "*" or ignore["ATTRIBUTE"] == ele1):
245+
if ignore.get("ATTRIBUTE", "") == "*" or ignore.get("ATTRIBUTE","") == ele1:
247246
return result
248247
for attribute in ele1.attributes:
249248
if attribute not in ele2.attributes:
@@ -281,7 +280,7 @@ def compare_ecu(ecu1, ecu2, ignore=None):
281280
"changed", "ECU", ecu1, [
282281
ecu1.comment, ecu2.comment]))
283282

284-
if "ATTRIBUTE" in ignore and ignore["ATTRIBUTE"] == "*":
283+
if ignore.get("ATTRIBUTE", "") == "*":
285284
pass
286285
else:
287286
result.add_child(compare_attributes(ecu1, ecu2, ignore))
@@ -336,7 +335,7 @@ def compare_frame(f1, f2, ignore=None):
336335
if not s1:
337336
result.add_child(CompareResult("added", "SIGNAL", s2))
338337

339-
if "ATTRIBUTE" in ignore and ignore["ATTRIBUTE"] == "*":
338+
if ignore.get("ATTRIBUTE", "") == "*":
340339
pass
341340
else:
342341
result.add_child(compare_attributes(f1, f2, ignore))
@@ -457,12 +456,12 @@ def compare_signal(s1, s2, ignore=None):
457456
receiver,
458457
s1.receivers))
459458

460-
if "ATTRIBUTE" in ignore and ignore["ATTRIBUTE"] == "*":
459+
if ignore.get("ATTRIBUTE", "") == "*":
461460
pass
462461
else:
463462
result.add_child(compare_attributes(s1, s2, ignore))
464463

465-
if "VALUETABLES" in ignore and ignore["VALUETABLES"]:
464+
if ignore.get("VALUETABLES", ""):
466465
pass
467466
else:
468467
result.add_child(compare_value_table(s1.values, s2.values))

src/canmatrix/formats/cmjson.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ def dump(db, f, **options):
4848
else:
4949
mode = 'wb'
5050

51-
additionalFrameColums = [] # type: typing.List[str]
52-
if "additionalFrameAttributes" in options and options["additionalFrameAttributes"]:
53-
additionalFrameColums = options["additionalFrameAttributes"].split(",")
51+
if options.get("additionalFrameAttributes",False):
52+
additionalFrameColums = options.get("additionalFrameAttributes").split(",")
53+
else:
54+
additionalFrameColums = [] # type: typing.List[str]
5455

5556
exportArray = [] # type: typing.List[typing.Union[str, int, list, dict]]
5657

@@ -192,10 +193,9 @@ def load(f, **options):
192193
newframe = canmatrix.Frame(frame["name"],
193194
arbitration_id=frame["id"],
194195
size=8)
195-
if "length" in frame:
196-
newframe.size = frame["length"]
196+
newframe.size = frame.get("length",0)
197197

198-
if "is_extended_frame" in frame and frame["is_extended_frame"]:
198+
if frame.get("is_extended_frame", False):
199199
newframe.arbitration_id.extended = 1
200200
else:
201201
newframe.arbitration_id.extended = 0
@@ -221,7 +221,7 @@ def load(f, **options):
221221
factor=signal["factor"],
222222
offset=signal["offset"])
223223

224-
if signal.get("min") is not None:
224+
if signal.get("min", False) is not None:
225225
newsignal.min = newsignal.float_factory(signal["min"])
226226

227227
if signal.get("max", False):

src/canmatrix/formats/dbc.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ def add_frame_by_id(frame): # type: (canmatrix.Frame) -> None
892892
# Backtracking
893893

894894
for env_var_name, env_var in db.env_vars.items():
895-
if "attributes" in env_var and 'SystemEnvVarLongSymbol' in env_var["attributes"]:
895+
if 'SystemEnvVarLongSymbol' in env_var.get("attributes", ""):
896896
long_name = env_var["attributes"]["SystemEnvVarLongSymbol"][1:-1]
897897
del(env_var["attributes"]["SystemEnvVarLongSymbol"])
898898
db.env_vars[long_name] = db.env_vars.pop(env_var_name)
@@ -912,7 +912,7 @@ def add_frame_by_id(frame): # type: (canmatrix.Frame) -> None
912912
# frame.id -= 0x80000000
913913
# frame.extended = 1
914914

915-
if "VFrameFormat" in frame.attributes and "_FD" in frame.attributes["VFrameFormat"]:
915+
if "_FD" in frame.attributes.get("VFrameFormat",""):
916916
frame.is_fd = True
917917

918918
for signal in frame.signals:

src/canmatrix/formats/xls.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,17 @@ def dump(db, file, **options):
114114
'Signal Name', 'Signal Function', 'Signal Length [Bit]', 'Signal Default', ' Signal Not Available', 'Byteorder']
115115
head_tail = ['Value', 'Name / Phys. Range', 'Function / Increment Unit']
116116

117-
if "additionalAttributes" in options and len(options["additionalAttributes"]) > 0:
118-
additional_signal_colums = options["additionalAttributes"].split(",")
117+
if len(options.get("additionalAttributes", "")) > 0:
118+
additional_signal_colums = options.get("additionalAttributes").split(",")
119119
else:
120120
additional_signal_colums = []#["attributes['DisplayDecimalPlaces']"]
121121

122-
if "additionalFrameAttributes" in options and len(options["additionalFrameAttributes"]) > 0:
123-
additional_frame_colums = options["additionalFrameAttributes"].split(",")
122+
if len(options.get("additionalFrameAttributes","")) > 0:
123+
additional_frame_colums = options.get("additionalFrameAttributes").split(",")
124124
else:
125125
additional_frame_colums = []#["attributes['DisplayDecimalPlaces']"]
126126

127-
if 'xlsMotorolaBitFormat' in options:
128-
motorolaBitFormat = options["xlsMotorolaBitFormat"]
129-
else:
130-
motorolaBitFormat = "msbreverse"
127+
motorolaBitFormat = options.get("xlsMotorolaBitFormat","msbreverse")
131128

132129
workbook = xlwt.Workbook(encoding='utf8')
133130
# wsname = os.path.basename(filename).replace('.xls', '')
@@ -469,7 +466,7 @@ def load(file, **options):
469466
mux, signalComment = signalComment[4:].split(':', 1)
470467
multiplex = int(mux.strip())
471468

472-
if "byteorder" in index:
469+
if index.get("byteorder", False):
473470
signalByteorder = sh.cell(rownum, index['byteorder']).value
474471

475472
if 'i' in signalByteorder:

src/canmatrix/formats/xlsx.py

+6-12
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ def dump(db, filename, **options):
123123
'Byteorder']
124124
head_tail = ['Value', 'Name / Phys. Range', 'Function / Increment Unit']
125125

126-
if "additionalAttributes" in options and len(options["additionalAttributes"]) > 0:
127-
additional_signal_colums = options["additionalAttributes"].split(",")
126+
if len(options.get("additionalAttributes", "")) > 0:
127+
additional_signal_colums = options.get("additionalAttributes").split(",")
128128
else:
129129
additional_signal_colums = []#["attributes['DisplayDecimalPlaces']"]
130130

131-
if "additionalFrameAttributes" in options and len(options["additionalFrameAttributes"]) > 0:
132-
additional_frame_colums = options["additionalFrameAttributes"].split(",")
131+
if len(options.get("additionalFrameAttributes", "")) > 0:
132+
additional_frame_colums = options.get("additionalFrameAttributes").split(",")
133133
else:
134134
additional_frame_colums = []#["attributes['DisplayDecimalPlaces']"]
135135

@@ -347,14 +347,8 @@ def readXlsx(file, **args):
347347
import zipfile
348348
from xml.etree.ElementTree import iterparse
349349

350-
if "sheet" in args:
351-
sheet = args["sheet"]
352-
else:
353-
sheet = 1
354-
if "header" in args:
355-
isHeader = args["header"]
356-
else:
357-
isHeader = False
350+
sheet = args.get("sheet", 1)
351+
isHeader = args.get("header", False)
358352

359353
rows = [] # type: typing.List[typing.Dict[str, str]]
360354
row = {}

0 commit comments

Comments
 (0)