Browse Source

Fix interval comparisons for Python 3

tags/nilmdb-2.0.0
Jim Paris 2 years ago
parent
commit
5da7e6558e
3 changed files with 29 additions and 9 deletions
  1. +13
    -5
      nilmdb/server/interval.pyx
  2. +13
    -3
      nilmdb/utils/interval.py
  3. +3
    -1
      tests/test_interval.py

+ 13
- 5
nilmdb/server/interval.pyx View File

@@ -60,11 +60,19 @@ cdef class Interval:
return ("[" + timestamp_to_string(self.start) +
" -> " + timestamp_to_string(self.end) + ")")

def __cmp__(self, Interval other):
"""Compare two intervals. If non-equal, order by start then end"""
def cmp(a, b):
return (a > b) - (a < b)
return cmp(self.start, other.start) or cmp(self.end, other.end)
# Compare two intervals. If non-equal, order by start then end
def __lt__(self, Interval other):
return (self.start, self.end) < (other.start, other.end)
def __gt__(self, Interval other):
return (self.start, self.end) > (other.start, other.end)
def __le__(self, Interval other):
return (self.start, self.end) <= (other.start, other.end)
def __le__(self, Interval other):
return (self.start, self.end) >= (other.start, other.end)
def __eq__(self, Interval other):
return (self.start, self.end) == (other.start, other.end)
def __ne__(self, Interval other):
return (self.start, self.end) != (other.start, other.end)

cpdef intersects(self, Interval other):
"""Return True if two Interval objects intersect"""


+ 13
- 3
nilmdb/utils/interval.py View File

@@ -39,9 +39,19 @@ class Interval:
return ("[ " + nilmdb.utils.time.timestamp_to_human(self.start) +
" -> " + nilmdb.utils.time.timestamp_to_human(self.end) + " ]")

def __cmp__(self, other):
"""Compare two intervals. If non-equal, order by start then end"""
return cmp(self.start, other.start) or cmp(self.end, other.end)
# Compare two intervals. If non-equal, order by start then end
def __lt__(self, other):
return (self.start, self.end) < (other.start, other.end)
def __gt__(self, other):
return (self.start, self.end) > (other.start, other.end)
def __le__(self, other):
return (self.start, self.end) <= (other.start, other.end)
def __le__(self, other):
return (self.start, self.end) >= (other.start, other.end)
def __eq__(self, other):
return (self.start, self.end) == (other.start, other.end)
def __ne__(self, other):
return (self.start, self.end) != (other.start, other.end)

def intersects(self, other):
"""Return True if two Interval objects intersect"""


+ 3
- 1
tests/test_interval.py View File

@@ -428,4 +428,6 @@ class TestIntervalSpeed:
speed / (j*math.log(j))) # should be constant
speeds[j] = speed
yappi.stop()
yappi.print_stats(sort_type=yappi.SORTTYPE_TTOT, limit=10)
stats = yappi.get_func_stats()
stats.sort("ttot")
stats.print_all()

Loading…
Cancel
Save