From: MRAB on
John Posner wrote:
> On 8/5/2010 12:33 AM, John Nagle wrote:
>> There's got to be a better way to do this:
>>
>>
>> def editmoney(n) :
>> return((",".join(reduce(lambda lst, item : (lst + [item]) if
>> item else lst,
>> re.split(r'(\d\d\d)',str(n)[::-1]),[])))[::-1])
>>
>
> Here's a more elegant variant, using regexp lookahead:
>
> def thous_format(integer_string):
> """
> add comma thousands separator(s) to an integer-valued string
> """
> return re.sub(r'(\d{3})(?=\d)', r'\1,', integer_string[::-1])[::-1]
>
> I *thought* that I had found this on python-list on or about July 5, but
> I didn't find the thread after a search through the archives.
>
You don't need to reverse the string:

def thous_format(integer_string):
"""
add comma thousands separator(s) to an integer-valued string
"""
return re.sub(r"(?<=\d)(?=(?:\d\d\d)+$)", ",", integer_string)
From: John Posner on
On 8/5/2010 12:36 PM, MRAB wrote:

> You don't need to reverse the string:
>
> def thous_format(integer_string):
> """
> add comma thousands separator(s) to an integer-valued string
> """
> return re.sub(r"(?<=\d)(?=(?:\d\d\d)+$)", ",", integer_string)

Nice! My first encounter with a look-behind! It appears that a slight
simplification is possible:

return re.sub(r"(?<=\d)(?=(\d\d\d)+$)", ",", integer_string)

There no harm in putting \d\d\d into a group, though the group is never
used.

-John
From: Peter Otten on
Steven D'Aprano wrote:

> On Thu, 05 Aug 2010 00:22:57 -0700, geremy condra wrote:
>
>>>>>> locale.setlocale(locale.LC_ALL, "")
>>> 'de_DE.UTF-8'
>>>>>> print locale.currency(13535, grouping=True)
>>> 13.535,00 €
>>>>>> print locale.format("%d", 13535, grouping=True)
>>> 13.535
>>>
>>> Peter
>>
>> I had literally no idea this existed. Thanks.
>
> I knew it existed, but completely forgot about it.
>
> Thanks also Peter.

You're welcome!
From: Rebel Lion on
On Aug 5, 12:33 pm, John Nagle <na...(a)animats.com> wrote:
> There's got to be a better way to do this:
>
> def editmoney(n) :
>      return((",".join(reduce(lambda lst, item : (lst + [item]) if
>          item else lst,
>          re.split(r'(\d\d\d)',str(n)[::-1]),[])))[::-1])
>
>  >>> editmoney(0)
> '0'
>  >>> editmoney(13535)
> '13,535'
>  >>> editmoney(-14535)
> '-14,535'
>  >>> editmoney(123456)
> '123,456'
>  >>> editmoney(1234567890)
> '1,234,567,890'
>  >>> editmoney(-1234)
> '-1,234'
>
> The basic idea here is that we want to split the string of digits
> into groups of 3 digits, aligned at the right.  Because regular
> expressions are right to left, we have to reverse the string to
> do that, then reverse again at the end.  s[::-1} reverses an
> interable.
>
> "split" with a capturing group introduces empty strings into the
> list.  Hence the "reduce" and lambda to get rid of them.
>
> Any better ideas?
>
> (Yes, I know there's a built-in feature for this scheduled for
> Python 2.7.)
>
>                                         John Nagle

In [1]: '{:,}'.format(-12345678)
Out[1]: '-12,345,678'
From: Chris Withers on
DarkBlue wrote:
> On Aug 5, 7:06 pm, Chris Withers <ch...(a)simplistix.co.uk> wrote:
>> Peter Otten wrote:
>>>>>> locale.setlocale(locale.LC_ALL, ("en_US", "UTF-8"))
>>> 'en_US.UTF8'
>>>>>> print locale.currency(13535, grouping=True)
>>> $13,535.00
>> Okay, so if I'm writing a wsgi app, and I want to format depending on
>> the choices of the currently logged in users, what would you recommend?
>>
>> I can't do setlocale, since that would affect all users, and in a
>> mult-threaded environment that would be bad.
>>
>> Does that mean the whole locale package is useless to all web-app builders?
>>
>> Chris
>
> from re import *
>
> class editmoney(float):
> def __init__(self,mymoney):

What does this have to do with my question about locales in
multi-threaded environments?

Chris

--
Simplistix - Content Management, Batch Processing & Python Consulting
- http://www.simplistix.co.uk