### Install sbe-python Library Source: https://github.com/kizzx2/sbe-python/blob/master/README.md Provides the command line instruction to install the sbe-python library using the pip package manager. ```bash pip install sbe ``` -------------------------------- ### SBE Encoding in Python Source: https://github.com/kizzx2/sbe-python/blob/master/README.md Provides an example of how to encode a Python dictionary into an SBE formatted byte buffer using a parsed schema. Shows encoding a message by its ID and optionally supplying header values. ```python import sbe with open('./your-schema.xml', 'r') as f: schema = sbe.Schema.parse(f) # message_id from the schema you want to encode message_id = 3 obj = { 'userId': 11, 'price': 5678.0, # ... } # Encode from Python dict in one-line schema.encode(schema.messages[3], obj) # You can supply your header values as a dict schema.encode(schema.messages[3], obj, headers) ``` -------------------------------- ### Simple SBE Decoding in Python Source: https://github.com/kizzx2/sbe-python/blob/master/README.md Demonstrates how to parse an SBE schema from an XML file and decode an SBE data buffer into a standard Python dictionary for easy access. Includes examples for decoding with an initial offset and decoding only the message header. ```python import sbe with open('your-schema.xml', 'r') as f: schema = sbe.Schema.parse(f) wtih open('your-data.sbe', 'rb') as f: buf = f.read() # Get a Python dict in one-line x = schema.decode(buf) x.name # The template message name x.value # {'userId': 11, # 'timestamp': 1598784004840, # 'orderSize': 0, # 'price': 5678.0, # ... # If you need an initial offset, apply it Pythonically schema.decode(buf[19:]) # decode_header to filter out messages based on header to avoid decoding # message bodies that are not needed schema.decode_header(buf)['templateId'] ``` -------------------------------- ### High Performance SBE Decoding (Wrapping) Source: https://github.com/kizzx2/sbe-python/blob/master/README.md Shows how to use the `wrap` method to create a wrapper object around the SBE buffer. This approach avoids full decoding upfront, converting fields to Python variables on demand for improved performance while maintaining readability. ```python import sbe with open('your-schema.xml', 'r') as f: schema = sbe.Schema.parse(f) wtih open('your-data.sbe', 'rb') as f: buf = f.read() # Wrap the buffer without decoding it, fields are converted to Python variables # on demand x = schema.wrap(buf) x.header['templateId'] x.body['price'] x.body['someGroup'][2]['price'] ``` -------------------------------- ### Direct Access SBE Decoding with Pointers Source: https://github.com/kizzx2/sbe-python/blob/master/README.md Illustrates the highest performance decoding method using `get_raw_pointer` and `unpack` with a `memoryview`. This allows direct access to specific fields within the buffer without intermediate copies, suitable for performance-critical applications. ```python import sbe with open('your-schema.xml', 'r') as f: schema = sbe.Schema.parse(f) header_pointer = schema.header_wrapper.get_raw_pointer('templateId') # Let's say we are only interested in messages of templateId == 3 price_pointer = schema.message_wrappers[3].get_raw_pointer('price') wtih open('your-data.sbe', 'rb') as f: buf = f.read() # pass `memoryview` to `unpack` to avoid copying buf = memoryview(buf)[initial_offset:] template_id = header_pointer.unpack(buf) # calls buf[offset:offset+size].cast("I")[0] directly if template_id == 3: print(price_pointer.unpack(buf)) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.