diff options
author | Andrew M. Kuchling <amk@amk.ca> | 2003-04-24 17:17:56 (GMT) |
---|---|---|
committer | Andrew M. Kuchling <amk@amk.ca> | 2003-04-24 17:17:56 (GMT) |
commit | b7878d09e5d360da4eda65cdf6f51376c234af3d (patch) | |
tree | dd3a0d7952f019eb961ae9a12d15a4ec3fd62ec8 /Demo/comparisons | |
parent | 946c53ed7ff53f38792ac35e5da21de3e0a48ef2 (diff) | |
download | cpython-b7878d09e5d360da4eda65cdf6f51376c234af3d.zip cpython-b7878d09e5d360da4eda65cdf6f51376c234af3d.tar.gz cpython-b7878d09e5d360da4eda65cdf6f51376c234af3d.tar.bz2 |
Modernize the code a bit:
use re module
use .split() string method
Doesn't use 'for line in sys.stdin'; that ends up changing its interactive
behaviour.
Diffstat (limited to 'Demo/comparisons')
-rwxr-xr-x | Demo/comparisons/sortingtest.py | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/Demo/comparisons/sortingtest.py b/Demo/comparisons/sortingtest.py index 8fe2bbb..cabf626 100755 --- a/Demo/comparisons/sortingtest.py +++ b/Demo/comparisons/sortingtest.py @@ -23,15 +23,16 @@ # - Outputs the sorted fields with exactly one space between them # - Handles blank input lines correctly -import regex +import re import string import sys def main(): - prog = regex.compile('^\(.*\)=\([-+]?[0-9]+\)') + prog = re.compile('^(.*)=([-+]?[0-9]+)') def makekey(item, prog=prog): - if prog.match(item) >= 0: - var, num = prog.group(1, 2) + match = prog.match(item) + if match: + var, num = match.group(1, 2) return string.atoi(num), var else: # Bad input -- pretend it's a var with value 0 @@ -40,7 +41,7 @@ def main(): line = sys.stdin.readline() if not line: break - items = string.split(line) + items = line.split() items = map(makekey, items) items.sort() for num, var in items: |