annotate numerics/sort.py @ 150:8a1fe454c98a

STUB
author Jeff Hammel <k0scist@gmail.com>
date Mon, 13 Apr 2015 10:17:47 -0700
parents 12649a88545c
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
134
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
1 #!/usr/bin/env python
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
3
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
4 """
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
5 sort columned data
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
6 """
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
7
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
8 # module globals
135
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
9 __all__ = ['sort_arg', 'Sorter']
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
10
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
11 def sort_arg(string):
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
12 """converter appropriate for command line argument conversion"""
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
13
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
14 forward = True
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
15 if string.startswith('-'):
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
16 forward = False
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
17 string = string[1:]
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
18 return (int(string), forward)
12649a88545c stubbing: hooking this up to command line
Jeff Hammel <k0scist@gmail.com>
parents: 134
diff changeset
19
134
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
20
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
21 class Sorter(object):
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
22 """
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
23 sorter for columned data
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
24 """
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
25
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
26 def __init__(self, *indices):
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
27 """
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
28 indices -- 2-tuple of (index, forward)
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
29 where forward should be True or False
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
30 """
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
31 self.indices = indices
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
32
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
33 def __call__(self, rows):
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
34 return sorted(rows, key=self.key)
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
35
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
36 def key(self, row):
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
37 retval = []
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
38 for index, forward in self.indices:
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
39 value = row[index]
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
40 if not forward:
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
41 value = -value
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
42 retval.append(value)
b6242f916cef stub; needs a test
Jeff Hammel <k0scist@gmail.com>
parents:
diff changeset
43 return tuple(retval)