Skip to main content

Conversion between data structures and object strutures

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

structobj-0.0.1.tar.gz (2.9 kB view details)

Uploaded Source

Built Distribution

structobj-0.0.1-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file structobj-0.0.1.tar.gz.

File metadata

  • Download URL: structobj-0.0.1.tar.gz
  • Upload date:
  • Size: 2.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for structobj-0.0.1.tar.gz
Algorithm Hash digest
SHA256 720848405e3a360a60ff79c2e9c667e7813986712849ce65bc68497064320dde
MD5 65f8d711164b6ba1eeda9b5ee1d795d4
BLAKE2b-256 4050ff932768c2c2d681987135bf117794ddbb8b76deb21c292c8bd6bda8ab13

See more details on using hashes here.

File details

Details for the file structobj-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: structobj-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for structobj-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3ad438569445e5a9f021f95ee0e053a98b5ad9779a84a5c0850fb201a6461fd8
MD5 e615251fe2c714663c73fbddd09af8ff
BLAKE2b-256 f36af2ff1c0cd83ec1968ae9476cf6a20e3c42c50bf0dcc899e0f114a8257134

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page