Skip to content

Commit 9c76ac7

Browse files
committed
Comparison wrapper for Time.
1 parent 826bebb commit 9c76ac7

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

midi.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import binascii
2121
import collections
2222
import numbers
23+
import operator
2324
import copy
2425
import math
2526

@@ -434,6 +435,41 @@ def _node_error(self, attribute):
434435
'Cannot set {attribute} without a time specification.'.format(
435436
attribute=attribute))
436437

438+
def _comparison(self, other, comparison):
439+
if isinstance(other, Time):
440+
return comparison(self.note, other.note)
441+
elif isinstance(other, collections.Iterable):
442+
if len(other) == 3:
443+
time = Time(specification=self.specification)
444+
time.triple = other
445+
return comparison(self.note, time.note)
446+
elif isinstance(other, numbers.Number):
447+
return comparison(self.note, other)
448+
elif other == None:
449+
if comparison == operator.eq:
450+
return False
451+
elif comparison == operator.ne:
452+
return True
453+
return NotImplemented
454+
455+
def __lt__(self, other):
456+
return self._comparison(other, operator.lt)
457+
458+
def __le__(self, other):
459+
return self._comparison(other, operator.le)
460+
461+
def __eq__(self, other):
462+
return self._comparison(other, operator.eq)
463+
464+
def __ne__(self, other):
465+
return self._comparison(other, operator.ne)
466+
467+
def __ge__(self, other):
468+
return self._comparison(other, operator.ge)
469+
470+
def __gt__(self, other):
471+
return self._comparison(other, operator.gt)
472+
437473
def __repr__(self):
438474
return str(self)
439475

@@ -1319,7 +1355,7 @@ def meta(event):
13191355
def track(event):
13201356
return event.track
13211357
def time(event):
1322-
return event.time.note
1358+
return event.time
13231359
super().sort(key=meta, reverse=reverse)
13241360
super().sort(key=track, reverse=reverse)
13251361
super().sort(key=time, reverse=reverse)

0 commit comments

Comments
 (0)