Skip to main content

Python with braces

Project description

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

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

pybraces-0.1.1.tar.gz (17.7 kB view details)

Uploaded Source

Built Distribution

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

pybraces-0.1.1-py3-none-any.whl (17.5 kB view details)

Uploaded Python 3

File details

Details for the file pybraces-0.1.1.tar.gz.

File metadata

  • Download URL: pybraces-0.1.1.tar.gz
  • Upload date:
  • Size: 17.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for pybraces-0.1.1.tar.gz
Algorithm Hash digest
SHA256 917204ba76c4bc503d53333804988670e1528cddd153e2b01db47f106f13b19d
MD5 d615b61c184f0045440206d5cbe9fe46
BLAKE2b-256 28db1cca7afaab27e8f1a54c086233804dbb4d02fde2d48a847962650d94a75e

See more details on using hashes here.

File details

Details for the file pybraces-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pybraces-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 17.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for pybraces-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a5774aca5014374ac4dbe012930e27e55253d46617ad90f25066b513aaf15369
MD5 fba1089fc6605402c2d6c2060ccd95ad
BLAKE2b-256 8ef85de06b395efc1e897b9aa2b726640ba7d087f624c1f2068ade251082fd59

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