Re: A useful, but painful, one-liner to edit money amounts

2010-08-07 Thread Chris Withers
DarkBlue wrote: On Aug 5, 7:06 pm, Chris Withers 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 curren

Re: A useful, but painful, one-liner to edit money amounts

2010-08-07 Thread Rebel Lion
On Aug 5, 12:33 pm, 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]) > >  >>> editmoney(0) > '0' >  >>> edit

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread Peter Otten
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 >> >

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread John Posner
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

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread MRAB
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 lo

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread John Posner
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_

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread Peter Otten
Chris Withers 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 us

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread DarkBlue
On Aug 5, 7:06 pm, Chris Withers 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

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread Chris Withers
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

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread Steven D'Aprano
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

Re: A useful, but painful, one-liner to edit money amounts

2010-08-05 Thread geremy condra
On Wed, Aug 4, 2010 at 11:30 PM, Peter Otten <__pete...@web.de> wrote: > 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

Re: A useful, but painful, one-liner to edit money amounts

2010-08-04 Thread Peter Otten
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]) > > > >>> editmoney(0) > '0' > >>> editmoney(13535)

Re: A useful, but painful, one-liner to edit money amounts

2010-08-04 Thread Steven D'Aprano
On Wed, 04 Aug 2010 21:33:31 -0700, 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]) What does the name

Re: A useful, but painful, one-liner to edit money amounts

2010-08-04 Thread Paul Rubin
John Nagle writes: > 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]) Too obscure. I usually use something like this: def editmoney(n): if n < 0: return '-' + edit

A useful, but painful, one-liner to edit money amounts

2010-08-04 Thread John Nagle
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' >>> e