I'm Sure There's A Better Way

Bengt Richter bokr at accessone.com
Sun Jul 8 21:27:19 EDT 2001


On 09 Jul 2001 00:10:01 GMT, Tim Daneliuk <tundra at tundraware.com>
wrote:

>Bengt Richter wrote:
>>
>
>Thanks to all who answered!  A fertitle bunch of minds 'round here....
>
>The response that gets it all (despite my fuzzy definition) was mailed
>to me privately.  I deconstructed it (well really, reinvented it from 
>scratch myself) to make sure I understood what was going on. 
>
>And the winner is...
>
>^-?((\d+(\.\d{2})?)|(\.\d{2}))$
>
>To be really fussy, the \. should really be a string constant which
>is conatenated into the remainder substrings so that other delimiters
>could be used when I18Ning the code.
> 
>Just for the record - I knew when I started that a re was a better way to do
>this.  Although I've programmed a lot in other languages, I am still in
>the process of absorbing python, and I had not yet attacked its re
>capabilities so I decided to hand code it.  Now that I look at the re
>implementation, I realize I should have started there because it is so
>much more compact and elegant.
>
>In any case, it was a nice tutorial exercise which made me learn an
>essential piece of python.
>
>I'm writing a complete (small) application which does keyboard I/O,
>saves state to disk, does meaningful arithmetic and so on.  My intention
>is to put it up on the web when done so others can use it to see something
>a bit less trivial than the examples which are common in tutorials.
>
>Once again, many thanks to all who kindly took the time to respond.
>
>------------------------------------------------------------------------------
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()
__________________________________________________________________



More information about the Python-list mailing list