File: README.md

package info (click to toggle)
braceexpand 0.1.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 124 kB
  • sloc: python: 248; makefile: 12
file content (86 lines) | stat: -rw-r--r-- 2,278 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Bash-style brace expansion for Python

[![build-status-img]][build-status-url]

Implements Brace Expansion as described in [bash(1)][1], with the following
limitations:

  * A pattern containing unbalanced braces will raise an
    `UnbalancedBracesError` exception. In bash, unbalanced braces will either
    be partly expanded or ignored.

  * A mixed-case character range like `'{Z..a}'` or `'{a..Z}'` will not
    include the characters `` []^_` `` between `Z` and `a`.

`braceexpand` is tested with Python 2.7, and 3.6+

## Installation

Install the `braceexpand` package from pypi:

    $ pip install braceexpand

## Examples

The `braceexpand` function returns an iterator over the expansions generated
from a pattern.

~~~python
>>> from braceexpand import braceexpand

# Integer range
>>> list(braceexpand('item{1..3}'))
['item1', 'item2', 'item3']

# Character range
>>> list(braceexpand('{a..c}'))
['a', 'b', 'c']

# Sequence
>>> list(braceexpand('index.html{,.backup}'))
['index.html', 'index.html.backup']

# Nested patterns
>>> list(braceexpand('python{2.{5..7},3.{2,3}}'))
['python2.5', 'python2.6', 'python2.7', 'python3.2', 'python3.3']

# Prefixing an integer with zero causes all numbers to be padded to
# the same width.
>>> list(braceexpand('{07..10}'))
['07', '08', '09', '10']

# An optional increment can be specified for ranges.
>>> list(braceexpand('{a..g..2}'))
['a', 'c', 'e', 'g']

# Ranges can go in both directions.
>>> list(braceexpand('{4..1}'))
['4', '3', '2', '1']

# Numbers can be negative
>>> list(braceexpand('{2..-1}'))
['2', '1', '0', '-1']

# Unbalanced braces raise an exception.
>>> list(braceexpand('{1{2,3}'))
Traceback (most recent call last):
    ...
UnbalancedBracesError: Unbalanced braces: '{1{2,3}'

# By default, the backslash is the escape character.
>>> list(braceexpand(r'{1\{2,3}'))
['1{2', '3']

# Setting 'escape' to False disables backslash escaping.
>>> list(braceexpand(r'\{1,2}', escape=False))
['\\1', '\\2']
~~~

[1]: http://man7.org/linux/man-pages/man1/bash.1.html#EXPANSION
[build-status-url]: https://travis-ci.org/trendels/braceexpand
[build-status-img]: https://travis-ci.org/trendels/braceexpand.svg

## License

braceexpand is licensed unter the MIT License. See the included file `LICENSE`
for details.