Skip to main content

spintax-core (Python)

PyPI CI license

A framework-agnostic Spintax engine for Python — parse, render, validate, extract, analyze, and neutralize spintax templates. MIT, zero runtime dependencies, Python 3.10+.

This is the third engine in the Spintax family, and an independent implementation — not a transcription of the others. It is held to the same behavior contract by a shared golden corpus of language-neutral fixtures, which already gates the TypeScript engine and the PHP one. All 168 of them pass here, none skipped, none expected to fail.

Install

pip install spintax-core

Use

from spintax_core import render, validate, parse

render("{Hello|Hi} there!")                        # "Hello there!" or "Hi there!"
render("{Hello|Hi} there!", seed=42)               # same seed, same output, every time
render("Hi %name%!", context={"name": "Sam"})      # "Hi Sam!"

# Reuse a template: parse once, render many.
ast = parse('[<sep=", ">fast|cheap|good]')
[render(ast) for _ in range(3)]
# ['Cheap, fast, good', 'Good, cheap, fast', 'Cheap, good, fast']

# Check a template before you ship it.
[d.code for d in validate("{a|b")]                 # -> ['bracket.unclosed']

Rendering is lenient: malformed markup degrades rather than raising, so a template a non-programmer wrote cannot take a page down. Output is also tidied up by default — sentence capitalization, spacing around punctuation, URLs and abbreviations left intact — which is why […fast|cheap|good] above comes back capitalized. Pass post_process=False to turn that off and get the raw pick.

Syntax — enumerations {a|b}, permutations [<sep=", ">a|b], variables %name%, conditionals {?VAR?yes|no}, plural agreement {plural 3: one|few|many}, comments /# … #/, and the #set / #def / #include directives — is documented in full at spintax.net/docs.

Why

The existing PyPI spintax package is GPLv3 and has not shipped since 2018. GPL blocks commercial adoption; this one is MIT and maintained.

License

MIT. The WordPress plugin remains GPL; MIT/Expat is GPL-compatible.


Part of the 301.st toolset. Product home: spintax.net.

Development

The test suite is the shared golden corpus — the same JSON fixtures the TypeScript and PHP engines are tested against, read from a checkout rather than vendored here. A copy would drift, and a drifting contract is not a contract.

git clone https://github.com/investblog/spintax-js ../spintax-js   # once
python -m venv .venv && .venv/bin/pip install -e . pytest
SPINTAX_FIXTURES=../spintax-js/packages/conformance/fixtures pytest

Without the fixtures the suite fails rather than passing an empty run — a green suite that tested nothing is the most expensive kind of green.

The engine is complete, so every corpus case is a real pass: 168 corpus fixtures pass, 0 xfailed, 0 skipped, alongside the port's own local tests. A skip must never appear — it would mean a case is being neither asserted nor counted. During the build (P0–P3) xfailed was the milestone tracker, one number counting down what the corpus expected and the engine could not yet do; it reached zero when the renderer and analyze landed.

If you mutate a source file to check that a test catches it, delete __pycache__ first. Python invalidates bytecode on (mtime, size). A mutation that preserves both — swapping .*? for .+?, say — leaves the stale .pyc in place, so the next run still imports the mutated module after you have restored the file. That produced a failure pointing at correct code, and the obvious response to it would have been to break the code for real.

Metadata

Release files for spintax-core 0.3.3

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

Source distribution (sdist)

Source distribution for spintax-core 0.3.3
File Size Uploaded
spintax_core-0.3.3.tar.gz 151.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for spintax-core 0.3.3
File Interpreter ABI Platform
spintax_core-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 226.0 kB

Release files / spintax_core-0.3.3.tar.gz

Download URL spintax_core-0.3.3.tar.gz
Size 151.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0fcde6a04a51ccc76fd0dbe80619f60a108919dfa9c616e6bd638d447902032f
BLAKE2b-256 checksum
How to use checksums
0d8b64066f7ec51e47ff8c52f27a074a0a5a48e1b4ee07af58dd70d62e395452
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release files / spintax_core-0.3.3-py3-none-any.whl

Download URL spintax_core-0.3.3-py3-none-any.whl
Size 74.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
89b5937a65d9113fb8f39c7d48b28ce7ae26e2f327d4242a0a86453e13f02116
BLAKE2b-256 checksum
How to use checksums
2d4f9913f219552f64e06b96ad07948faa9aa28b91c53b3ba2039339d81cea28
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.13

Release history Release notifications | RSS feed

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

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