A bit more progress -- iterating over ES units (but can't do anything with them).

--HG--
extra : convert_revision : svn%3Aeff31bef-be4a-0410-a8fe-e47997df2690/trunk%4031
issue20
tibs 2008-08-18 21:43:53 +00:00
rodzic 354d0d09b9
commit 6eb70d4409
2 zmienionych plików z 82 dodań i 1 usunięć

Wyświetl plik

@ -17,6 +17,20 @@ The filename is available as a "readonly" value:
>>> stream.filename
'/Users/tibs/sw/tstools/data/aladdin.es'
We should be able to iterate over its ES units:
>>> count = 0
>>> for unit in stream:
... count += 1
... unit.show()
... if count > 5:
... break
Unfortunately, the *result* of that printout (from ``unit.show()``) goes to C
``stdout``, so the doctest mechanism doesn't get to test it. On the other
hand, that means it also doesn't care what the specifics of the data output
are, which is good if a different ES file is used...
// Local Variables:
// tab-width: 8

Wyświetl plik

@ -31,6 +31,9 @@
cdef extern from "stdio.h":
ctypedef struct FILE:
int _fileno
cdef enum:
EOF = -1
cdef FILE *stdout
cdef extern from 'es_defns.h':
# The reader for an ES file
@ -54,7 +57,61 @@ cdef extern from 'es_fns.h':
# Is this the best thing to do?
class TSToolsException(Exception):
pass
cdef class ESUnit:
"""A Python class representing an ES unit.
"""
cdef ES_unit_p unit
# It appears to be recommended to make __cinit__ expand to take more
# arguments (if __init__ ever gains them), since both get the same
# things passed to them. Hmm, normally I'd trust myself, but let's
# try the recommended route
def __cinit__(self, *args,**kwargs):
pass
def __init__(self):
pass
def show(self):
report_ES_unit(stdout, self.unit)
#def _set_unit(self, ES_unit_p unit):
# if self.unit:
# raise TSToolsException,'ESUnit already has an ES unit associated'
# else:
# self.unit = unit
def __dealloc__(self):
free_ES_unit(&self.unit)
def __repr__(self):
return 'ES unit %d'%self.count
cdef __set_es_unit(self, ES_unit_p unit):
if self.unit == NULL:
raise ValueError,'ES unit already defined'
else:
self.unit = unit
# 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):
cdef ES_unit_p unit
retval = find_and_build_next_ES_unit(stream, &unit)
if retval == EOF:
raise StopIteration
elif retval != 0:
raise TSToolsException,'Error getting next ES unit from file %s'%filename
cdef ESUnit u
u = ESUnit()
u.unit = unit
#u._set_unit(unit)
return u
cdef class ESStream:
"""A Python class representing an ES stream, readable from a file.
"""
@ -78,3 +135,13 @@ cdef class ESStream:
def __dealloc__(self):
close_elementary_stream(&self.stream)
def __iter__(self):
return self
# For Pyrex classes, we define a __next__ instead of a next method
# in order to form our iterator
def __next__(self):
"""Our iterator interface retrieves the ES units from the stream.
"""
return _next_ESUnit(self.stream,self.filename)