Skip to main content

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

Metadata

Release files for strcs 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for strcs 0.5.0
File Size Uploaded
strcs-0.5.0.tar.gz 19.1 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for strcs 0.5.0
File Interpreter ABI Platform
strcs-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 19.1 MB

Release files / strcs-0.5.0.tar.gz

Download URL strcs-0.5.0.tar.gz
Size 19.1 MB
Tags Source
SHA-256 checksum
How to use checksums
3ac7b4912ac00c0ba261ab59a24b83ef5a969a0d364fa03001223d225c0c9b06
BLAKE2b-256 checksum
How to use checksums
4eb4541255a2605fc964ff0b3727d09b5ecd20992ac1fc0a2418b9af5da0342e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 11, 2025.

Transparency log

Release files / strcs-0.5.0-py3-none-any.whl

Download URL strcs-0.5.0-py3-none-any.whl
Size 47.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
20cd8ed18d8310b20c99c7110123ba5ca73989c79b7ea1051ef4a749dab6008d
BLAKE2b-256 checksum
How to use checksums
1d7ff6f5337a0ac07bb2c9e5dad365b68d50ec250e45bf3e21e7d11417510693
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 11, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page