Re: Can Python format long integer 123456789 to 12,3456,789 ?

2006-06-02 Thread Warren Block
John Machin <[EMAIL PROTECTED]> wrote:
> On 2/06/2006 9:08 AM, A.M wrote:
>> Hi,
>> 
>> Is there any built in feature in Python that can format long integer 
>> 123456789 to 12,3456,789 ?
>> 
>> Thank you,
>> Alan
>
> Not that I know of, but this little kludge may help:
> 8<---
> import re
>
> subber = re.compile(r'^(-?\d+)(\d{3})').sub
>
> def fmt_thousands(amt, sep):
>  if amt in ('', '-'):
>  return ''
>  repl = r'\1' + sep + r'\2'
>  while True:
>  new_amt = subber(repl, amt)
>  if new_amt == amt:
>  return amt
>  amt = new_amt
>
> if __name__ == "__main__":
>  for prefix in ['', '-']:
>  for k in range(11):
>  arg = prefix + "1234567890.1234"[k:]
>  print "<%s> <%s>" % (arg, fmt_thousands(arg, ","))
> 8<---

Why not just port the Perl "commify" code?  You're close to it, at least
for the regex:

# From perldoc perlfaq5
# 1 while s/^([-+]?\d+)(\d{3})/$1,$2/;

# Python version

import re

def commify(n):
while True:
(n, count) = re.subn(r'^([-+]?\d+)(\d{3})', r'\1,\2', n)
if count == 0: break
return n

-- 
Warren Block * Rapid City, South Dakota * USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Installation Problem

2006-06-04 Thread Warren Block
Marshall Dudley <[EMAIL PROTECTED]> wrote:
> Sorry, this is a FreeBSD system 4.8-RELEASE
>
> I found another set of documents that say to use the following to
> install::
>
> python setup.py install
>
> but after running it, I still have the same problem.

[top-posting trimmed, please don't do that]

Doesn't the port work for 4.8?  It does work on FreeBSD 4.11, but there 
may have been changes to the ports system since 4.8.  (You should 
consider updating to 4.11.)

There are several patch files in the FreeBSD port, including
one to setup.py.

The easiest way is to cvsup your ports tree and then

cd /usr/ports/lang/python
make
make install
make clean

-- 
Warren Block * Rapid City, South Dakota * USA
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Strings for a newbie

2005-05-28 Thread Warren Block
James Hofmann <[EMAIL PROTECTED]> wrote:
> Malcolm Wooden  dtptypes.com> writes:
>> 
>> I'm trying to get my head around Python but seem to be failing miserably. I 
>> use RealBasic on a Mac and find it an absolute dream! But PythonUGH!
>> 
>> I want to put a sentence of words into an array, eg "This is a sentence of 
>> words"
>> 
>> In RB it would be simple:
>> 
>> Dim s as string
>> Dim a(-1) as string
>> Dim i as integer
>> 
>> s = "This is a sentence of words"
>> For i = 1 to CountFields(s," ")
>>   a.append NthField(s," ",i)
>> next
>> 
>> That's it an array a() containing the words of the sentence.

[snip]

> Now a "slim" version:
>
> s = "This is a sentence of words"
> print s.split()

To match the original program, it should be:

s = "This is a sentence of words"
a = s.split()

...assigning the list returned by split() to a variable called a.

-- 
Warren Block * Rapid City, South Dakota * USA
-- 
http://mail.python.org/mailman/listinfo/python-list