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


Python Struct.pack_into方法代码示例

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


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

示例1: test_struct_translations

# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import pack_into [as 别名]
    def test_struct_translations(self):
 
        distance_component_struct = Struct('iiiff')
        struct_size = distance_component_struct.size
        bytes = ctypes.create_string_buffer(struct_size*2) 
        for j in range(2):
            i = j+1
            distance_component_struct.pack_into(bytes,struct_size*j, 1*i,2*i,3*i,4*i,5*i)
        result = test_dump_dist_comp(bytes)
        expected = EXPECTED_3
        self.assertEqual(result, expected)
开发者ID:locsmith,项目名称:xcamshift,代码行数:13,代码来源:test_component_list.py

示例2: _build_native_component_offsets

# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import pack_into [as 别名]
 def _build_native_component_offsets(self):
     struct =  Struct('iii')
     
     ids =  self.get_component_atom_ids()
     num_ids = 0
     if len(ids) > 0:
         num_ids = max(ids)+1
     struct_size = struct.size
     bytes =  ctypes.create_string_buffer(struct_size * num_ids)
      
     for id in range(num_ids):
         if id in ids:
             start,end = self.get_component_range(id)
             native_component = id,start,end-start
         else:
             native_component = id,-1,-1
         struct.pack_into(bytes, struct_size * id, *native_component)
     
     return bytes
开发者ID:locsmith,项目名称:xcamshift,代码行数:21,代码来源:component_list.py

示例3: Native_component_list

# 需要导入模块: from struct import Struct [as 别名]
# 或者: from struct.Struct import pack_into [as 别名]
class Native_component_list(Component_list):
    def __init__(self, format, translator = lambda x : x, preparer = lambda x : x):
        super(Native_component_list, self).__init__()
        #TODO remove and simplifiy!
        self.set_translator(translator)
        self.set_format(format)    
        self._native_components =  None
        self._native_component_offsets = None
        self.set_preparer(preparer)

   
    def set_translator(self, translator):
        self._translator =  translator
        
    def set_preparer(self,preparer):
        self._preparer =  preparer
        
    def set_format(self,format):
        self._component_struct =  Struct(format)
    
    def _reset(self):
        self._native_components =  None
        self._native_component_offsets = None
                   
    def _basic_add_component(self,component):
        super(Native_component_list, self)._basic_add_component(component)
        self._reset()
        
    def clear(self):
        super(Native_component_list, self).clear()
        self._reset()
            
    def _translate_to_native_component(self, component):
        return self._translator(component)
    
    def _prepare_component_list(self, components):
        return self._preparer(components)

    def _build_native_components(self):
        
        target_components =  self._prepare_component_list(self._components)
        num_components = len(target_components)
        if num_components > 0 :
            format_length = len(self._component_struct.format)
            data_length =  len(self._translate_to_native_component(target_components[0]))
            
            if format_length != data_length:
                msg = 'data and format must have the same length, got len(fomat) = %i and len(data) = %i'
                raise Exception(msg % (format_length, data_length))
        
        struct_size = self._component_struct.size
        bytes = ctypes.create_string_buffer(struct_size * num_components)
        for i in range(num_components):
            native_component = self._translate_to_native_component(target_components[i])
            self._component_struct.pack_into(bytes, struct_size * i, *native_component)
        
        return bytes

    def get_native_components(self):
        if self._native_components == None:
            self._native_components = self._build_native_components()
        bytes = self._native_components 
        
        return bytes
       
    def _build_native_component_offsets(self):
        struct =  Struct('iii')
        
        ids =  self.get_component_atom_ids()
        num_ids = 0
        if len(ids) > 0:
            num_ids = max(ids)+1
        struct_size = struct.size
        bytes =  ctypes.create_string_buffer(struct_size * num_ids)
         
        for id in range(num_ids):
            if id in ids:
                start,end = self.get_component_range(id)
                native_component = id,start,end-start
            else:
                native_component = id,-1,-1
            struct.pack_into(bytes, struct_size * id, *native_component)
        
        return bytes

    def get_native_component_offsets(self):
        if self._native_component_offsets == None:
            self._native_component_offsets = self._build_native_component_offsets()
        bytes = self._native_component_offsets
        return bytes
开发者ID:locsmith,项目名称:xcamshift,代码行数:92,代码来源:component_list.py


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