Skip to main content

Wrapper to make it more convenient to make structure hooks for cattrs

Project description

A Python3.10+ 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

import typing as tp

import attrs

import strcs

reg = strcs.CreateRegister()
creator = reg.make_decorator()

Number = tp.NewType("Number", int)
Word = tp.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: tp.Annotated[Thing, strcs.FromMeta("thing")]
    words: list[Word]
    some_number: tp.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.4.1.tar.gz (36.1 kB view details)

Uploaded Source

Built Distribution

strcs-0.4.1-py3-none-any.whl (47.1 kB view details)

Uploaded Python 3

File details

Details for the file strcs-0.4.1.tar.gz.

File metadata

  • Download URL: strcs-0.4.1.tar.gz
  • Upload date:
  • Size: 36.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.20

File hashes

Hashes for strcs-0.4.1.tar.gz
Algorithm Hash digest
SHA256 4dc8c2ca8baeb68c824f2ba2e18f09292b625609e84739f4207944a704911bb8
MD5 ca3bc00691b128ede3b19a620f4d6862
BLAKE2b-256 20edcd9f2af50b0254fa11ce6a7ab927a4ee92308c3ef7f2e485f058a097fb26

See more details on using hashes here.

File details

Details for the file strcs-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: strcs-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 47.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.8.20

File hashes

Hashes for strcs-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5b6ea50bd4b39d92cf7898f24ceb8c31fe5e69a6e496fee255bf39f2b62b8a0e
MD5 c8d65b729c39e302517adaff47540d4a
BLAKE2b-256 fc175f6bf10248234d9c1b0accad20c7bc64d0e866a300ca53e72ebc5e7d415e

See more details on using hashes here.

Supported by

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