annotate Demo/comparisons/sortingtest.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents 97d7cd365dd5
children
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 sys
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
28
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 def main():
28789
015d891f3873 Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents: 28788
diff changeset
30 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
31 def makekey(item, prog=prog):
28789
015d891f3873 Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents: 28788
diff changeset
32 match = prog.match(item)
015d891f3873 Modernize the code a bit:
Andrew M. Kuchling <amk@amk.ca>
parents: 28788
diff changeset
33 if match:
56830
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 28789
diff changeset
34 var, num = match.groups()
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 28789
diff changeset
35 return int(num), var
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 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
37 # 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
38 return 0, item
56830
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 28789
diff changeset
39 for line in sys.stdin:
97d7cd365dd5 Merged revisions 73206,73232,73299,73683,74020,74185,74544,74643,74647,74817,74838-74839,74865,74946,75402,75459,75604,75696 via svnmerge from
Georg Brandl <georg@python.org>
parents: 28789
diff changeset
40 items = sorted(makekey(item) for item in 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
41 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
42 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
43 print
2651
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
84e2260db179 commit -- why not...
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 main()