port sorting by time to python 3 while retaining compatibility with 2.7

This commit is contained in:
Axel Kohlmeyer
2022-08-09 16:51:00 -04:00
parent 111caac960
commit c8b6b052fc

View File

@ -194,6 +194,21 @@ import numpy as np
try: from DEFAULTS import PIZZA_GUNZIP try: from DEFAULTS import PIZZA_GUNZIP
except: PIZZA_GUNZIP = "gunzip" except: PIZZA_GUNZIP = "gunzip"
# --------------------------------------------------------------------
# wrapper to convert old style comparision function to key function
def cmp2key(oldcmp):
class keycmp:
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return oldcmp(self.obj,other.obj) < 0
def __gt__(self, other):
return oldcmp(self.obj,other.obj) > 0
def __eq__(self, other):
return oldcmp(self.obj,other.obj) == 0
return keycmp
# Class definition # Class definition
class dump: class dump:
@ -255,7 +270,7 @@ class dump:
# sort entries by timestep, cull duplicates # sort entries by timestep, cull duplicates
self.snaps.sort(self.compare_time) self.snaps.sort(key=cmp2key(self.compare_time))
self.cull() self.cull()
self.nsnaps = len(self.snaps) self.nsnaps = len(self.snaps)
print("read %d snapshots" % self.nsnaps) print("read %d snapshots" % self.nsnaps)