Skip to main content

Interpolate %[meta-text] with %[sigils].

Project description

“An inscribed or painted symbol considered to have magical power.”

Sigils is a Python library for text and meta-text interpolation. It provides advanced capabilities such as context-based interpolation, function execution, nested and recursive interpolation, case-insensitive matching, and global context support.

Any Python object can be provided as context, including nested dictionaries, lists, and functions. Sigils can be used directly from Python or from the command line. Sigils is thread-safe and has no dependencies outside of the Python standard library.

Installation

Install Sigils using pip:

pip install sigils

Usage

Here’s a simple example of using Sigils:

from sigils import Sigil

context = {"user": {"name": "Alice"}}
s = Sigil("Hello, %[user.name]!")
print(s % context)  # Outputs: Hello, Alice!

Sigils can handle function execution and nested interpolation:

from sigils import Sigil

context = {
    "user": {
        "name": "Alice",
        "greet": lambda name: f"Hello, {name}!"
    }
}

s = Sigil("%[user.greet:user.name]")
print(s % context)  # Outputs: Hello, Alice!

s = Sigil("%[user.greet:%%user.name]")
print(s % context)  # Outputs: Hello, %user.name! %user.name is treated as a literal value

Sigils support case-insensitive matching and global context fallback:

from sigils import Sigil, Sigil.Context

global_context = {"greeting": "Hello, world!"}
with Sigil.Context(global_context):
    s = Sigil("%[GREETING]")
    print(s % {})  # Outputs: Hello, world!

Command-Line Usage

Sigils can be used directly from the command line. Here’s an example:

sigils "Hello, %[user.name]!" -c context.json

In this example, “context.json” is a JSON file with a structure like {“user”: {“name”: “Alice”}}. The command will output: “Hello, Alice!”.

Considerations

  • Function Execution: If the value of a Sigil is a callable function, it will be executed and its return value used in the string. Ensure all function values in your context are safe to execute.

  • Recursion Depth: Sigils handles up to 6 levels of nested interpolation by default. Adjust this limit by passing a different max_depth value to the interpolate method.

  • Thread Safety: The global context in Sigils is thread-safe. However, if you’re using mutable objects in your context and modifying them from multiple threads, manage thread safety at the application level.

  • Case-Insensitive Matching: If a key fails to resolve, a case-insensitive lookup is attempted. This only works if the keys in your context are all unique when lowercased.

Performance

Sigils is designed with performance in mind. In typical use cases, Sigils performs competitively with Python’s built-in string formatting.

License

Sigils is licensed under the MIT License. See the LICENSE file for details.

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

sigils-0.3.5.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

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

sigils-0.3.5-py3-none-any.whl (14.6 kB view details)

Uploaded Python 3

File details

Details for the file sigils-0.3.5.tar.gz.

File metadata

  • Download URL: sigils-0.3.5.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for sigils-0.3.5.tar.gz
Algorithm Hash digest
SHA256 1ecb99483149fd81bed8e7c218bac52bc8aa76333d3a478905e654a5ef2fb90b
MD5 cdd7fd5c3e0fd1037263bd93380356ca
BLAKE2b-256 40ee37724903c14d75eff4ea8c8290070abf4a896be06c23190a884efbcfed2d

See more details on using hashes here.

File details

Details for the file sigils-0.3.5-py3-none-any.whl.

File metadata

  • Download URL: sigils-0.3.5-py3-none-any.whl
  • Upload date:
  • Size: 14.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.0

File hashes

Hashes for sigils-0.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 762655b64e15e1563bd69d111a95b484ce7cfb632142ffef9a0176cc25ff7c48
MD5 46ae89ec15b6730da216f6d5ff976ef8
BLAKE2b-256 fcbd1534b19f85672244eaaaf77fd05fc5046835421c36d1354febd4fb278ef6

See more details on using hashes here.

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