python的bash风格大括号扩展
braceexpand的Python项目详细描述
按中所述实现大括号扩展 bash(1), 具有以下限制:
- 包含不平衡大括号的模式将引发 UnbalancedBracesError异常。在bash中,不平衡的大括号将 要么被部分扩展,要么被忽略。
- 像'{Z..a}'或'{a..Z}'这样的混合大小写字符范围将 不包括Z和a之间的字符[]^_`。
braceexpand使用python 2.6、2.7、3.2、3.3、3.4进行测试。
安装
将braceexpand.py文件放入项目中,或安装 braceexpand来自pypi的包:
$ pip install braceexpand
示例
函数braceexpand返回扩展上的迭代器 由图案产生的。
>>>frombraceexpandimportbraceexpand# 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']# Unbalanced braces raise an exception.>>>list(braceexpand('{1{2,3}'))Traceback(mostrecentcalllast):...UnbalancedBracesError:Unbalancedbraces:'{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']
许可证
BraceExpand是在麻省理工学院许可证下获得许可的。查看包含的文件 LICENSE了解详细信息。