Skip to main content

Object-based, bidirectional string templating with declarative result types

Project description

regma

Python Tests codecov Ruff Checked with mypy

String templates with dataclass style

regma lets you define string templates through models behaving like standard dataclass.

It aims at solving a common scenario in the VFX/animation industry where we need to construct file paths from tokens and extract tokens from file paths.

regma lets you define the structure of your strings from typed fields and will handle for you:

  • formatting of your model instances to string
  • parsing of a string to model instances

Features

  • Dataclass like structure: your models are properly typed and subclassable
  • Typed fields: string, integer, choice
  • Reference to other models: using annotations or using the reference field
  • Field validation: fields are validated both during formatting and parsing. Strictness can be configured in both directions independently
  • Configurable delimiters: {}, <>, []; global or per-model
  • Great developer experience: common linter and static type checker are supported. Most common IDEs (VS Code, PyCharm) provide autocompletion

Concrete example

from regma import TemplateModel
from regma import string, choice, integer


class Asset(TemplateModel):
    type: str = choice(["chr", "prp", "set"])
    code: str = string(r"[a-z][a-z0-9]+")
    template = "{type}_{code}"


# Create an asset from its fields
asset = Asset("chr", "foo")

# Get it as a string
asset_str = asset.format()  # or using str(asset)

# Parse an asset from its string
parsed_asset = Asset.parse("chr_foo")


# Build a more complex example to build a path
class GroomRetargetInfoPath(TemplateModel):
    source_asset: Asset
    target_asset: Asset
    groom_name: str = string(r"[a-z][a-z0-9]+")

    template = (
        "/root/assets/{target_asset.type}/{target_asset}/{asset_target}_{groom}_{asset_target}.json"
    )


# Create a new model
groom_info = GroomRetargetInfoPath(Asset("chr", "foo"), Asset("chr", "bar"), "hair")

# Get it as string
groom_info_path = groom_info.format()  # /root/assets/chr/chr_foo/chr_foo_hair_chr_bar.json

# Parse the path to its information
parsed_groom_info = GroomRetargetInfoPath.parse("/root/assets/chr/chr_foo/chr_foo_hair_chr_bar.json")

License

MIT

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

regma-0.0.1.tar.gz (81.8 kB view details)

Uploaded Source

Built Distribution

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

regma-0.0.1-py3-none-any.whl (12.8 kB view details)

Uploaded Python 3

File details

Details for the file regma-0.0.1.tar.gz.

File metadata

  • Download URL: regma-0.0.1.tar.gz
  • Upload date:
  • Size: 81.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for regma-0.0.1.tar.gz
Algorithm Hash digest
SHA256 45d0f30789b51c72c1f69662f5b3fc1d0935c040685bfe5dfecc1f6a835b48ba
MD5 7c9ad1b41f7af9ebf8648d6e4c982994
BLAKE2b-256 1e785875a031ae7026ccf4cb5333983cc4a996af080109d8d280a70e48d5ccad

See more details on using hashes here.

Provenance

The following attestation bundles were made for regma-0.0.1.tar.gz:

Publisher: publish.yml on PoloB/regma

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

File details

Details for the file regma-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: regma-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 12.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for regma-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a22e4abd07639aa8bbd03dfa9bfa1cd8bfa57d26f00419ae3bc13c97e2ee0fae
MD5 30f90c83a529ac43dcd19e09588d5f4a
BLAKE2b-256 768cb57efc56b371ec902c07968c503a145f86a09c854338d49477a06d2740e6

See more details on using hashes here.

Provenance

The following attestation bundles were made for regma-0.0.1-py3-none-any.whl:

Publisher: publish.yml on PoloB/regma

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