当前位置: 首页>>代码示例>>Python>>正文


Python MessageTemplate.decode方法代码示例

本文整理汇总了Python中Rammbock.templates.containers.MessageTemplate.decode方法的典型用法代码示例。如果您正苦于以下问题:Python MessageTemplate.decode方法的具体用法?Python MessageTemplate.decode怎么用?Python MessageTemplate.decode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Rammbock.templates.containers.MessageTemplate的用法示例。


在下文中一共展示了MessageTemplate.decode方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestMessageTemplate

# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import decode [as 别名]
class TestMessageTemplate(TestCase):

    def setUp(self):
        self._protocol = Protocol('TestProtocol')
        self._protocol.add(UInt(2, 'msgId', 5))
        self._protocol.add(UInt(2, 'length', None))
        self._protocol.add(PDU('length-4'))
        self.tmp = MessageTemplate('FooRequest', self._protocol, {})
        self.tmp.add(UInt(2, 'field_1', 1))
        self.tmp.add(UInt(2, 'field_2', 2))

    def test_create_template(self):
        self.assertEquals(len(self.tmp._fields), 2)

    def test_encode_template(self):
        msg = self.tmp.encode({}, {})
        self.assertEquals(msg.field_1.int, 1)
        self.assertEquals(msg.field_2.int, 2)

    def test_message_field_type_conversions(self):
        msg = self.tmp.encode({'field_1': 1024}, {})
        self.assertEquals(msg.field_1.int, 1024)
        self.assertEquals(msg.field_1.hex, '0x0400')
        self.assertEquals(msg.field_1.bytes, '\x04\x00')

    def test_encode_template_with_params(self):
        msg = self.tmp.encode({'field_1': 111, 'field_2': 222}, {})
        self.assertEquals(msg.field_1.int, 111)
        self.assertEquals(msg.field_2.int, 222)

    def test_encode_template_header(self):
        msg = self.tmp.encode({}, {})
        self.assertEquals(msg._header.msgId.int, 5)
        self.assertEquals(msg._header.length.int, 8)

    def test_encode_to_bytes(self):
        msg = self.tmp.encode({}, {})
        self.assertEquals(msg._header.msgId.int, 5)
        self.assertEquals(msg._raw, to_bin_of_length(8, '0x0005 0008 0001 0002'))

    def test_pretty_print(self):
        msg = self.tmp.encode({}, {})
        self.assertEquals(msg._header.msgId.int, 5)
        self.assertEquals(str(msg), 'Message FooRequest')
        self.assertEquals(repr(msg),
                          """Message FooRequest
  Header TestProtocol
    msgId = 5 (0x0005)
    length = 8 (0x0008)
  field_1 = 1 (0x0001)
  field_2 = 2 (0x0002)
""")

    def test_unknown_params_cause_exception(self):
        self.assertRaises(AssertionError, self.tmp.encode, {'unknown': 111}, {})

    def test_decode_message(self):
        msg = self.tmp.decode(to_bin('0xcafebabe'))
        self.assertEquals(msg.field_1.hex, '0xcafe')
开发者ID:caseyding,项目名称:Rammbock,代码行数:61,代码来源:test_message_templates.py

示例2: test_decode_dynamic_list

# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import decode [as 别名]
 def test_decode_dynamic_list(self):
     tmp = MessageTemplate('Dymagic', self._protocol, {})
     tmp.add(UInt(2, 'len', None))
     lst = ListTemplate('len', 'foo', parent=None)
     lst.add(UInt(1, 'bar', None))
     tmp.add(lst)
     decoded = tmp.decode(to_bin('0x 00 04 6162 6364'))
     self.assertEquals(decoded.len.int, 4)
     self.assertEquals(decoded.foo[0].hex, '0x61')
开发者ID:embeddedninja,项目名称:Rammbock,代码行数:11,代码来源:test_containers.py

示例3: TestMessageTemplateValidation

# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import decode [as 别名]
class TestMessageTemplateValidation(TestCase):

    def setUp(self):
        self._protocol = Protocol('TestProtocol')
        self._protocol.add(UInt(2, 'msgId', 5))
        self._protocol.add(UInt(2, 'length', None))
        self._protocol.add(PDU('length-4'))
        self.tmp = MessageTemplate('FooRequest', self._protocol, {})
        self.tmp.add(UInt(2, 'field_1', '0xcafe'))
        self.tmp.add(UInt(2, 'field_2', '0xbabe'))
        self.example = self.tmp.encode({}, {})

    def test_validate_passing_hex(self):
        msg = self._decode_and_set_fake_header('0xcafebabe')
        errors = self.tmp.validate(msg, {}, {})
        self.assertEquals(errors, [])

    def test_validate_error_default_value(self):
        msg = self._decode_and_set_fake_header('0xcafedead')
        errors = self.tmp.validate(msg, {}, {})
        self.assertEquals(errors, ['Value of field field_2 does not match 0xdead!=0xbabe'])

    def test_validate_error_override(self):
        msg = self._decode_and_set_fake_header('0xcafebabe')
        errors = self.tmp.validate(msg, {'field_2': '0xdead'}, {})
        self.assertEquals(errors, ['Value of field field_2 does not match 0xbabe!=0xdead'])

    def test_validate_two_errors(self):
        msg = self._decode_and_set_fake_header('0xbeefbabe')
        errors = self.tmp.validate(msg, {'field_2': '0xdead'}, {})
        self.assertEquals(len(errors), 2)

    def test_validate_pattern_pass(self):
        msg = self._decode_and_set_fake_header('0xcafe0002')
        errors = self.tmp.validate(msg, {'field_2': '(0|2)'}, {})
        self.assertEquals(len(errors), 0)

    def test_validate_pattern_failure(self):
        msg = self._decode_and_set_fake_header('0xcafe0002')
        errors = self.tmp.validate(msg, {'field_2': '(0|3)'}, {})
        self.assertEquals(len(errors), 1)

    def test_validate_passing_int(self):
        msg = self._decode_and_set_fake_header('0xcafe0200')
        errors = self.tmp.validate(msg, {'field_2': '512'}, {})
        self.assertEquals(errors, [])

    def _decode_and_set_fake_header(self, bin_value):
        msg = self.tmp.decode(to_bin(bin_value))
        msg['_header'] = self.example._header
        return msg

    def test_failing_passing_int(self):
        msg = self._decode_and_set_fake_header('0xcafe0200')
        errors = self.tmp.validate(msg, {'field_2': '513'}, {})
        self.assertEquals(len(errors), 1)
开发者ID:caseyding,项目名称:Rammbock,代码行数:58,代码来源:test_message_templates.py

示例4: test_decode_dynamic_primitive

# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import decode [as 别名]
 def test_decode_dynamic_primitive(self):
     tmp = MessageTemplate('Dymagic', self._protocol, {})
     tmp.add(UInt(1, 'len', None))
     tmp.add(Char('len', 'chars', None))
     tmp.add(UInt(1, 'len2', None))
     tmp.add(Char('len2', 'chars2', None))
     decoded = tmp.decode(to_bin('0x 04 6162 6364 02 6566'))
     self.assertEquals(decoded.len.int, 4)
     self.assertEquals(decoded.chars.ascii, 'abcd')
     self.assertEquals(decoded.chars2.ascii, 'ef')
开发者ID:embeddedninja,项目名称:Rammbock,代码行数:12,代码来源:test_containers.py


注:本文中的Rammbock.templates.containers.MessageTemplate.decode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。