Skip to main content

Spintax engine for Python — enumerations, permutations, variables, conditionals, plural agreement, includes. Zero dependencies.

Project description

spintax-core (Python)

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.

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

spintax_core-0.1.2.tar.gz (131.7 kB view details)

Uploaded Source

Built Distribution

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

spintax_core-0.1.2-py3-none-any.whl (63.0 kB view details)

Uploaded Python 3

File details

Details for the file spintax_core-0.1.2.tar.gz.

File metadata

  • Download URL: spintax_core-0.1.2.tar.gz
  • Upload date:
  • Size: 131.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for spintax_core-0.1.2.tar.gz
Algorithm Hash digest
SHA256 4254e377229cffccab7f264461293dfb3683bbf350e680c1adaa1b2a279e9625
MD5 ac9f5aaed6d868cda86dd27d73b9ddaf
BLAKE2b-256 289df04b604823186270cec388bdc05add7295315ec15c1ede24230769ab2c0e

See more details on using hashes here.

File details

Details for the file spintax_core-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: spintax_core-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 63.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for spintax_core-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7208e0d06dc910126f9ff90612cb50e54716b65c464d0cbd33daf70cb307a701
MD5 748046f5e4994649ec5fdb4d52d01933
BLAKE2b-256 7b1bc8b9b4767d1d51a7edf5fcb5c9929aa83d3cd8c80e295bd4ef95e1f7698a

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