Bash Style Brace Expander
Bracex is a Python library that provides Bash-style brace expansion, enabling the generation of arbitrary strings from patterns. It closely emulates Bash's brace processing, supporting comma-separated lists, numerical and alphabetical sequences (ranges), and optional step increments. The library is actively maintained, with version 2.6 being the latest, and releases occur regularly to ensure compatibility with newer Python versions and address minor fixes.
Common errors
-
ModuleNotFoundError: No module named 'bracex'
cause The 'bracex' library is not installed in your Python environment, or the Python interpreter cannot find it.fixInstall the library using pip: `pip install bracex` -
bracex.BraceExpansionError: Brace expansion exceeded the limit of 1000 items.
cause The brace expansion pattern you provided generates more strings than the default `limit` of 1000 allowed by `bracex.expand()` or `bracex.iexpand()` to prevent 'brace bombs' or excessive memory usage.fixIncrease the `limit` parameter in the `expand()` or `iexpand()` function call, or set `limit=0` to disable it if you are certain of the pattern's safety and have sufficient memory. Using `iexpand()` can also help manage memory for large results. ```python import bracex # To increase the limit expanded_list = bracex.expand('{1..5000}', limit=5000) # To disable the limit (use with caution for unbounded expansions) expanded_list = bracex.expand('{1..infinity}', limit=0) ``` -
ERROR: Package 'bracex' requires Python '>=3.9' but the running Python is X.Y.Z
cause You are attempting to install or run `bracex` version 2.6 (or newer) with a Python version older than 3.9, which is not supported by this version of the library.fixUpgrade your Python environment to version 3.9 or newer. Alternatively, if feasible, install an older version of `bracex` that supports your current Python version (e.g., `pip install bracex==2.5` for Python 3.8, if available, but upgrading Python is recommended).
Warnings
- breaking Bracex has progressively dropped support for older Python versions. Version 2.6 (current) requires Python 3.9 or higher. Previous versions dropped support for Python 3.8 (in 2.6), 3.7 (in 2.4), 3.6 (in 2.3), and 3.5 (in 2.1.0).
- gotcha The `expand()` and `iexpand()` functions include a `limit` parameter (defaulting to 1000) to prevent excessively large expansions, which could lead to performance issues or 'brace bombs'. If an expansion exceeds this limit, an error is raised.
- gotcha While `bracex` closely mimics Bash's brace expansion, it is not a 1:1 implementation. It may deviate in certain edge cases, particularly regarding how Bash handles command-line inputs (e.g., backticks and quotes).
- deprecated The build backend for `bracex` switched from Setuptools to Hatch in version 2.3. While this primarily affects maintainers and packagers, it's a significant internal change.
Install
-
pip install bracex
Imports
- expand
from bracex import expand # Less common, but still correct if preferred for brevity.
import bracex expanded_list = bracex.expand('file-{a,b}.txt') - iexpand
import bracex expanded_generator = bracex.iexpand('file-{1..3}.jpg')
Quickstart
import bracex
# Basic list expansion
print(bracex.expand(r'file-{a,b,c}.txt'))
# Nested brace expansion
print(bracex.expand(r'path/to/{{foo,bar},baz}/file.txt'))
# Numerical sequence (range) expansion
print(bracex.expand(r'image{00..02}.png'))
# Numerical sequence with step
print(bracex.expand(r'item{1..10..3}.log'))
# Alphabetic sequence
print(bracex.expand(r'letter{A..D}.md'))
# Using the generator version
for item in bracex.iexpand(r'data-{1..5}.csv'):
print(item)