Metadata-Version: 1.1
Name: Struction
Version: 0.2.0
Summary: Pythonic, C-style structs.
Home-page: http://github.com/reshanie/struction
Author: Patrick Dill
Author-email: jamespatrickdill@gmail.com
License: MIT
Download-URL: http://github.com/reshanie/struction/archive/master.tar.gz
Description: struction
        ---------
        Pythonic, C-Style structs.
        
        Structs are similar to namedtuples, but they allow type assertion and are defined the same way as any class. Creating
        a struct is easy.
        
        ``$ pip install struction``
        
        .. code-block:: python
        
            from struction import Struct, default
        
            class MyStruct(Struct):
                field_0 = int
                field_1 = str
        
                # you can also allow multiple types
                multi_type = int, str
        
                # and default values!
                with_default = int, default(10)
        
                # and of course, all of that can be mixed together.
                with_multi_and_default = int, str, default(10)
        
        
        Once a struct is created, it's fields can be changed, but they must match the given type or
        a TypeError will be raised.
        
        ``del struct.field`` resets the field to ``None``.
        
        It's also possible to nest structs.
        Any structs that are nested will automatically be initialized.
        
        
        .. code-block:: python
        
            class OnlyInt(Struct):
                field_0 = int
                field_1 = int
        
        
            class Nested(Struct):
                abc = str
                nest = OnlyInt  # nested struct
        
        
        .. code-block:: python
        
            >>> print(Nested())
            # Struct Nested {
            #     abc = None
            #     nest = OnlyInt {
            #         field_0 = None
            #         field_1 = None
            #     }
            # }
        
            >>> print(Nested(nest=None))
            # Struct Nested {
            #     abc = None
            #     nest = None
            # }
        
        If you don't want strict types, you can also use a ``TypecastingStruct``. This will attempt to typecast the given value
        to the field's type. If it can't be typecasted, it will then raise a TypeError.
        
        .. code-block:: python
        
            from struction import TypecastingStruct
        
            class Test(TypecastingStruct):
                i = int
                f = float
                s = str
                all = float, int, str
        
        .. code-block:: python
        
            >>> test = Test()
            >>> test.i = 5.3
            >>> test.f = 100
            >>> test.s = {"a": 1, "b":2}
            >>> print(test)
            # Struct Test {
            #     all = None
            #     f = 100.0
            #     i = 5
            #     s = "{'a': 1, 'b': 2}"
            # }
            >>> # If multiple types are allowed for a field, the value will be
            >>> # casted to the first type that doesn't throw an Exception
            >>> test.all = ("a", 1, "b", 2, "c", 3)
            >>> test.all
            # '("a", 1, "b", 2, "c", 3)'
        
        **Note:** Typecasting only works at runtime. The values still need to match their types at class definition.
        
        Reference
        ---------
        These can be applied to any Struct class.
        
        - ``Struct.dict()`` : dict with struct's fields. {name: value, ...}
        - ``Struct.fields()`` : list of fields struct has.
        
Keywords: structs struct datastructure data
Platform: UNKNOWN
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.0
Classifier: Programming Language :: Python :: 3.1
Classifier: Programming Language :: Python :: 3.2
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Intended Audience :: Developers
