Browse Source

refactor: use `bytes.hex()`

https://bugs.python.org/issue9951
Fabian Peter Hammerle 3 years ago
parent
commit
b89519d61e
4 changed files with 46 additions and 17 deletions
  1. 3 9
      cc1101/__init__.py
  2. 1 0
      tests/test_config.py
  3. 29 7
      tests/test_str.py
  4. 13 1
      tests/test_transmit.py

+ 3 - 9
cc1101/__init__.py

@@ -103,9 +103,7 @@ class _ReceivedPacket:  # unstable
 
     def __str__(self) -> str:
         return "{}(RSSI {:.0f}dBm, 0x{})".format(
-            type(self).__name__,
-            self.rssi_dbm,
-            "".join("{:02x}".format(b) for b in self.payload),
+            type(self).__name__, self.rssi_dbm, self.payload.hex()
         )
 
 
@@ -666,7 +664,7 @@ class CC1101:
             "preamble_length={}B".format(self.get_preamble_length_bytes())
             if sync_mode != SyncMode.NO_PREAMBLE_AND_SYNC_WORD
             else None,
-            "sync_word=0x{:02x}{:02x}".format(*self.get_sync_word())
+            "sync_word=0x{}".format(self.get_sync_word().hex())
             if sync_mode != SyncMode.NO_PREAMBLE_AND_SYNC_WORD
             else None,
             "packet_length{}{}B".format(
@@ -909,11 +907,7 @@ class CC1101:
             )
         self._flush_tx_fifo_buffer()
         self._write_burst(FIFORegisterAddress.TX, list(payload))
-        _LOGGER.info(
-            "transmitting 0x%s (%r)",
-            "".join("{:02x}".format(b) for b in payload),
-            payload,
-        )
+        _LOGGER.info("transmitting 0x%s (%r)", payload.hex(), payload)
         self._command_strobe(StrobeAddress.STX)
 
     @contextlib.contextmanager

+ 1 - 0
tests/test_config.py

@@ -183,6 +183,7 @@ def test_get_output_power(transceiver, patable, patable_index, power_levels):
         (1, (198, 0)),  # library's default
         (1, (0, 198)),
         (1, [0, 198]),
+        (1, b"\0\x6c"),
         (7, (0, 1, 2, 3, 4, 5, 21, 42)),
     ),
 )

+ 29 - 7
tests/test_str.py

@@ -1,9 +1,29 @@
 import unittest.mock
 
+import pytest
+
 import cc1101
 
 
-def test___str___():
+@pytest.mark.parametrize(
+    ("transceiver_str", "sync_word"),
+    (
+        (
+            "CC1101(marcstate=idle, base_frequency=433.92MHz, "
+            "symbol_rate=2.14kBaud, modulation_format=ASK_OOK, "
+            "sync_mode=NO_PREAMBLE_AND_SYNC_WORD, packet_length=21B, output_power=(0,0xc0))",
+            None,
+        ),
+        (
+            "CC1101(marcstate=idle, base_frequency=433.92MHz, "
+            "symbol_rate=2.14kBaud, modulation_format=ASK_OOK, "
+            "sync_mode=TRANSMIT_16_MATCH_15_BITS, preamble_length=4B, sync_word=0x01ef, "
+            "packet_length=21B, output_power=(0,0xc0))",
+            b"\x01\xef",
+        ),
+    ),
+)
+def test___str___(transceiver_str, sync_word):
     transceiver = cc1101.CC1101()
     with unittest.mock.patch.object(
         transceiver,
@@ -20,7 +40,13 @@ def test___str___():
     ), unittest.mock.patch.object(
         transceiver,
         "get_sync_mode",
-        return_value=cc1101.SyncMode.NO_PREAMBLE_AND_SYNC_WORD,
+        return_value=cc1101.SyncMode.TRANSMIT_16_MATCH_15_BITS
+        if sync_word
+        else cc1101.SyncMode.NO_PREAMBLE_AND_SYNC_WORD,
+    ), unittest.mock.patch.object(
+        transceiver, "get_preamble_length_bytes", return_value=4
+    ), unittest.mock.patch.object(
+        transceiver, "get_sync_word", return_value=sync_word
     ), unittest.mock.patch.object(
         transceiver,
         "get_packet_length_mode",
@@ -30,8 +56,4 @@ def test___str___():
     ), unittest.mock.patch.object(
         transceiver, "get_output_power", return_value=(0, 0xC0)
     ):
-        assert (
-            str(transceiver) == "CC1101(marcstate=idle, base_frequency=433.92MHz, "
-            "symbol_rate=2.14kBaud, modulation_format=ASK_OOK, "
-            "sync_mode=NO_PREAMBLE_AND_SYNC_WORD, packet_length=21B, output_power=(0,0xc0))"
-        )
+        assert str(transceiver) == transceiver_str

+ 13 - 1
tests/test_transmit.py

@@ -1,3 +1,4 @@
+import logging
 import unittest.mock
 
 import pytest
@@ -54,7 +55,7 @@ def test_transmit_unexpected_payload_len(transceiver, packet_length, payload):
 
 
 @pytest.mark.parametrize("payload", (b"\0", b"\xaa\xbb\xcc", bytes(range(42))))
-def test_transmit_fixed(transceiver, payload):
+def test_transmit_fixed(caplog, transceiver, payload):
     transceiver._spi.xfer.side_effect = lambda v: [15] * len(v)
     with unittest.mock.patch.object(
         transceiver,
@@ -66,6 +67,8 @@ def test_transmit_fixed(transceiver, payload):
         transceiver,
         "get_main_radio_control_state_machine_state",
         return_value=cc1101.MainRadioControlStateMachineState.IDLE,
+    ), caplog.at_level(
+        logging.INFO
     ):
         transceiver.transmit(payload)
     assert transceiver._spi.xfer.call_args_list == [
@@ -73,6 +76,15 @@ def test_transmit_fixed(transceiver, payload):
         unittest.mock.call([0x3F | 0x40] + list(payload)),
         unittest.mock.call([0x35]),  # start transmission
     ]
+    assert caplog.record_tuples == [
+        (
+            "cc1101",
+            20,
+            "transmitting 0x{} ({!r})".format(
+                "".join("{:02x}".format(b) for b in payload), payload
+            ),
+        )
+    ]
 
 
 @pytest.mark.parametrize(