Ejemplo n.º 1
0
 def encode_comment_dict(d: dict) -> str:
     """Convert a dict into a json string suitable for sending
     to the RFID reader as a comment
     """
     # NOTE: the leading space in the format string is required
     return " {}{}{}".format(BaseCommLink.DCT_START_CHAR,
                             qai_helper.tojson(d),
                             BaseCommLink.DCT_STOP_CHAR)
Ejemplo n.º 2
0
 def test_tojson02(self) -> None:
     """Encoding a user-defined class should work."""
     class bla:
         def __init__(self):
             self.bg = 'red'
             self.goo = 'blue'
     b = bla()
     rs = qai_helper.tojson(b)
     assert isinstance(rs, str), 'string expected'
Ejemplo n.º 3
0
 def test_safe_fromjson(self) -> None:
     """
     safe_fromjson should provide a data structure or None
     """
     avar = dict(a=100.0, b=99, c='hello', d=[12, 'blu'])
     astr = qai_helper.tojson(avar)
     bstr = "this is not json"
     for var, exp_res in [(astr, avar), (bstr, None)]:
         res = qai_helper.safe_fromjson(var)
         assert res == exp_res, "got unexpected result"
Ejemplo n.º 4
0
 def test_jsonconvert(self) -> None:
     """Encode and decoding should produce the same data structure.
     NOTE: tuple are converted into lists in json -- therefore our tests
     should not use them.
     NOTE: complex numbers come back as strings -- do not test either.
     NOTE: sets cannot be used either.
     """
     a = dict(a=100.0, b=99, c='hello', d=[12, 'blu'])
     b = [100.0, -99, 'bla', ['sub', 'list']]
     c = [1, 2, 3]
     for var in [a, b, c]:
         transmit_bytes = qai_helper.tojson(var)
         got_var = qai_helper.fromjson(transmit_bytes)
         assert got_var == var, "variable is not the same "
Ejemplo n.º 5
0
 def test_tojson01(self) -> None:
     """Encoding a dict with a complex key raises a TypeError"""
     s = {(1, 2): 'hello'}
     with pytest.raises(TypeError):
         qai_helper.tojson(s)
Ejemplo n.º 6
0
 def encodeMSG(self, msg: WebsocketMSG) -> typing.Any:
     """Encode the input message into a json string for transmission over websocket.
     """
     return qai_helper.tojson(msg)
Ejemplo n.º 7
0
 def _set_data(self, data: typing.Any = None) -> None:
     self.retdat: typing.Optional[bytes] = None if data is None else bytes(
         qai_helper.tojson(data), 'utf-8')