Message108132
I finally managed to get the time to finish the patch that allows negative indexes inside square brackets so now they work with the same semantics as in a python expression:
>>> '{0[-1]}'.format(['abc', 'def'])
'def'
>>> '{0[-2]}'.format(['abc', 'def'])
'abc'
>>> '{0[-1][-1]}'.format(['abc', ['def']])
'def'
They work auto-numbered fields too:
>>> '{[-1]}'.format(['abc', 'def'])
'def'
Also, a positive sign is now accepted as part of a valid integer:
>>> '{0[+1]}'.format(['abc', 'def'])
'def'
As a bonus, negatives indexes are also allowed to refer to positional arguments:
>>> '{-1}'.format('abc', 'def')
'def'
>>> '{-2}'.format('abc', 'def')
'abc'
I'm attaching a patch against trunk. I added some tests for this functionality in test_str.py.
By the way, this code doesn't work anymore:
>>> "{[-1]}".format({'-1': 'X'})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: -1L
But now it behaves in the same way as:
>>> "{[1]}".format({'1': 'X'})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 1L
I didn't attempt to ignore whitespaces when trying to parse the index as an integer (to allow that "{ 0 }" can be treated as "{0}" and "{0[1]}" as "{ 0 [ 1 ] }") because I'm not sure if this behavior is desirable. |
|
Date |
User |
Action |
Args |
2010-06-18 19:50:41 | gosella | set | recipients:
+ gosella, mark.dickinson, eric.smith, eric.araujo, mrabarnett |
2010-06-18 19:50:41 | gosella | set | messageid: <1276890641.29.0.358167000197.issue7951@psf.upfronthosting.co.za> |
2010-06-18 19:50:39 | gosella | link | issue7951 messages |
2010-06-18 19:50:39 | gosella | create | |
|