Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

strcs-0.5.0.tar.gz (19.1 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

strcs-0.5.0-py3-none-any.whl (47.6 kB view details)

Uploaded Python 3

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

Hashes for strcs-0.5.0.tar.gz
Algorithm Hash digest
SHA256 3ac7b4912ac00c0ba261ab59a24b83ef5a969a0d364fa03001223d225c0c9b06
MD5 edaf6eba0cc104495220974d68ce651e
BLAKE2b-256 4eb4541255a2605fc964ff0b3727d09b5ecd20992ac1fc0a2418b9af5da0342e

See more details on using hashes here.

Provenance

The following attestation bundles were made for strcs-0.5.0.tar.gz:

Publisher: release.yml on delfick/strcs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for strcs-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 20cd8ed18d8310b20c99c7110123ba5ca73989c79b7ea1051ef4a749dab6008d
MD5 12295c5de3169d534fa4b5a1ade5071f
BLAKE2b-256 1d7ff6f5337a0ac07bb2c9e5dad365b68d50ec250e45bf3e21e7d11417510693

See more details on using hashes here.

Provenance

The following attestation bundles were made for strcs-0.5.0-py3-none-any.whl:

Publisher: release.yml on delfick/strcs

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page