{"id":22689159,"url":"https://github.com/flokapi/structobj","last_synced_at":"2025-03-29T16:15:43.105Z","repository":{"id":198041025,"uuid":"699955841","full_name":"flokapi/structobj","owner":"flokapi","description":"Conversion between data structures and object strutures","archived":false,"fork":false,"pushed_at":"2023-10-03T21:33:31.000Z","size":4,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-11T02:46:19.275Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flokapi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2023-10-03T17:01:23.000Z","updated_at":"2025-02-20T16:32:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"76888b38-bf27-4e16-b987-ebc7656cb303","html_url":"https://github.com/flokapi/structobj","commit_stats":null,"previous_names":["flokapi/structobj"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flokapi%2Fstructobj","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flokapi%2Fstructobj/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flokapi%2Fstructobj/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flokapi%2Fstructobj/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flokapi","download_url":"https://codeload.github.com/flokapi/structobj/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246207507,"owners_count":20740723,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-10T00:17:51.111Z","updated_at":"2025-03-29T16:15:43.068Z","avatar_url":"https://github.com/flokapi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# About\n\nThis simple Python package provides conversion between data structures and objects structures.\n\n\n\nThis is allows to get the advantage of both:\n\n- data structures:\n    - Convenient to save the state and share in an explicit format such as json format.\n    - Convenient to generate.\n- object structures:\n    - Provide methods to the objects. \n    - Convenient to perform operations relative to an object.\n\n\n\n# How to use\n\nObjects 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.\n\n```python\n{\n    '_obj': 'Boat',\n    'country_flag': 'NZ',\n    'passengers': 3\n}\n```\n\n\n\nThe set of objects which can be created\n\n- must be registered using the `@structobj.register` decorator.\n- must inherit the `structobj.StructObj` class.\n- must have a `data` parameter in the constructor and use it to initialize the parent class.\n\n```python\n@so.register\nclass Earth(so.StructObj):\n    def __init__(self, data):\n        super().__init__(data)\n        print('New earth')\n```\n\n\n\nThe object structure can be created using the `structobj.make_obj_struct()` function.\n\n```python\nobj_struct = so.make_obj_struct(data_struct)\n```\n\n\n\nThe data structure of an object structure can be accessed using the  `.get_data()` method.\n\n```python\nobj_struct.get_data()\n```\n\n\n\n\n\n# Installation\n\n```\npip3 install objstruct\n```\n\n\n\n# Example\n\n```python\nfrom src import structobj as so\n\n\n@so.register\nclass Earth(so.StructObj):\n    def __init__(self, data):\n        super().__init__(data)\n        print('New earth')\n\n\n@so.register\nclass Ocean(so.StructObj):\n    def __init__(self, data):\n        super().__init__(data)\n        print('New ocean')\n\n\n@so.register\nclass Boat(so.StructObj):\n    def __init__(self, data):\n        super().__init__(data)\n        print('New boat')\n\n\ndata_struct = {\n    '_obj': 'Earth',\n    'color': 'blue',\n    'oceans': [\n        {\n            '_obj': 'Ocean',\n            'name': 'pacific',\n            'boats': [\n                {\n                    '_obj': 'Boat',\n                    'country_flag': 'NZ',\n                    'passengers': 3\n                },\n                {\n                    '_obj': 'Boat',\n                    'country_flag': 'IN',\n                    'passengers': 20\n                }\n            ]\n        }\n    ]\n}\n\nobj_struct = so.make_obj_struct(data_struct)\nprint(obj_struct)\nprint(obj_struct.color)\nprint(obj_struct.oceans)\nprint(obj_struct.oceans[0].name)\nprint(obj_struct.oceans[0].boats)\nprint(obj_struct.oceans[0].boats[1].passengers)\n\nprint(obj_struct.get_data() == data_struct)\n```\n\n\n\nThis will give:\n\n```\nNew boat\nNew boat\nNew ocean\nNew earth\n\u003c__main__.Earth object at 0x7f311930e590\u003e\nblue\n[\u003c__main__.Ocean object at 0x7f311930f0d0\u003e]\npacific\n[\u003c__main__.Boat object at 0x7f311930f090\u003e, \u003c__main__.Boat object at 0x7f31194c2950\u003e]\n20\nTrue\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflokapi%2Fstructobj","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflokapi%2Fstructobj","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflokapi%2Fstructobj/lists"}