Wrapper to make it more convenient to make structure hooks for cattrs
Project description
A Python3.13+ library that wraps cattrs for a modular approach to constructing objects with the ability to string data through the process.
Install from pypi:
> python -m pip install strcs
Documentation at https://strcs.readthedocs.io/
Example
from typing import Annotated, NewType
import attrs
import strcs
reg = strcs.CreateRegister()
creator = reg.make_decorator()
Number = NewType("Number", int)
Word = NewType("Word", str)
@attrs.define(frozen=True)
class Maths(strcs.MetaAnnotation):
multiply: int
def calculate(self, val: int) -> Number:
return Number(val * self.multiply)
class Thing:
pass
@attrs.define
class Config:
thing: Annotated[Thing, strcs.FromMeta("thing")]
words: list[Word]
some_number: Annotated[Number, Maths(multiply=2)]
contrived1: str
contrived2: str
some_other_number: int = 16
@creator(Number)
def create_number(val: object, /, annotation: Maths) -> strcs.ConvertResponse[Number]:
if not isinstance(val, int):
return None
return annotation.calculate(val)
@creator(Word)
def create_word(val: object, /, word_prefix: str = "") -> strcs.ConvertResponse[Word]:
if not isinstance(val, str):
return None
return Word(f"{word_prefix}{val}")
@creator(Config)
def create_config(val: object, /) -> strcs.ConvertResponse[Config]:
if not isinstance(val, dict):
return None
result = dict(val)
if "contrived" in result:
contrived = result.pop("contrived")
result["contrived1"], result["contrived2"] = contrived.split("_")
return result
thing = Thing()
meta = strcs.Meta({"thing": thing, "word_prefix": "the_prefix__"})
config = reg.create(
Config,
{"words": ["one", "two"], "some_number": 20, "contrived": "stephen_bob"},
meta=meta,
)
print(config)
assert isinstance(config, Config)
assert config.thing is thing
assert config.words == ["the_prefix__one", "the_prefix__two"]
assert config.some_number == 40
assert config.some_other_number == 16
assert config.contrived1 == "stephen"
assert config.contrived2 == "bob"
Development
To have a virtualenv that has everything needed in it:
> source run.sh activate
To run tests, linting, formatting, type checking:
> ./test.sh > ./lint > ./format > ./types
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file strcs-0.5.0.tar.gz.
File metadata
- Download URL: strcs-0.5.0.tar.gz
- Upload date:
- Size: 19.1 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ac7b4912ac00c0ba261ab59a24b83ef5a969a0d364fa03001223d225c0c9b06
|
|
| MD5 |
edaf6eba0cc104495220974d68ce651e
|
|
| BLAKE2b-256 |
4eb4541255a2605fc964ff0b3727d09b5ecd20992ac1fc0a2418b9af5da0342e
|
Provenance
The following attestation bundles were made for strcs-0.5.0.tar.gz:
Publisher:
release.yml on delfick/strcs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strcs-0.5.0.tar.gz -
Subject digest:
3ac7b4912ac00c0ba261ab59a24b83ef5a969a0d364fa03001223d225c0c9b06 - Sigstore transparency entry: 210160109
- Sigstore integration time:
-
Permalink:
delfick/strcs@bb217c6f48a73bcc00263827415410a636455c02 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/delfick
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@bb217c6f48a73bcc00263827415410a636455c02 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file strcs-0.5.0-py3-none-any.whl.
File metadata
- Download URL: strcs-0.5.0-py3-none-any.whl
- Upload date:
- Size: 47.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20cd8ed18d8310b20c99c7110123ba5ca73989c79b7ea1051ef4a749dab6008d
|
|
| MD5 |
12295c5de3169d534fa4b5a1ade5071f
|
|
| BLAKE2b-256 |
1d7ff6f5337a0ac07bb2c9e5dad365b68d50ec250e45bf3e21e7d11417510693
|
Provenance
The following attestation bundles were made for strcs-0.5.0-py3-none-any.whl:
Publisher:
release.yml on delfick/strcs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
strcs-0.5.0-py3-none-any.whl -
Subject digest:
20cd8ed18d8310b20c99c7110123ba5ca73989c79b7ea1051ef4a749dab6008d - Sigstore transparency entry: 210160110
- Sigstore integration time:
-
Permalink:
delfick/strcs@bb217c6f48a73bcc00263827415410a636455c02 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/delfick
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@bb217c6f48a73bcc00263827415410a636455c02 -
Trigger Event:
workflow_dispatch
-
Statement type: