This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author gosella
Recipients eric.araujo, eric.smith, gosella, mark.dickinson, mrabarnett
Date 2010-06-18.19:50:37
SpamBayes Score 1.6526199e-05
Marked as misclassified No
Message-id <1276890641.29.0.358167000197.issue7951@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2010-06-18 19:50:41gosellasetrecipients: + gosella, mark.dickinson, eric.smith, eric.araujo, mrabarnett
2010-06-18 19:50:41gosellasetmessageid: <1276890641.29.0.358167000197.issue7951@psf.upfronthosting.co.za>
2010-06-18 19:50:39gosellalinkissue7951 messages
2010-06-18 19:50:39gosellacreate