{"id":18905499,"url":"https://github.com/utility-libraries/structurelib-py","last_synced_at":"2025-08-16T23:06:19.669Z","repository":{"id":213356026,"uuid":"733860174","full_name":"utility-libraries/structurelib-py","owner":"utility-libraries","description":"similar function to the builtin struct-library but more friendly","archived":false,"fork":false,"pushed_at":"2024-07-27T10:35:06.000Z","size":40,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-05-24T12:42:28.661Z","etag":null,"topics":["python3","structures"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/utility-libraries.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-12-20T09:45:52.000Z","updated_at":"2024-07-27T10:35:09.000Z","dependencies_parsed_at":"2024-07-27T11:53:32.120Z","dependency_job_id":null,"html_url":"https://github.com/utility-libraries/structurelib-py","commit_stats":null,"previous_names":["utility-libraries/structurelib-py"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/utility-libraries/structurelib-py","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utility-libraries%2Fstructurelib-py","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utility-libraries%2Fstructurelib-py/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utility-libraries%2Fstructurelib-py/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utility-libraries%2Fstructurelib-py/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/utility-libraries","download_url":"https://codeload.github.com/utility-libraries/structurelib-py/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/utility-libraries%2Fstructurelib-py/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270781393,"owners_count":24643820,"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","status":"online","status_checked_at":"2025-08-16T02:00:11.002Z","response_time":91,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["python3","structures"],"created_at":"2024-11-08T09:12:13.625Z","updated_at":"2025-08-16T23:06:19.623Z","avatar_url":"https://github.com/utility-libraries.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# structurelib-py\nsimilar function to the builtin struct-library but more friendly\n\n\u003c!-- TOC --\u003e\n* [structurelib-py](#structurelib-py)\n  * [Installation](#installation)\n  * [Examples](#examples)\n    * [Example 1: Basic](#example-1-basic)\n    * [Example 2: Nested Objects](#example-2-nested-objects)\n  * [Documentation](#documentation)\n    * [Supported types](#supported-types)\n    * [`dumps()`](#dumps)\n    * [`loads()`](#loads)\n    * [`Structure`-class](#structure-class)\n\u003c!-- TOC --\u003e\n\n## Installation\n\n[![PyPI - Version](https://img.shields.io/pypi/v/structurelib)\n](https://pypi.org/project/structurelib/)\n\n`pip install structurelib`\n\n## Examples\n\n### Example 1: Basic\n\n```python\nfrom dataclasses import dataclass\nimport structurelib as sl\nfrom structurelib.types import *\n\n@dataclass\nclass MyStruct(sl.Structure):  # sl.Structure inheritance is optional\n    name: str  # `string` is also possible\n    value: int8\n\nsl.dumps(MyStruct(\"Hello World\", 8))  # b'\\x0bHello World\\x08'\nMyStruct.dump_struct(MyStruct(\"Hello World\", 8))  # b'\\x0bHello World\\x08'\nsl.loads(b'\\x0bHello World\\x08', cls=MyStruct)  # MyStruct(name=\"Hello World\", value=8)\nMyStruct.load_struct(b'\\x0bHello World\\x08')  # MyStruct(name=\"Hello World\", value=8)\n```\n\n### Example 2: Nested Objects\n\n```python\nimport dataclasses\nimport structurelib as sl\nfrom structurelib.types import *\n\n\n@dataclasses.dataclass\nclass Credentials:\n    name: string\n    password: string\n\n\n@dataclasses.dataclass\nclass User:\n    name: string\n    credentials: Credentials\n\n\ninstance = User(\"hello\", Credentials(\"hello\", \"world\"))\nprint(instance)  # User(name=\"hello\", Credentials(name=\"hello\", password=\"world\"))\nsld = sl.dumps(instance)\nprint(f\"dump | {len(sld):\u003e3} | {sld}\")\nloaded = sl.loads(sld, cls=User)\nprint(loaded)  # User(name=\"hello\", Credentials(name=\"hello\", password=\"world\"))\n```\n\n## Documentation\n\n### Supported types\n\nYou can keep it basic and continue to use the builtin types and it should work\n\n```python\nclass MyStruct:\n    name: str\n    number: int\n```\n\nBut you can also add the additional types by importing them at the top of your file.\nThese types can reduce the output size.\n```python\nfrom structurelib.types import *\n```\nAll additional types can be used without problem to replace the old ones and won't interfere with typechecking.\n```python\nclass MyStruct:\n    name: string\n    number: uint16\n```\n\n| type              | description                | size    | values                                                 |\n|-------------------|----------------------------|---------|--------------------------------------------------------|\n| int/integer       | any integer                | dynamic | -∞ - +∞                                                |\n| integer[n, False] | integer                    | n byte  | 0 - 2\u003csup\u003e8*n\u003c/sup\u003e                                    |\n| integer[n, True]  | integer                    | n byte  | -2\u003csup\u003e8*(n-1)\u003c/sup\u003e - 2\u003csup\u003e8*(n-1)\u003c/sup\u003e             |\n| int8              | integer                    | 1 byte  | -128 - 127                                             |\n| int16             | integer                    | 2 bytes | -32,768 - 32,767                                       |\n| int32             | integer                    | 4 bytes | -2,147,483,648 - 2,147,483,647                         |\n| int64             | integer                    | 8 bytes | -9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 |\n| uint              | any positive integer       | dynamic | 0 - +∞                                                 |\n| uint8             | positive integer           | 1 byte  | 0 - 255                                                |\n| uint16            | positive integer           | 2 bytes | 0 - 65,535                                             |\n| uint32            | positive integer           | 4 bytes | 0 - 4,294,967,295                                      |\n| uint64            | positive integer           | 8 bytes | 0 - 18,446,744,073,709,551,615                         |\n| float/floating    | floating point number      | 8 bytes | -1.7e308 - 1.7e308                                     |\n| float32           | floating point number      | 4 bytes | -3.4e38 - 3.4e38                                       |\n| float64           | floating point number      | 8 bytes | -1.7e308 - 1.7e308                                     |\n| str/string        | text of any kind           | dynamic |                                                        |\n| string[n]         | text with encoded length n | n bytes |                                                        |\n| char              | single character           | 1 byte  | *0-255                                                 |\n| bytes/binary      | any binary data            | dynamic |                                                        |\n| binary[n]         | binary data with length n  | n nyte  |                                                        |\n| bool/boolean      | boolean value              | 1 byte  | True/False                                             |\n\nOther builtin types like `list`, `dict`, `set` and so on are currently not supported.\n\n### `dumps()`\n\n\u003e dump any object into bytes\n\n\u003e Note: only annotations of a class are dumped and later reconstructed\n\n```python\ndef dumps(obj: object) -\u003e bytes: ...\n```\n\n### `loads()`\n\n\u003e reconstructs object of `cls` from the dumped data\n\n```python\ndef loads(dump: bytes | BinaryIO, cls: Type[T]) -\u003e T: ...\n```\n\n### `Structure`-class\n\n\u003e This class is only for convenience and wraps the `dumps` and `loads` functions.\n\n```python\nclass Structure:\n    @classmethod\n    def load_struct(cls: Type[T], dump: bytes | BinaryIO) -\u003e T: ...\n    def dump_struct(self) -\u003e bytes: ...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Futility-libraries%2Fstructurelib-py","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Futility-libraries%2Fstructurelib-py","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Futility-libraries%2Fstructurelib-py/lists"}