Skip to main content

PyBraces - Python with Braces

Python is a great language, but writing one-liners in it can be challenging. PyBraces fixes that.

TL;DR

$ pyb -c 'def fn(n): { for i in range(n): { print(f"Hello World {i}"); } } fn(5);'

Hello World 0
Hello World 1
Hello World 2
Hello World 3
Hello World 4

$ pyb -t -c 'def fn(n): { for i in range(n): { print(f"Hello World {i}"); } } fn(5);'

def fn(n):
    for i in range(n):
        print(f"Hello World {i}")
fn(5)

Installation

pip install pybraces

Usage

# Transform input.b.py to output.py
$ pyb -t < input.b.py > output.py

# Transform oneliner script into Python
$ pyb -t -c 'if 1: { if 2: { print(3) } }'

# Execute oneliner script as Python
$ pyb -c 'if 1: { if 2: { print(3) } }'
$ pyb -c 'if 1: { if 2: print(3) }'

Description

This package is a preprocessor that transforms Python code with braces into Python code with indentation. Although it's targeted to writing one-liners in Python, it can be used to write Python code with braces.

The implementation is extremely lightweight and has only one dependency: regex package for recursive regexes. Once Python natively supports recursive regexes, the dependency will be removed.

In comparison to Bython, PyBraces doesn't require a special syntax for inline dictionaries.

The pyb command that comes with this package can be used to transform Python code with braces into Python code with indentation or to directly execute code as Python. It then passes all arguments to the Python interpreter.

Transformation Rules

The transformation is done by the following rules:

  1. : {...} in indented appropriately, braces are removed.
  2. : {...} can be nested.
  3. ; inserts a newline, this terminating the statement.
  4. # comments are removed.
  5. Newlines are replaced with a space.
  6. Everything inside of [] or () is pasted as is.
  7. Everything else is pasted as is.

Why semicolon?

One might ask why the semicolon is kept in the "braced" syntax?

Why

if 1: { print(1) }

not just

if 1 { print(1) }

This is to avoid confusion with dicts and sets where braces are used in normal Python so that normal syntax for dicts and sets can be used with braces:

a = {"qwe": 123, "asd": 456}
b = 5 in {1, 2, 3}

What's the standard file extension for PyBraces?

Although one-liners are not supposed to be saved in files, the suggested extension for PyBraces is .b.py. PyBraces syntax is not exactly Python but still close to it, so developers can benefit from syntax highlighting and other features of their IDEs to some extent.

Links

Metadata

Release files for pybraces 0.2.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 pybraces 0.2.0
File Size Uploaded Provenance Actions
pybraces-0.2.0.tar.gz 20.7 kB Without provenance Details for pybraces-0.2.0.tar.gz

Built distribution (wheel)

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

Total release size: 39.9 kB

Release files / pybraces-0.2.0.tar.gz

Download URL pybraces-0.2.0.tar.gz
Size 20.7 kB
Tags Source
SHA-256 checksum
How to use checksums
f8e90a2d7f1a67b085a915baccbbfd948c54492a20602c4a3ccff53c145c6767
BLAKE2b-256 checksum
How to use checksums
3b3f91d24f590dce3130ce1723c21e42cadbc0de89c123564f36dc5d2736a818
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Release files / pybraces-0.2.0-py3-none-any.whl

Download URL pybraces-0.2.0-py3-none-any.whl
Size 19.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9210df0165acc440912d699d2b19ffc8e922e74ebf34dc19681a1395b8bebe63
BLAKE2b-256 checksum
How to use checksums
ef6a66c5d2b7738a2e62b8e83bf96c74432c6bb97964af5ba95db187371d06a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.1.1 CPython/3.11.9

Security for pybraces 0.2.0

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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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