Skip to content

Instantly share code, notes, and snippets.

@kra3
Created September 19, 2011 19:42
Show Gist options
  • Save kra3/1227375 to your computer and use it in GitHub Desktop.
Save kra3/1227375 to your computer and use it in GitHub Desktop.
final solution
from datetime import datetime
import xml.dom.minidom as m
def cmp_to_key(col, covert_to_proper_datatype):
class K(object):
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return covert_to_proper_datatype(self.obj[col]) < covert_to_proper_datatype(other.obj[col])
return K
mapping_functions = {
'str_lower' : lambda x: x.lower(),
'date_mdY' : lambda x: datetime.strptime(x, "%m/%d/%Y"),
'ssn' : lambda x: int(x.replace('-', ''))
'int' : lambda x: int(x)
'dom' : lambda x: int(m.parseString(x).childNodes[0].childNodes[0].nodeValue)
}
mapping_tbl = []
mapping_tbl.insert(0, mapping_functions['dom'])
mapping_tbl.insert(1, mapping_functions['str_lower'])
mapping_tbl.insert(2, mapping_functions['str_lower'])
mapping_tbl.insert(3, mapping_functions['int'])
mapping_tbl.insert(4, mapping_functions['date_mdY'])
mapping_tbl.insert(5, mapping_functions['ssn'])
mapping_tbl.insert(6, mapping_functions['date_mdY'])
mapping_tbl.insert(7, mapping_functions['date_mdY'])
mapping_tbl.insert(8, mapping_functions['str_lower'])
mapping_tbl.insert(9, mapping_functions['str_lower'])
search_data = sorted(search_data, key=cmp_to_key(column_to_sort, mapping_tbl[column_to_sort]), reverse = (sort_dir != "asc"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment