Skip to main content

Bash style brace expander.

Project description

Unix Build Status Coverage pypi-version License

Bracex

Bracex is a brace expanding library (à la Bash) for Python. Brace expanding is used to generate arbitrary strings.

> echo {{a,b},c}d
ad bd cd

Bracex adds this ability to Python:

>>> bracex.expand(r'file-{1,2,3}.txt')
['file-1.txt', 'file-2.txt', 'file-3.txt']
>>> bracex.expand(r'file-{1,2,3}.txt')
['file-1.txt', 'file-2.txt', 'file-3.txt']
>>> bracex.expand(r'-v{,,}')
['-v', '-v', '-v']

Nested braces:

>>> bracex.expand(r'file-{{a,b},c}d.txt')
['file-ad.txt', 'file-bd.txt', 'file-cd.txt']

Numerical sequences:

>>> bracex.expand(r'file{0..3}.txt')
['file0.txt', 'file1.txt', 'file2.txt', 'file3.txt']
>>> bracex.expand(r'file{0..6..2}.txt')
['file0.txt', 'file2.txt', 'file4.txt', 'file6.txt']
>>> bracex.expand(r'file{00..10..5}.jpg')
['file00.jpg', 'file05.jpg', 'file10.jpg']

Alphabetic sequences:

>>> bracex.expand(r'file{A..D}.txt')
['fileA.txt', 'fileB.txt', 'fileC.txt', 'fileD.txt']
>>> bracex.expand(r'file{A..G..2}.txt')
['fileA.txt', 'fileC.txt', 'fileE.txt', 'fileG.txt']

Allows escaping:

>>> bracex.expand(r'file\{00..10..5}.jpg')
['file{00..10..5}.jpg']
>>> bracex.expand(r'file\{00..10..5}.jpg', keep_escapes=True)
['file\\{00..10..5}.jpg']

Bracex will not expand braces in the form of ${...}:

>>> bracex.expand(r'file${a,b,c}.jpg')
['file${a,b,c}.jpg']

Install

> pip install bracex

API

expand

def expand(string, keep_escapes=False):

expand accepts a string and returns a list of expanded strings. It will always return at least a single empty string [""]. By default, escapes will be resolved and the backslashes reduced accordingly, but keep_escapes will process the escapes without stripping them out.

iexpand

def iexpand(string, keep_escapes=False):

iexpand is just like expand except it returns a generator.

License

MIT License

Copyright (c) 2018 Isaac Muse

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bracex-1.0.2.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bracex-1.0.2-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file bracex-1.0.2.tar.gz.

File metadata

  • Download URL: bracex-1.0.2.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for bracex-1.0.2.tar.gz
Algorithm Hash digest
SHA256 86240653ec7c6427ece7b5b63c58dc80703ba33cdb9ef60c146a9282d7150947
MD5 6c522169bdc1ba56036ba3beb11b01b6
BLAKE2b-256 8a64ddd6d98feabfca62d394577dfa6bc0880b1ee311adaa559d5bd08d509ebd

See more details on using hashes here.

File details

Details for the file bracex-1.0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for bracex-1.0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 27306ef55c652a2ba15c019262dadd01090d4525dc8d640bff0b1542c112221a
MD5 eb3dcc635958b148eb1f158c8de135a9
BLAKE2b-256 8a97a60f474f28a7498c81ec4d6ddd0e5a1fbfe87187cb056298f5b0542d9f92

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page