# protonbites <kbd>🧪 EXPR1</kbd>
Most sane way to store JSON data. Simple, light, strongly-typed and secure. (Probably, overall)
**Step 1.** Create a dataclass.
```python
from dataclasses import dataclass
@dataclass
class Person:
name: str
age: int
```
> For floats and ints, you can use `typing.Annotated[int, '<dtype>']` where `<dtype>` is the desired datatype.
> Below are the avilable dtypes:
>
> **ints**
> - int8 / uint8
> - int16 / uint16
> - int32 / uint32
> - int64 / uint64
>
> **floats**
> - float32
> - float64
**Step 2.** Create a schema from the dataclass.
```python
from protonbites import get_schema
schema = get_schema(Person)
```
**Step 3.** Use the schema to encode/decode data.
```python
# Init a new dataclass
person = Person(name="Jesse Pinkman", age=28)
encoded = schema.encode(person)
decoded = schema.decode(encoded)
assert isinstance(decoded, Person)
```
## API Documentation <kbd>incomplete</kbd>
Oopsy daisy.
### <kbd>def</kbd> encode
```python
def encode(
obj: PythonBackendDataTypes,
/,
*,
force_keep_str: bool = False
) -> bytes
```
Encode data to a proton.
**Args**:
- obj (`PythonBackendDataTypes`): Object.
- force_keep_str (`bool`): Force keep the string? Usually, when the expected text length is more than 102 characters, we use `gzip` to compress the text data. If you wish to keep the string, set this to `True`.
**Simple Example:**
```python
encode({
"name": "Mr. Penguin",
"tags": ["depressed"],
"friends": [
{
"name": "Fernando Miguel",
"tags": ["dancing", "noot"]
}
]
})
# => b"\x01\x0f'Mr. Penguin'\x11\x03\x0f'depressed'\x11\x04\x11\x03\x01\x0f'Fernando Miguel'\x11\x03\x0f'dancing'\x11\x0f'noot'\x11\x04\x11\x02\x11\x04\x11\x02"
encode({ "text": "what the fish " * 9_999 })
# => b'\x01\x0f1f8b0800a6ed516602ffe…bf2c1f2a24c7aad4220200\x11\x02'
```
**Example using custom ints and floats:**
```python
from protonbites import uint8, float32
encode({
"a": uint8(10),
"b": float32(10.98535)
})
```
### <kbd>def</kbd> decode
```python
def decode(__c: bytes, /) -> PythonBackendTypes
```
Decode the data.
**Args:**
- \_\_c: The encoded data.
**Example:**
```python
a = decode(b"\x01…\x02")
# => [ …, …, … ]
reveal_type(a) # PythonBackendDataTypes (type_checking)
# To ensure the decoded data is the entrypoint
b = decoded_safely(a)
reveal_type(b) # list (type_checking)
```
### <kbd>def</kbd> get_schema
```rust
get_schema(__dc: type[T], /) -> Schema[T]
where T: DataclassProtocol
```
**Args:**
- \_\_dc: The dataclass.
```python
@dataclass
class Person:
name: str
age: int
schema = get_schema()
schema.encode(Person(name="Jesse Pinkman", age=28))
```
<details>
<summary>Were you looking for Mr. Penguin?</summary>
<p>
<img src="https://github.com/AWeirdDev/protonbites/assets/90096971/26303b62-3ffe-4665-ab2b-36f331ec2f04" alt="What you're looking for is here" align="left" />
<p>I'm standing in a void. No light. No sound. And as I stand there... In front of me, a penguin manifests. He merely stands. Observing. But I. I am filled with dread. I dare think it, but not say it. Are you the embodiment of my end? His gaze, so vacant, pierces my very soul. Then, from the all-encompassing abyss itself, the noots of a hundred penguins billow out. The noots coalesce, forming bodies. But from those bodies, arise not life, but... flames. Their joyful noots mutate into agonized screams. Suddenly, they're engulfed by the void. Yet, the most haunting realization? In their fleeting, fiery visages, I glimpse my own reflection.</p><br /><br />
</p>
</details>
***
(c) 2024 AWeirdDev
Raw data
{
"_id": null,
"home_page": null,
"name": "protonbites",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "protonbites, proton, bytes, encode, decode, json, protobuf, encoder, decoder, schema",
"author": null,
"author_email": "AWeirdDev <aweirdscratcher@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/9a/08/88143ce85c6b51d2376acd8f7d90f14b0080e53879102b8e5f8cec0843e0/protonbites-0.2.tar.gz",
"platform": null,
"description": "# protonbites <kbd>\ud83e\uddea EXPR1</kbd>\r\nMost sane way to store JSON data. Simple, light, strongly-typed and secure. (Probably, overall)\r\n\r\n**Step 1.** Create a dataclass.\r\n\r\n```python\r\nfrom dataclasses import dataclass\r\n\r\n@dataclass\r\nclass Person:\r\n name: str\r\n age: int\r\n```\r\n\r\n> For floats and ints, you can use `typing.Annotated[int, '<dtype>']` where `<dtype>` is the desired datatype.\r\n> Below are the avilable dtypes:\r\n> \r\n> **ints**\r\n> - int8 / uint8\r\n> - int16 / uint16\r\n> - int32 / uint32\r\n> - int64 / uint64\r\n> \r\n> **floats**\r\n> - float32\r\n> - float64\r\n\r\n\r\n**Step 2.** Create a schema from the dataclass.\r\n\r\n```python\r\nfrom protonbites import get_schema\r\n\r\nschema = get_schema(Person)\r\n```\r\n\r\n**Step 3.** Use the schema to encode/decode data.\r\n\r\n```python\r\n# Init a new dataclass\r\nperson = Person(name=\"Jesse Pinkman\", age=28)\r\n\r\nencoded = schema.encode(person)\r\ndecoded = schema.decode(encoded)\r\n\r\nassert isinstance(decoded, Person)\r\n```\r\n\r\n## API Documentation <kbd>incomplete</kbd>\r\n\r\nOopsy daisy.\r\n\r\n### <kbd>def</kbd> encode\r\n\r\n```python\r\ndef encode(\r\n obj: PythonBackendDataTypes, \r\n /, \r\n *,\r\n force_keep_str: bool = False\r\n) -> bytes\r\n```\r\n\r\nEncode data to a proton.\r\n\r\n**Args**:\r\n- obj (`PythonBackendDataTypes`): Object.\r\n- force_keep_str (`bool`): Force keep the string? Usually, when the expected text length is more than 102 characters, we use `gzip` to compress the text data. If you wish to keep the string, set this to `True`.\r\n\r\n**Simple Example:**\r\n```python\r\nencode({\r\n \"name\": \"Mr. Penguin\",\r\n \"tags\": [\"depressed\"],\r\n \"friends\": [\r\n {\r\n \"name\": \"Fernando Miguel\",\r\n \"tags\": [\"dancing\", \"noot\"]\r\n }\r\n ]\r\n})\r\n# => b\"\\x01\\x0f'Mr. Penguin'\\x11\\x03\\x0f'depressed'\\x11\\x04\\x11\\x03\\x01\\x0f'Fernando Miguel'\\x11\\x03\\x0f'dancing'\\x11\\x0f'noot'\\x11\\x04\\x11\\x02\\x11\\x04\\x11\\x02\"\r\n\r\nencode({ \"text\": \"what the fish \" * 9_999 })\r\n# => b'\\x01\\x0f1f8b0800a6ed516602ffe\u2026bf2c1f2a24c7aad4220200\\x11\\x02'\r\n```\r\n\r\n**Example using custom ints and floats:**\r\n```python\r\nfrom protonbites import uint8, float32\r\n\r\nencode({\r\n \"a\": uint8(10),\r\n \"b\": float32(10.98535)\r\n})\r\n```\r\n\r\n### <kbd>def</kbd> decode\r\n\r\n```python\r\ndef decode(__c: bytes, /) -> PythonBackendTypes\r\n```\r\n\r\nDecode the data.\r\n\r\n**Args:**\r\n- \\_\\_c: The encoded data.\r\n\r\n**Example:**\r\n```python\r\na = decode(b\"\\x01\u2026\\x02\")\r\n# => [ \u2026, \u2026, \u2026 ]\r\n\r\nreveal_type(a) # PythonBackendDataTypes (type_checking)\r\n\r\n# To ensure the decoded data is the entrypoint\r\nb = decoded_safely(a)\r\nreveal_type(b) # list (type_checking)\r\n```\r\n\r\n### <kbd>def</kbd> get_schema\r\n\r\n```rust\r\nget_schema(__dc: type[T], /) -> Schema[T]\r\nwhere T: DataclassProtocol\r\n```\r\n\r\n**Args:**\r\n- \\_\\_dc: The dataclass.\r\n\r\n```python\r\n@dataclass\r\nclass Person:\r\n name: str\r\n age: int\r\n\r\nschema = get_schema()\r\nschema.encode(Person(name=\"Jesse Pinkman\", age=28))\r\n```\r\n\r\n<details>\r\n<summary>Were you looking for Mr. Penguin?</summary>\r\n<p>\r\n\r\n<img src=\"https://github.com/AWeirdDev/protonbites/assets/90096971/26303b62-3ffe-4665-ab2b-36f331ec2f04\" alt=\"What you're looking for is here\" align=\"left\" />\r\n<p>I'm standing in a void. No light. No sound. And as I stand there... In front of me, a penguin manifests. He merely stands. Observing. But I. I am filled with dread. I dare think it, but not say it. Are you the embodiment of my end? His gaze, so vacant, pierces my very soul. Then, from the all-encompassing abyss itself, the noots of a hundred penguins billow out. The noots coalesce, forming bodies. But from those bodies, arise not life, but... flames. Their joyful noots mutate into agonized screams. Suddenly, they're engulfed by the void. Yet, the most haunting realization? In their fleeting, fiery visages, I glimpse my own reflection.</p><br /><br />\r\n</p>\r\n</details>\r\n\r\n***\r\n\r\n(c) 2024 AWeirdDev\r\n",
"bugtrack_url": null,
"license": null,
"summary": "Most sane way to store JSON data. Simple, light, strongly-typed and secure. (Probably, overall)",
"version": "0.2",
"project_urls": {
"Bug Tracker": "https://github.com/AWeirdDev/protonbites/issues",
"Source": "https://github.com/AWeirdDev/protonbites"
},
"split_keywords": [
"protonbites",
" proton",
" bytes",
" encode",
" decode",
" json",
" protobuf",
" encoder",
" decoder",
" schema"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9a0888143ce85c6b51d2376acd8f7d90f14b0080e53879102b8e5f8cec0843e0",
"md5": "1f12f79788cf50a8123083479507c0d8",
"sha256": "a2a66810c0e1c03fd2bc50f2fa05e1e48fe561a3da15ffb10cb0eb0cdd0c00e1"
},
"downloads": -1,
"filename": "protonbites-0.2.tar.gz",
"has_sig": false,
"md5_digest": "1f12f79788cf50a8123083479507c0d8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 6802,
"upload_time": "2024-05-25T14:02:24",
"upload_time_iso_8601": "2024-05-25T14:02:24.261000Z",
"url": "https://files.pythonhosted.org/packages/9a/08/88143ce85c6b51d2376acd8f7d90f14b0080e53879102b8e5f8cec0843e0/protonbites-0.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-05-25 14:02:24",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "AWeirdDev",
"github_project": "protonbites",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "protonbites"
}