Skip to content

User friendly implementation of C-like structure in Python language

License

Notifications You must be signed in to change notification settings

molejar/pyStruct

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

pyStruct

User friendly implementation of C-like structure in Python language.

Dependencies

  • Python - Python 3.x interpreter
  • easy_enum - User friendly implementation of documented Enum type for Python language.

Installation

To install the latest version from master branch execute in shell following commands:

    $ pip install -U https://github.com/molejar/pyStruct/archive/master.zip

In case of development, install pyStruct from sources:

    $ git clone https://github.com/molejar/pyStruct.git
    $ cd pyStruct
    $ pip install -U -e .

You may run into a permissions issues running these commands. Here are a few options how to fix it:

  1. Run with sudo to install pyStruct and dependencies globally
  2. Specify the --user option to install locally into your home directory (export "~/.local/bin" into PATH variable if haven't).
  3. Run the command in a virtualenv local to a specific project working set.

Usage

Example code:

    import re
    from easy_enum import Enum
    from easy_struct import *

    class MyEnum(Enum):
        """ Example of Enum """
        ITEM1 = (1, 'first', 'Description for first item')   
        ITEM2 = (2, 'second', 'Description for second item')
        ITEM3 = (3, 'third', 'Description for third item')
        ITEM4 = (4, 'fourth', 'Description for fourth item')
    
    class MyStruct(DataStructure):
        """ Example of Data Structure """
    
        # public member with string type
        name: String(length=14, default="example string")
        
        # private members with 16-bit unsigned little endian type
        _major_version: Int16ul
        _minor_version: Int16ul
        
        # others public members
        flags:   Int8ul(default=MyEnum.ITEM2, choices=MyEnum)
        user_id: Int16ul(default=0xF0F125D, fmt='X')
        
        # reserved bytes can be defined as private member or skipt with offset parameter
        # _reserved: Int32ul
        temperature: Int8sl(default=22, min=-20, max=100, offset=4)
        
        # bytes array
        data: Bytes(length=24)
    
        @property
        def version(self):
            return f"{self._major_version}.{self._minor_version}"

        @version.setter
        def version(self, value):
            assert isinstance(value, str) and re.match(r'^\d+\.\d+$, value)
            self._major_version, self._minor_version = [int(v) for v in value.split('.')]
        
        # user methods
        # ...
        
    if __name__ == '__main__':
        # main code
        ms = MyStruct()
        print(ms.info())
        data = ms.export()
        parsed_ms = MyStruct.parse(data)
        print(parsed_ms.info())

About

User friendly implementation of C-like structure in Python language

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages