struction
Pythonic, yet C-Style structs/unions.
Structs are similar to namedtuples, but they allow type assertion and are defined the same way as any class. Creating a struct is easy.
$ pip install struction
from struction import Struct, default, between, clamp
class MyStruct(Struct):
# any types specified *must* be a class/type
field_0 = int
field_1 = str
# you can also allow multiple types
multi_type = int, str
# and default values!
with_default = int, default(10)
with_multi_and_default = int, str, default(10)
# it's also possible to set a specific range for fields
with_range = int, float, between(1, 10) # setting this to a value < 1 or > 10 will raise ValueError
# or, values can be clamped to a range
with_clamp = int, float, clamp(-5, 5) # setting this to any value outside of range will clamp it
Once a struct is created, it’s fields can be changed, but they must match the given type or a TypeError will be raised. Using del on a field resets it to its default value.
It’s also possible to nest structs. Any structs that are nested will automatically be initialized.
class NestMe(Struct):
field_0 = int
field_1 = int
class Nester(Struct):
abc = str
nest = OnlyInt # nested struct
>>> print(Nester())
# Nester {
# abc = None
# nest = OnlyInt {
# field_0 = None
# field_1 = None
# }
# }
>>> print(Nester(nest=None))
# Nester {
# abc = None
# nest = None
# }
If you don’t want strict types, you can also use a TypecastingStruct. This will attempt to typecast the given value to the field’s type. If it can’t be typecasted, it will then raise a TypeError.
from struction import TypecastingStruct
class Test(TypecastingStruct):
i = int
f = float
s = str
all = float, int, str
>>> test = Test()
>>> test.i = 5.3
>>> test.f = 100
>>> test.s = {"a": 1, "b":2}
>>> print(test)
# Struct Test {
# all = None
# f = 100.0
# i = 5
# s = "{'a': 1, 'b': 2}"
# }
>>> # If multiple types are allowed for a field, the value will be
>>> # casted to the first type that doesn't throw an Exception
>>> test.all = ("a", 1, "b", 2, "c", 3)
>>> test.all
# '("a", 1, "b", 2, "c", 3)'
Note: Typecasting only works at runtime. The values still need to match their types at class definition.
Reference
These can be applied to any Struct class.
Struct.dict() : dict with struct’s fields. {name: value, …}
Struct.fields() : list of fields struct has.
str(Struct) : Multi-line representation of struct.
repr(Struct) : Single line representation of struct.
Metadata
Release files for Struction 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 |
|---|---|---|---|---|
| Struction-0.3.0.tar.gz | 5.4 kB | Without provenance | Details for Struction-0.3.0.tar.gz |
Release files / Struction-0.3.0.tar.gz
| Download URL | Struction-0.3.0.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b6390a0cc1dc09b3fb8cd08bc373bfc83fcb13e395301d56b37c8f0202df21b9
|
|
BLAKE2b-256 checksum How to use checksums |
8075044b440ca9454bb191a1cd1f800cf24fe01f0d586419b5390698d08acd6b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for Struction 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.