Skip to content

Commit 434d6c3

Browse files
committed
ruff
1 parent 260420e commit 434d6c3

File tree

5 files changed

+155
-25
lines changed

5 files changed

+155
-25
lines changed

src/ibex_non_ca_helpers/hex.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import binascii
2-
import zlib
32
import json
4-
from ibex_non_ca_helpers.pv import waveform_to_string
3+
import zlib
54
from typing import Any, List
65

6+
from ibex_non_ca_helpers.pv import waveform_to_string
7+
8+
79
def compress_and_hex(value: str) -> bytes:
810
"""Compresses the inputted string and encodes it as hex.
911
@@ -12,7 +14,7 @@ def compress_and_hex(value: str) -> bytes:
1214
Returns:
1315
bytes : A compressed and hexed version of the inputted string
1416
"""
15-
assert type(value) == str, (
17+
assert type(value) is str, (
1618
"Non-str argument passed to compress_and_hex, maybe Python 2/3 compatibility issue\n"
1719
"Argument was type {} with value {}".format(value.__class__.__name__, value)
1820
)
@@ -29,23 +31,24 @@ def dehex_and_decompress(value: bytes) -> bytes:
2931
Returns:
3032
bytes : A decompressed version of the inputted string
3133
"""
32-
assert type(value) == bytes, (
34+
assert type(value) is bytes, (
3335
"Non-bytes argument passed to dehex_and_decompress, maybe Python 2/3 compatibility issue\n"
3436
"Argument was type {} with value {}".format(value.__class__.__name__, value)
3537
)
3638
return zlib.decompress(binascii.unhexlify(value))
3739

3840

3941
def dehex_and_decompress_waveform(value: List[int]) -> bytes:
40-
"""Decompresses the inputted waveform, assuming it is a array of integers representing characters (null terminated).
42+
"""Decompresses the inputted waveform,
43+
assuming it is an array of integers representing characters (null terminated).
4144
4245
Args:
4346
value (list[int]): The string to be decompressed
4447
4548
Returns:
4649
bytes : A decompressed version of the inputted string
4750
"""
48-
assert type(value) == list, (
51+
assert type(value) is list, (
4952
"Non-list argument passed to dehex_and_decompress_waveform\n"
5053
"Argument was type {} with value {}".format(value.__class__.__name__, value)
5154
)
@@ -54,6 +57,7 @@ def dehex_and_decompress_waveform(value: List[int]) -> bytes:
5457
bytes_rep = unicode_rep.encode("ascii")
5558
return dehex_and_decompress(bytes_rep)
5659

60+
5761
def dehex_decompress_and_dejson(value: str | bytes) -> Any: # noqa: ANN401
5862
"""
5963
Convert string from zipped hexed json to a python representation

src/ibex_non_ca_helpers/pv.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from typing import Iterable
22

3+
34
def waveform_to_string(data: Iterable[int | str]) -> str:
45
output = ""
56
for i in data:

tests/test_hex.py

Lines changed: 129 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import pytest
2-
from ibex_non_ca_helpers.hex import compress_and_hex, dehex_and_decompress, dehex_and_decompress_waveform, dehex_decompress_and_dejson
2+
3+
from ibex_non_ca_helpers.hex import (
4+
compress_and_hex,
5+
dehex_and_decompress,
6+
dehex_and_decompress_waveform,
7+
dehex_decompress_and_dejson,
8+
)
39

410

511
def test_can_dehex_and_decompress():
@@ -15,25 +21,144 @@ def test_can_hex_and_compress():
1521
result = compress_and_hex(to_compress_and_hex)
1622
assert result == expected
1723

24+
1825
def test_non_bytes_given_to_dehex_and_decompress_raises_assertionerror():
1926
with pytest.raises(AssertionError):
2027
dehex_and_decompress("test")
2128

29+
2230
def test_non_string_given_to_compress_and_hex_raises_assertionerror():
2331
with pytest.raises(AssertionError):
2432
compress_and_hex(b"test")
2533

34+
2635
def test_non_list_given_to_dehex_and_decompress_waveform_raises_assertionerror():
2736
with pytest.raises(AssertionError):
2837
dehex_and_decompress_waveform("test")
2938

39+
3040
def test_dehex_and_decompress_waveform_with_ok_waveform_returns_expected():
31-
test = [55, 56, 57, 99, 56, 98, 53, 54, 52, 97, 99, 99, 99, 57, 52, 99, 52, 101, 53, 53, 100, 50, 53, 49, 53, 48, 52, 97, 99, 98, 99, 57, 50, 99, 50, 56, 52, 56, 50, 100, 48, 50, 51, 49, 57, 51, 102, 50, 57, 51, 52, 48, 53, 52, 55, 49, 52, 57, 53, 49, 98, 99, 49, 49, 56, 99, 54, 49, 48, 56, 54, 53, 56, 48, 97, 56, 100, 99, 102, 99, 49, 50, 49, 48, 53, 53, 54, 48, 48, 97, 50, 54, 56, 100, 57, 53, 54, 50, 48, 49, 101, 49, 99, 53, 49, 51, 54, 52]
41+
test = [
42+
55,
43+
56,
44+
57,
45+
99,
46+
56,
47+
98,
48+
53,
49+
54,
50+
52,
51+
97,
52+
99,
53+
99,
54+
99,
55+
57,
56+
52,
57+
99,
58+
52,
59+
101,
60+
53,
61+
53,
62+
100,
63+
50,
64+
53,
65+
49,
66+
53,
67+
48,
68+
52,
69+
97,
70+
99,
71+
98,
72+
99,
73+
57,
74+
50,
75+
99,
76+
50,
77+
56,
78+
52,
79+
56,
80+
50,
81+
100,
82+
48,
83+
50,
84+
51,
85+
49,
86+
57,
87+
51,
88+
102,
89+
50,
90+
57,
91+
51,
92+
52,
93+
48,
94+
53,
95+
52,
96+
55,
97+
49,
98+
52,
99+
57,
100+
53,
101+
49,
102+
98,
103+
99,
104+
49,
105+
49,
106+
56,
107+
99,
108+
54,
109+
49,
110+
48,
111+
56,
112+
54,
113+
53,
114+
56,
115+
48,
116+
97,
117+
56,
118+
100,
119+
99,
120+
102,
121+
99,
122+
49,
123+
50,
124+
49,
125+
48,
126+
53,
127+
53,
128+
54,
129+
48,
130+
48,
131+
97,
132+
50,
133+
54,
134+
56,
135+
100,
136+
57,
137+
53,
138+
54,
139+
50,
140+
48,
141+
49,
142+
101,
143+
49,
144+
99,
145+
53,
146+
49,
147+
51,
148+
54,
149+
52,
150+
]
32151

33152
res = dehex_and_decompress_waveform(test)
34153

35154
assert res == b'["alice", "flipper", "bob", "str_2", "str_1", "str", "mot", "p5", "p3"]'
36155

156+
37157
def test_dehex_decompress_dejson():
38-
expected = {'key1': 'value1', 'key2': 'value2'}
39-
assert dehex_decompress_and_dejson(b'789cab56ca4ead3454b252502a4bcc294d3554d251008918c1458c946a01c39b0a9b') == expected
158+
expected = {"key1": "value1", "key2": "value2"}
159+
assert (
160+
dehex_decompress_and_dejson(
161+
b"789cab56ca4ead3454b252502a4bcc294d3554d251008918c1458c946a01c39b0a9b"
162+
)
163+
== expected
164+
)

tests/test_macros.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
from unittest.mock import patch
2+
23
from ibex_non_ca_helpers.macros import get_macro_values
34

5+
46
@patch("ibex_non_ca_helpers.macros.os.environ.get", return_value={})
57
def test_get_macro_values_returns_dict_with_macros_in(environ):
68
pass
79

10+
811
@patch("ibex_non_ca_helpers.macros.os.environ")
912
def test_get_macro_values_returns_empty_dict_if_no_macros(environ):
1013
expected_key_1 = "key1"
1114
expected_key_2 = "key2"
1215
expected_value_1 = "value1"
1316
expected_value_2 = "value2"
1417
environ.get.return_value = '{"key1": "value1", "key2": "value2"}'
15-
assert get_macro_values() == {expected_key_1: expected_value_1, expected_key_2: expected_value_2}
16-
18+
assert get_macro_values() == {
19+
expected_key_1: expected_value_1,
20+
expected_key_2: expected_value_2,
21+
}

tests/test_pv.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
23
from ibex_non_ca_helpers.pv import waveform_to_string
34

45

@@ -12,8 +13,7 @@ def create_waveform_from_list(input_list):
1213
return "".join([chr(i) for i in input_list])
1314

1415

15-
def test_GIVEN_short_list_of_strings_WHEN_waveform_converted_to_string_THEN_result_contains_a_string_of_strings(
16-
):
16+
def test_GIVEN_short_list_of_strings_WHEN_waveform_converted_to_string_THEN_result_contains_a_string_of_strings():
1717
# Arrange
1818
test_waveform = ["hello", "world"]
1919

@@ -25,8 +25,7 @@ def test_GIVEN_short_list_of_strings_WHEN_waveform_converted_to_string_THEN_resu
2525
check_waveform(test_waveform, expected_value)
2626

2727

28-
def test_GIVEN_long_list_of_strings_WHEN_waveform_converted_to_string_THEN_result_contains_a_string_of_strings(
29-
):
28+
def test_GIVEN_long_list_of_strings_WHEN_waveform_converted_to_string_THEN_result_contains_a_string_of_strings():
3029
# Arrange
3130
test_waveform = ["this", "is", "a", "long", "list", "of", "strings!"]
3231

@@ -38,8 +37,7 @@ def test_GIVEN_long_list_of_strings_WHEN_waveform_converted_to_string_THEN_resul
3837
check_waveform(test_waveform, expected_value)
3938

4039

41-
def test_GIVEN_short_list_of_numbers_WHEN_waveform_converted_to_string_THEN_result_contains_string_of_unicode_chars_for_numbers(
42-
):
40+
def test_GIVEN_short_list_of_numbers_WHEN_waveform_converted_to_string_THEN_result_contains_string_of_unicode_chars_for_numbers():
4341
# Arrange
4442
test_waveform = [1, 2, 3, 4]
4543

@@ -51,8 +49,7 @@ def test_GIVEN_short_list_of_numbers_WHEN_waveform_converted_to_string_THEN_resu
5149
check_waveform(test_waveform, expected_value)
5250

5351

54-
def test_GIVEN_list_of_numbers_containing_0_WHEN_waveform_converted_to_string_THEN_result_terminates_at_character_before_0(
55-
):
52+
def test_GIVEN_list_of_numbers_containing_0_WHEN_waveform_converted_to_string_THEN_result_terminates_at_character_before_0():
5653
# Arrange
5754
test_waveform = [1, 2, 3, 4, 0, 5, 6, 7, 8, 9]
5855

@@ -64,8 +61,7 @@ def test_GIVEN_list_of_numbers_containing_0_WHEN_waveform_converted_to_string_TH
6461
check_waveform(test_waveform, expected_value)
6562

6663

67-
def test_GIVEN_long_list_of_numbers_WHEN_waveform_converted_to_string_THEN_result_contains_string_of_unicode_chars_for_numbers(
68-
):
64+
def test_GIVEN_long_list_of_numbers_WHEN_waveform_converted_to_string_THEN_result_contains_string_of_unicode_chars_for_numbers():
6965
# Arrange
7066
max_unichr = 128
7167
length = 1000
@@ -79,13 +75,12 @@ def test_GIVEN_long_list_of_numbers_WHEN_waveform_converted_to_string_THEN_resul
7975
check_waveform(test_waveform, expected_value)
8076

8177

82-
def test_GIVEN_negative_integer_in_waveform_WHEN_waveform_converted_to_string_THEN_result_raises_value_error(
83-
):
78+
def test_GIVEN_negative_integer_in_waveform_WHEN_waveform_converted_to_string_THEN_result_raises_value_error():
8479
# Arrange
8580
test_waveform = [-1]
8681

8782
# Act
8883

8984
# Assert
9085
with pytest.raises(ValueError):
91-
waveform_to_string(test_waveform)
86+
waveform_to_string(test_waveform)

0 commit comments

Comments
 (0)