annotate Demo/comparisons/sortingtest.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 015d891f3873
children 97d7cd365dd5 7d16b42fcdbd
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4573
73758db2f464 /usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents: 2651
diff changeset
1 #! /usr/bin/env python
2651
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # 2) Sorting Test
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
4 #
2651
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # Sort an input file that consists of lines like this
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
6 #
2651
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 # var1=23 other=14 ditto=23 fred=2
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
8 #
2651
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # such that each output line is sorted WRT to the number. Order
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # of output lines does not change. Resolve collisions using the
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 # variable name. e.g.
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
12 #
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
13 # fred=2 other=14 ditto=23 var1=23
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
14 #
2651
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 # Lines may be up to several kilobytes in length and contain
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 # zillions of variables.
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
17
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # This implementation:
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # - Reads stdin, writes stdout
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 # - Uses any amount of whitespace to separate fields
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 # - Allows signed numbers
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 # - Treats illegally formatted fields as field=0
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 # - Outputs the sorted fields with exactly one space between them
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 # - Handles blank input lines correctly
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
28789
015d891f3873 Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents: 28788
diff changeset
26 import re
2651
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 import string
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 import sys
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 def main():
28789
015d891f3873 Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents: 28788
diff changeset
31 prog = re.compile('^(.*)=([-+]?[0-9]+)')
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
32 def makekey(item, prog=prog):
28789
015d891f3873 Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents: 28788
diff changeset
33 match = prog.match(item)
015d891f3873 Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents: 28788
diff changeset
34 if match:
015d891f3873 Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents: 28788
diff changeset
35 var, num = match.group(1, 2)
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
36 return string.atoi(num), var
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
37 else:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
38 # Bad input -- pretend it's a var with value 0
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
39 return 0, item
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
40 while 1:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
41 line = sys.stdin.readline()
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
42 if not line:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
43 break
28789
015d891f3873 Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents: 28788
diff changeset
44 items = line.split()
28788
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
45 items = map(makekey, items)
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
46 items.sort()
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
47 for num, var in items:
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
48 print "%s=%s" % (var, num),
558689b6d7f0 Run these demo scripts through reindent.py to give them 4-space indents. I've verified that their output is unchanged.
Andrew M. Kuchling <amk@amk.ca>
parents: 4573
diff changeset
49 print
2651
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 main()