def tag_from_key(key): """ return tag from key Tries to convert key to bytes, and return the string enclosed by '{' and '}', if any. If there is no pair of curly braces enclosing a string, the key, converted to bytes, is returned. :param key: str, bytes :return: bytes """ key = to_bytes(key) lcb = key.find(b'{') rcb = key.find(b'}') if not (lcb >= 0 and rcb >= 0): return key else: return key[lcb+1:rcb]
def test_bytes(self): expected = b'0815' result = to_bytes(b'0815') self.assertEqual(result, expected)
def test_float(self): expected = b'0.815' result = to_bytes(0.815) self.assertEqual(result, expected)
def test_str(self): expected = b'\xc3\xbc\xc3\x9f_blarg' result = to_bytes('üß_blarg') self.assertEqual(result, expected)
def test_int(self): expected = b'512' result = to_bytes(512) self.assertEqual(result, expected)