{"id":2417,"library":"braceexpand","title":"Braceexpand","description":"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.","status":"active","version":"0.1.7","language":"en","source_language":"en","source_url":"https://github.com/trendels/braceexpand","tags":["bash","expansion","glob","utility","string","pattern matching"],"install":[{"cmd":"pip install braceexpand","lang":"bash","label":"Install from PyPI"}],"dependencies":[],"imports":[{"symbol":"braceexpand","correct":"from braceexpand import braceexpand"},{"note":"Import for handling errors related to malformed brace patterns.","symbol":"UnbalancedBracesError","correct":"from braceexpand import UnbalancedBracesError"}],"quickstart":{"code":"from braceexpand import braceexpand\n\n# Example 1: Integer range\nexpanded_items = list(braceexpand('item{1..3}'))\nprint(f\"Integer range: {expanded_items}\")\n\n# Example 2: Character range\nexpanded_chars = list(braceexpand('{a..c}'))\nprint(f\"Character range: {expanded_chars}\")\n\n# Example 3: Sequence expansion\nexpanded_files = list(braceexpand('index.html{,.backup}'))\nprint(f\"Sequence expansion: {expanded_files}\")\n\n# Example 4: Nested patterns\nexpanded_versions = list(braceexpand('python{2.{5..7},3.{2,3}}'))\nprint(f\"Nested patterns: {expanded_versions}\")\n\n# Example 5: Handling unbalanced braces (raises an exception)\nfrom braceexpand import UnbalancedBracesError\ntry:\n    list(braceexpand('{1{2,3}'))\nexcept UnbalancedBracesError as e:\n    print(f\"Caught expected error: {e}\")","lang":"python","description":"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."},"warnings":[{"fix":"Ensure all opening braces `{` have a corresponding closing brace `}`. Implement try-except blocks for `UnbalancedBracesError` if processing user-supplied patterns.","message":"Unlike Bash, a pattern containing unbalanced braces will raise an `UnbalancedBracesError` exception. Bash would typically partly expand or ignore such patterns.","severity":"breaking","affected_versions":"All versions (0.1.0+)"},{"fix":"Be explicit with character sets if needing to include non-alphanumeric characters, or use separate expansions for uppercase and lowercase ranges. E.g., `{a..z,A..Z}`.","message":"Mixed-case character ranges like `{Z..a}` or `{a..Z}` will not include the characters `[]^_`` between 'Z' and 'a' in ASCII order. This differs from some Bash environments.","severity":"gotcha","affected_versions":"All versions (0.1.0+)"},{"fix":"Always wrap the call in `list()` if you require a complete list, or iterate directly over the returned object.","message":"The `braceexpand` function returns an iterator, not a list. If you need all expanded strings at once, you must explicitly convert the iterator to a list (e.g., `list(braceexpand(...))`).","severity":"gotcha","affected_versions":"All versions (0.1.0+)"},{"fix":"For extremely large or performance-critical expansions, consider alternative methods or preprocess patterns to minimize expansion complexity.","message":"Performance may be an issue for very large datasets. The library is not designed for high-performance scenarios with extensive expansions.","severity":"gotcha","affected_versions":"All versions (0.1.0+)"},{"fix":"Use raw strings (e.g., `r'...'`) for patterns with backslashes you want treated literally or set `braceexpand(pattern, escape=False)` to disable backslash escaping entirely.","message":"The backslash (`\\`) is the default escape character. If you want to expand patterns containing literal backslashes or disable escaping, you need to use raw strings and/or set `escape=False`.","severity":"gotcha","affected_versions":"All versions (0.1.0+)"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}