# 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);'`
```python
def fn(n):
for i in range(n):
print(f"Hello World {i}")
fn(5)
```
# Installation
```bash
pip install pybraces
```
# Usage
```bash
# 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](https://pypi.org/project/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.
5. `# comments` are removed.
6. Newlines are replaced with a space.
7. Everything inside of `[]` or `()` is pasted as is.
8. Everything else is pasted as is.
## Why semicolon?
One might ask why the semicolon is kept in the "braced" syntax?
Why
```python
if 1: { print(1) }
```
not just
```python
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:
```python
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
* This project on GitHub: [PyBraces](https://github.com/ershov/pybraces)
* Prior art: [Bython](https://github.com/mathialo/bython)
Raw data
{
"_id": null,
"home_page": "https://github.com/ershov/pybraces",
"name": "pybraces",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Yury Ershov",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/3b/3f/91d24f590dce3130ce1723c21e42cadbc0de89c123564f36dc5d2736a818/pybraces-0.2.0.tar.gz",
"platform": null,
"description": "# PyBraces - Python with Braces\n\nPython is a great language, but writing one-liners in it can be challenging.\nPyBraces fixes that.\n\n## TL;DR\n\n`$ pyb -c 'def fn(n): { for i in range(n): { print(f\"Hello World {i}\"); } } fn(5);'`\n\n```\nHello World 0\nHello World 1\nHello World 2\nHello World 3\nHello World 4\n```\n\n`$ pyb -t -c 'def fn(n): { for i in range(n): { print(f\"Hello World {i}\"); } } fn(5);'`\n\n```python\ndef fn(n):\n for i in range(n):\n print(f\"Hello World {i}\")\nfn(5)\n```\n\n# Installation\n\n```bash\npip install pybraces\n```\n\n# Usage\n\n```bash\n# Transform input.b.py to output.py\n$ pyb -t < input.b.py > output.py\n\n# Transform oneliner script into Python\n$ pyb -t -c 'if 1: { if 2: { print(3) } }'\n\n# Execute oneliner script as Python\n$ pyb -c 'if 1: { if 2: { print(3) } }'\n$ pyb -c 'if 1: { if 2: print(3) }'\n```\n\n# Description\n\nThis package is a preprocessor that transforms Python code with braces into Python code with indentation.\nAlthough it's targeted to writing one-liners in Python, it can be used to write Python code with braces.\n\nThe implementation is extremely lightweight and has only one dependency:\n[regex](https://pypi.org/project/regex/) package for recursive regexes.\nOnce Python natively supports recursive regexes, the dependency will be removed.\n\nIn comparison to Bython, PyBraces doesn't require a special syntax for inline dictionaries.\n\nThe `pyb` command that comes with this package can be used to transform Python code with braces\ninto Python code with indentation or to directly execute code as Python.\nIt then passes all arguments to the Python interpreter.\n\n## Transformation Rules\n\nThe transformation is done by the following rules:\n\n1. `: {...}` in indented appropriately, braces are removed.\n2. `: {...}` can be nested.\n3. `;` inserts a newline, this terminating the statement.\n5. `# comments` are removed.\n6. Newlines are replaced with a space.\n7. Everything inside of `[]` or `()` is pasted as is.\n8. Everything else is pasted as is.\n\n## Why semicolon?\n\nOne might ask why the semicolon is kept in the \"braced\" syntax?\n\nWhy\n```python\nif 1: { print(1) }\n```\nnot just\n```python\nif 1 { print(1) }\n```\n\nThis is to avoid confusion with dicts and sets where braces are used in normal Python so that\nnormal syntax for dicts and sets can be used with braces:\n\n```python\na = {\"qwe\": 123, \"asd\": 456}\nb = 5 in {1, 2, 3}\n```\n\n## What's the standard file extension for PyBraces?\n\nAlthough one-liners are not supposed to be saved in files, the suggested extension for PyBraces is\n`.b.py`. PyBraces syntax is not exactly Python but still close to it, so\ndevelopers can benefit from syntax highlighting and other features of their IDEs\nto some extent.\n\n# Links\n\n* This project on GitHub: [PyBraces](https://github.com/ershov/pybraces)\n* Prior art: [Bython](https://github.com/mathialo/bython)\n",
"bugtrack_url": null,
"license": "GPL3",
"summary": "Python with braces",
"version": "0.2.0",
"project_urls": {
"Homepage": "https://github.com/ershov/pybraces"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ef6a66c5d2b7738a2e62b8e83bf96c74432c6bb97964af5ba95db187371d06a6",
"md5": "d194e98ba8f123a0c1b4668c1e40df78",
"sha256": "9210df0165acc440912d699d2b19ffc8e922e74ebf34dc19681a1395b8bebe63"
},
"downloads": -1,
"filename": "pybraces-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "d194e98ba8f123a0c1b4668c1e40df78",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 19235,
"upload_time": "2024-11-05T07:56:48",
"upload_time_iso_8601": "2024-11-05T07:56:48.486266Z",
"url": "https://files.pythonhosted.org/packages/ef/6a/66c5d2b7738a2e62b8e83bf96c74432c6bb97964af5ba95db187371d06a6/pybraces-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3b3f91d24f590dce3130ce1723c21e42cadbc0de89c123564f36dc5d2736a818",
"md5": "8142fdb69e24cab043f9e67866489e19",
"sha256": "f8e90a2d7f1a67b085a915baccbbfd948c54492a20602c4a3ccff53c145c6767"
},
"downloads": -1,
"filename": "pybraces-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "8142fdb69e24cab043f9e67866489e19",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 20672,
"upload_time": "2024-11-05T07:56:50",
"upload_time_iso_8601": "2024-11-05T07:56:50.602405Z",
"url": "https://files.pythonhosted.org/packages/3b/3f/91d24f590dce3130ce1723c21e42cadbc0de89c123564f36dc5d2736a818/pybraces-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-05 07:56:50",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ershov",
"github_project": "pybraces",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "pybraces"
}