Bracex
Overview
Bracex is a brace expanding library (à la Bash) for Python. Brace expanding is used to generate arbitrary strings.
Bracex adds this ability to Python:
and as a command:
$ python3 -m bracex -0 "base/{a,b}/{1..2}" | xargs -0 mkdir -p
$ tree base/
base/
├── a
│ ├── 1
│ └── 2
└── b
├── 1
└── 2
-
Why Bracex over other solutions?
Bracex actually follows pretty closely to how Bash processes braces. It is not a 1:1 implementation of how Bash handles braces, but generally, it follows very closely. Almost all of the test cases are run through Bash first, then our implementation is compared against the results Bash gives. There are a few cases where we have purposely deviated. For instance, we are not handling Bash's command line inputs, so we are not giving special meaning to back ticks and quotes at this time.
On the command line Bracex can handle more expansions than Bash itself.
More Examples:
Nested braces:
Numerical sequences:
Alphabetic sequences:
Allows escaping:
Bracex will not expand braces in the form of ${...}:
Install
API
expand()
expand accepts a string and returns a list of expanded strings. It expansions are all empty, an empty array will be
returned. If it is desired to always at least have an empty string, return_empty can be be enabled to always return
at least [""].
3.0 Change in Behavior
Prior to 3.0, expand always returned at least [""], even if all expansions were empty. Now return_empty must be
enabled to restore this behavior.
By default, escapes will be resolved and the backslashes reduced accordingly, but keep_escapes will process
the escapes without stripping them out.
By default, brace expansion growth is limited to 1000. This limit can be configured via the limit option. If you
would like to remove the limit option, you simply set limit to 0.
iexpand()
iexpand is just like expand except it returns a generator.
Command line interface
$ python3 -m bracex --help
usage: python -mbracex [-h] [--terminator STR | -0] [--version] expression
Expands a bash-style brace expression, and outputs each expansion.
positional arguments:
expression Brace expression to expand
optional arguments:
-h, --help show this help message and exit
--terminator STR, -t STR
Terminate each expansion with string STR (default: \n)
-0 Terminate each expansion with a NUL character
--version show program's version number and exit