Braceexpand

0.1.7 · active · verified Fri Apr 10

Braceexpand is a Python library that implements Bash-style brace expansion, allowing for the generation of arbitrary strings based on patterns. It provides an iterator over expanded strings, supporting integer ranges, character ranges, sequences, and nested patterns. The current version is 0.1.7, and it is compatible with Python 2.7 and 3.6+. While it closely mimics Bash behavior, it has a slower release cadence, with the last major update to 0.1.7 being in May 2021.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic brace expansion for integer and character ranges, sequence expansion, and nested patterns. It also shows how to catch the `UnbalancedBracesError` for malformed input strings, a key deviation from Bash behavior. The `braceexpand` function returns an iterator, so `list()` is often used to see all results immediately.

from braceexpand import braceexpand

# Example 1: Integer range
expanded_items = list(braceexpand('item{1..3}'))
print(f"Integer range: {expanded_items}")

# Example 2: Character range
expanded_chars = list(braceexpand('{a..c}'))
print(f"Character range: {expanded_chars}")

# Example 3: Sequence expansion
expanded_files = list(braceexpand('index.html{,.backup}'))
print(f"Sequence expansion: {expanded_files}")

# Example 4: Nested patterns
expanded_versions = list(braceexpand('python{2.{5..7},3.{2,3}}'))
print(f"Nested patterns: {expanded_versions}")

# Example 5: Handling unbalanced braces (raises an exception)
from braceexpand import UnbalancedBracesError
try:
    list(braceexpand('{1{2,3}'))
except UnbalancedBracesError as e:
    print(f"Caught expected error: {e}")

view raw JSON →