Skip to main content

About

This simple modules provides conversion between data structures and objects structures.

This is allows to get the advantage of both:

  • data structures:
    • Convenient to save the state and share in an explicit format such as json format.
    • Convenient to generate.
  • object structures:
    • Provide methods to the objects. Convenient to perform operations relative to an object.

How to use

Objects are represented by a dictionary containing the special _obj key and the associated string corresponds to the name of the object that has to be created. The other attributes of the dictionary will be set as object attributes, wich can also contain lists, dictionaries and other objects.

{
    '_obj': 'Boat',
    'country_flag': 'NZ',
    'passengers': 3
}

The set of objects which can be created

  • must be registered using the @structobj.register decorator.
  • must inherit the structobj.StructObj class

The object structure can be created using the structobj.make_obj_struct(data) function.

The data structure of an object structure can be accessed using the obj.get_data() method.

Installation

pip3 install objstruct

Example

from src import structobj as so


@so.register
class Earth(so.StructObj):
    def __init__(self, data):
        super().__init__(data)
        print('New earth')


@so.register
class Ocean(so.StructObj):
    def __init__(self, data):
        super().__init__(data)
        print('New ocean')


@so.register
class Boat(so.StructObj):
    def __init__(self, data):
        super().__init__(data)
        print('New boat')


data_struct = {
    '_obj': 'Earth',
    'color': 'blue',
    'oceans': [
        {
            '_obj': 'Ocean',
            'name': 'pacific',
            'boats': [
                {
                    '_obj': 'Boat',
                    'country_flag': 'NZ',
                    'passengers': 3
                },
                {
                    '_obj': 'Boat',
                    'country_flag': 'IN',
                    'passengers': 20
                }
            ]
        }
    ]
}

obj_struct = so.make_obj_struct(data_struct)
print(obj_struct)
print(obj_struct.color)
print(obj_struct.oceans)
print(obj_struct.oceans[0].name)
print(obj_struct.oceans[0].boats)
print(obj_struct.oceans[0].boats[1].passengers)

print(obj_struct.get_data() == data_struct)

This will give:

New boat
New boat
New ocean
New earth
<__main__.Earth object at 0x7f311930e590>
blue
[<__main__.Ocean object at 0x7f311930f0d0>]
pacific
[<__main__.Boat object at 0x7f311930f090>, <__main__.Boat object at 0x7f31194c2950>]
20
True

Release files for structobj 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for structobj 0.0.1
File Size Uploaded
structobj-0.0.1.tar.gz 2.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for structobj 0.0.1
File Interpreter ABI Platform
structobj-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 6.4 kB

Release files / structobj-0.0.1.tar.gz

Download URL structobj-0.0.1.tar.gz
Size 2.9 kB
Tags Source
SHA-256 checksum
How to use checksums
720848405e3a360a60ff79c2e9c667e7813986712849ce65bc68497064320dde
BLAKE2b-256 checksum
How to use checksums
4050ff932768c2c2d681987135bf117794ddbb8b76deb21c292c8bd6bda8ab13
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release files / structobj-0.0.1-py3-none-any.whl

Download URL structobj-0.0.1-py3-none-any.whl
Size 3.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3ad438569445e5a9f021f95ee0e053a98b5ad9779a84a5c0850fb201a6461fd8
BLAKE2b-256 checksum
How to use checksums
f36af2ff1c0cd83ec1968ae9476cf6a20e3c42c50bf0dcc899e0f114a8257134
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.4

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page