Skip to content

Commit

Permalink
tests/usbsdmux: manually address ruff "SIM" linter errors
Browse files Browse the repository at this point in the history
The SIM rule implements the rules provided by "flake8-simplify",
which describes itself as:

  A flake8 plugin that helps you simplify your code

Signed-off-by: Leonard Göhrs <l.goehrs@pengutronix.de>
  • Loading branch information
hnez committed May 8, 2024
1 parent bb17e2e commit ba27294
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 22 deletions.
18 changes: 10 additions & 8 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ def test_help_in_readme(capsys, mocker):

readme_path = os.path.join(os.path.dirname(__file__), "../", "README.rst")
readme_lines = None
for line in open(readme_path).readlines():
line = line.rstrip()
if line == " $ usbsdmux -h":
readme_lines = []
elif readme_lines is not None:
if line and not line.startswith(" "):
break
readme_lines.append(line)

with open(readme_path) as readme:
for line in readme.readlines():
line = line.rstrip()
if line == " $ usbsdmux -h":
readme_lines = []
elif readme_lines is not None:
if line and not line.startswith(" "):
break
readme_lines.append(line)

assert readme_lines is not None, "Bash command not found. Did you include ' $ usbsdmux -h'?"
assert readme_lines, "No output lines found. Did you indent the output correctly?"
Expand Down
12 changes: 9 additions & 3 deletions tests/test_sd_regs.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
@pytest.mark.parametrize("cid", REFS)
def test_decode(cid):
ref_name = os.path.join(os.path.dirname(__file__), "reference", f"{cid}.json")
ref = json.load(open(ref_name))

with open(ref_name) as ref_file:
ref = json.load(ref_file)

res = {}
res["scr"] = SCR(ref["scr"]["raw"]).decode()
Expand All @@ -33,8 +35,12 @@ def test_decode(cid):
def test_to_text(cid):
ref_name_json = os.path.join(os.path.dirname(__file__), "reference", f"{cid}.json")
ref_name_text = os.path.join(os.path.dirname(__file__), "reference", f"{cid}.text")
ref_json = json.load(open(ref_name_json))
ref_text = open(ref_name_text).read().split("\n")[:-1]

with open(ref_name_json) as ref_file_json:
ref_json = json.load(ref_file_json)

with open(ref_name_text) as ref_file_text:
ref_text = ref_file_text.read().split("\n")[:-1]

res = []

Expand Down
6 changes: 2 additions & 4 deletions usbsdmux/ctypehelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,8 @@ def string_to_uint8_array(str, array_length, c_string=False, padding=0xFF, encod

# Determine number of bytes to copy
nbytes = str.encode(encoding)
if c_string:
count = min(len(nbytes), array_length - 1)
else:
count = min(len(nbytes), array_length)
length = array_length - 1 if c_string else array_length
count = min(len(nbytes), length)

# Do the actual copy
for i in range(count):
Expand Down
12 changes: 5 additions & 7 deletions usbsdmux/sd_regs.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ def decode_TAAC(self):
scale = TIME_SCALE_ENUM[bitslice(v[0], 2, 0)]
value = self.TIME_VALUE_ENUM[bitslice(v[0], 6, 3)]

if isinstance(value, float):
scaled_value = value * scale
else:
scaled_value = None
scaled_value = value * scale if isinstance(value, float) else None

return {
"decoded": (value, unit),
Expand All @@ -156,10 +153,10 @@ def decode_TRAN_SPEED(self):
scale = RATE_SCALE_ENUM[bitslice(v[0], 2, 0)]
value = self.TIME_VALUE_ENUM[bitslice(v[0], 6, 3)]

scaled_value = None

if isinstance(value, float) and isinstance(scale, int):
scaled_value = value * scale
else:
scaled_value = None

return {
"decoded": (value, unit),
Expand Down Expand Up @@ -517,7 +514,8 @@ class SCR(RegisterDecoder):

args = parser.parse_args()

data = json.loads(open(args.input).read())
with open(args.input) as fd:
data = json.loads(fd.read())

if args.json:
res = {}
Expand Down

0 comments on commit ba27294

Please sign in to comment.