I'm Sure There's A Better Way

Tim Daneliuk tundra at tundraware.com
Mon Jul 9 19:30:01 CEST 2001


Bengt Richter wrote:
> 
> On 09 Jul 2001 00:10:01 GMT, Tim Daneliuk <tundra at tundraware.com>
> wrote:
> 
> >Bengt Richter wrote:
> >>
> <SNIP>
> >------------------------------------------------------------------------------
> For your amusement, here is what the above winning re does with some
> numbers, followed by the code. You can just type in a list of test
> numbers on the command line (which wrapped), like so:
> Is it what you wanted?
> 
> [18:22] C:\pywk\numck>numcks.py - -0 -.00 -0.00 00.00 012 012.34
> 12.345 1.2.3 0.00 0
>        -: Winner says bad, runner-up says bad
>       -0: Winner says  ok, runner-up says bad
>     -.00: Winner says  ok, runner-up says bad
>    -0.00: Winner says  ok, runner-up says bad
>    00.00: Winner says  ok, runner-up says bad
>      012: Winner says  ok, runner-up says bad
>   012.34: Winner says  ok, runner-up says bad
>   12.345: Winner says bad, runner-up says bad
>    1.2.3: Winner says bad, runner-up says bad
>     0.00: Winner says  ok, runner-up says  ok
>        0: Winner says  ok, runner-up says  ok
> 
> (some lines wrapped below)
> __________________________________________________
> 
> # numcks.py
> import sys
> import re
> def ck(x):
>     if re.match(
> r'(-(?!(0*\.?0+$|0+\.0*$)))?(0|\d?\.\d\d|[1-9]\d*(\.\d\d)?)$', x):
>             return 'ok'
>     else:
>             return 'bad'
> 
> # declared winner re: ^-?((\d+(\.\d{2})?)|(\.\d{2}))$
> def ckw(x):
>     if re.match( r'^-?((\d+(\.\d{2})?)|(\.\d{2}))$', x):
>             return 'ok'
>     else:
>             return 'bad'
> 
> def main():
>     args = sys.argv[1:]
>     if not args:
>         sys.stderr.write("usage: %s number ...\n" % sys.argv[0])
>         sys.exit(2)
>     for num in args:
>         print "%8s: Winner says %3s, runner-up says %3s" % (num,
> ckw(str(num)),ck(str(num)))
> 
> if __name__ == "__main__":
>     main()
> __________________________________________________________________


This is a specification problem on my part (fuzzy definitions allow almost
any conclusion to be right ;)  Your way, of course, is a bit cleaner to
look up, but I would argue it is no more "correct" -  My overarching
goal here was to validate an entry as being a legitimate currency amount.
0.00. -00000, -0012312.12 are all legitimate dollar (pound, euro, peso...)
string representations which can be correctly coerced into floats (ignoring
delimiter characters and rounding errors for the moment).

In any case, I appreciate the Skull Sweat you (and everyone else) put into
this.  Yet another demonstration that regular expressions may be elegant,
but they're very tricky to get exactly right ;))  As a token of my
esteem, I'd like to send you a 7-figure check for -0000000.00   ;)))))

-- 
------------------------------------------------------------------------------
Tim Daneliuk
tundra at tundraware.com



More information about the Python-list mailing list