(a) learn how to do comparisons, (b) make ES units comparable

--HG--
extra : convert_revision : svn%3Aeff31bef-be4a-0410-a8fe-e47997df2690/trunk%4039
issue20
tibs 2008-08-23 21:15:29 +00:00
rodzic 1750c74d44
commit 02f72c180d
2 zmienionych plików z 40 dodań i 0 usunięć

Wyświetl plik

@ -57,6 +57,21 @@ And close it:
>>> stream.mode is None
True
ES units can be compared for equality (but not order):
>>> es_unit_list[0] == es_unit_list[0]
1
>>> es_unit_list[0] == es_unit_list[1]
0
>>> es_unit_list[0] != es_unit_list[1]
1
>>> es_unit_list[0] != es_unit_list[0]
0
>>> es_unit_list[0] < es_unit_list[1]
Traceback (most recent call last):
...
TypeError: ESUnit only supports == and != comparisons
And write another file...
>>> import tempfile

Wyświetl plik

@ -117,6 +117,27 @@ cdef extern from 'es_fns.h':
class TSToolsException(Exception):
pass
cdef same_ES_unit(ES_unit_p this, ES_unit_p that):
"""Two ES units do not need to be at the same place to be the same.
"""
if this.data_len != that.data_len:
return False
for 0 <= ii < this.data_len:
if this.data[ii] != that.data[ii]:
return False
return True
cdef class ESUnit # Forward declaration
cdef object compare_ESUnits(ESUnit this, ESUnit that, int op):
"""op is 2 for ==, 3 for !=, other values not allowed.
"""
if op == 2: # ==
return same_ES_unit(this.unit, that.unit)
elif op == 3: # !=
return not same_ES_unit(this.unit, that.unit)
else:
#return NotImplemented
raise TypeError, 'ESUnit only supports == and != comparisons'
cdef class ESUnit:
"""A Python class representing an ES unit.
@ -159,6 +180,9 @@ cdef class ESUnit:
else:
self.unit = unit
def __richcmp__(self,other,op):
return compare_ESUnits(self,other,op)
# Is this the simplest way? Since it appears that a class method
# doesn't want to take a non-Python item as an argument...
cdef _next_ESUnit(ES_p stream, filename):
@ -259,6 +283,7 @@ cdef class ESStream:
"""
return _next_ESUnit(self.stream,self.filename)
def close(self):
if self.python_file:
self.python_file.close()