Python Zig Zon Parser
A native python extension built with zig to parse zig-object-notation strings into python objects.
On PyPi it is only distributed in source, and built during install.
It therefore needs to download the ziglang python package.
Depending on your OS, you may need the python development headers
(e.g. python3-dev) installed.
Usage
install from source on https://pypi.org/project/zig-zon:
pip install zig-zon
Parsing
>>> import zig_zon
>>> parsed = zig_zon.parse('.{.allyourcode = .are_belong_to_us, .asd = 123}')
>>> print(parsed)
{'allyourcode': 'are_belong_to_us', 'asd': 123}
Parsing Options
bytes: Controls how binary / non-UTF-8 string literals in ZON are handled:"error"(default): Non-UTF-8 string literals raise aUnicodeDecodeError."auto": Valid UTF-8 strings are returned aszig_zon.String(subclassesstr); strings containing invalid UTF-8 bytes are returned aszig_zon.ByteString(subclassesbytes)."always": Always returnszig_zon.ByteStringfor all string literals.
or, look at the test file
Stringification / Serialization
Use zig_zon.stringify to serialize Python objects to ZON strings, or zig_zon.dump to write directly to a file-like object:
>>> import zig_zon
>>> data = {
... "name": zig_zon.Enum("my_pkg"),
... "version": "0.1.0",
... "fingerprint": zig_zon.Hex(0x1234abcd),
... "dependencies": {
... "dep1": {
... "url": "https://example.com/dep1.tar.gz",
... "hash": "1220...",
... }
... },
... "paths": ["build.zig", "build.zig.zon", "src"],
... }
>>> print(zig_zon.stringify(data))
.{
.name = .my_pkg,
.version = "0.1.0",
.fingerprint = 0x1234abcd,
.dependencies = .{
.dep1 = .{
.url = "https://example.com/dep1.tar.gz",
.hash = "1220...",
},
},
.paths = .{
"build.zig",
"build.zig.zon",
"src",
},
}
Write to a file or stream:
with open("build.zig.zon", "w", encoding="utf-8") as f:
zig_zon.dump(data, f)
Serialization Options
integer_mode: Controls integer formatting:"auto"(default): UsesHex-> hex (0x...),Integer-> decimal, plainint-> decimal."int": Always formats integers as decimal."hex": Always formats integers as hex (0x...)."fallback_int": UsesHex-> hex,Integer-> decimal, plainint-> decimal."fallback_hex": UsesInteger-> decimal,Hex-> hex, plainint-> hex."error": Errors out (TypeError) if an integer is not azig_zon.Integerorzig_zon.Hexsubclass.
dict_as_tuples(bool, defaultFalse): IfTrue, converts dictionaries to tuples of key-value tuples (.{ .{ "key", value }, ... }) rather than struct notation (.{ .key = value }).encoding(str, default"utf-8"): Specifies the encoding used for raw strings (str) and encoded strings (bytes/bytearray).whitespace(bool, defaultTrue): IfFalse, omits unnecessary whitespace and newlines for compact output.
Supported Python Types for Serialization
list,tuple,zig_zon.Tuple: Serialized as ZON tuples.{ ... }.set,frozenset: Serialized as ZON tuples.{ ... }.namedtuple: Serialized as ZON structs.{ .field = value }.dict,zig_zon.Struct: Serialized as structs or key-value tuples (withdict_as_tuples=True).str,zig_zon.String: Serialized as string literals"...".bytes,bytearray: Decoded usingencodingand serialized as string literals"...".zig_zon.ByteString: Serialized directly as binary string literals"..."preserving raw byte escapes.zig_zon.BareEnum: Serialized as bare enums.identifier.zig_zon.QuotedEnum: Serialized as quoted enums.@"identifier".zig_zon.Enum: Serialized as enum literals (bare or quoted as necessary).int,zig_zon.Integer,zig_zon.Hex: Formatted according tointeger_mode.float,zig_zon.Float: Serialized as float literals.bool: Serialized astrueorfalse.None: Serialized asnull.
Supported Zon values and their python type
The parser returns custom classes for each Zig type that wrap and subclass standard Python types:
structs->zig_zon.Struct(subclassesdict)tuples->zig_zon.Tuple(subclasseslist)integer literals->zig_zon.Integer(subclassesint)hex literals->zig_zon.Hex(wraps / subclassesint)float literals->zig_zon.Float(subclassesfloat)booleans->bool(Python built-inboolis final and cannot be subclassed)null->None(PythonNoneTypeis final and cannot be subclassed)string literals->zig_zon.String(subclassesstr)binary string literals->zig_zon.ByteString(subclassesbytes)bare enum literals->zig_zon.BareEnum(subclasseszig_zon.Enum->str)quoted enum literals->zig_zon.QuotedEnum(subclasseszig_zon.Enum->str)
Because the custom classes wrap and subclass their respective Python base types, both isinstance(val, BaseType) and isinstance(val, zig_zon.CustomClass) evaluate to True.
Not supported values
bigint
How it works
The zig build commands produces a python source distribution archive ready
for uploading to Pypi. It includes the sources of zig-python-sdist
and a custom setup.py that handles the build of the cpython extension.
When you run pip install, the source distribution is downloaded, and since
this source distribution depends on the ziglang python package at build time,
a zig build command is used to compile the extension.
Developing
as opposed to other solutions for building cpython extensions in zig, i want to develop this using:
zig build
for linking against the local python installation zig-python-sdist
runs a python script that imports the cpython headers into zig's build cache,
run translate-c on the result and offers a cpython module to import
in our zig code.
On windows, the extension library is also linked against
the python runtime static lib and renamed to a .pyd file for importing.
Metadata
Release files for zig-zon 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| zig_zon-0.3.0.tar.gz | 34.5 kB | Without provenance | Details for zig_zon-0.3.0.tar.gz |
Release files / zig_zon-0.3.0.tar.gz
| Download URL | zig_zon-0.3.0.tar.gz |
|---|---|
| Size | 34.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
61f8f5c91ea36dafbb5babad7a819b7d06419030e4d1c7a165f9b018a1edb3ca
|
|
BLAKE2b-256 checksum How to use checksums |
f1e94707d72c29eb4d7805435be0d2447bbffc27d4987fe3b179d2a650f7ab38
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.10.21
|
Security for zig-zon 0.3.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.