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.
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
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)