Class RowBuilder
object --+
|
RowBuilder
- Known Subclasses:
-
This class provides the logic required to transform a sequence of of
tokens parsed out of the delimited text of a Stream element into a
sequence of row objects for insertion into a Table element. An instance
of this class is initialized with a Python class to be instantiated to
form row objects, and an iterable providing the names of the row class'
attributes to which tokens will be assigned in order.
Example:
>>> import tokenizer
>>> class Row(object):
... pass
...
>>> t = tokenizer.Tokenizer(u",")
>>> t.set_types([int, float])
>>> rows = tokenizer.RowBuilder(Row, ["time", "snr"])
>>> l = list(rows.append(t.append(u"10,6.8,15,29.1,")))
>>> l[0].snr
6.8
>>> l[1].time
15
Hint: If you wish to try to save memory by "interning" the
values in certain columns of a table, try sub-classing this and replacing
the append method with your own.
Example:
>>> strings = {}
>>> OldRowBuilder = RowBuilder
>>> class MyRowBuilder(RowBuilder):
... def append(self, tokens):
... for row in OldRowBuilder.append(self, tokens):
... if hasattr(row, "channel"):
... row.channel = strings.setdefault(row.channel, row.channel)
... yield row
...
>>> RowBuilder = MyRowBuilder
This will significantly slow down table parsing, but for now this
approach of allowing individual applications to override row construction
on an as-desired basis seems to be the best way to implement the feature
without adding a great deal of complexity. Note that when initialized
the RowBuilder class is passed the interns argument which is an iterable
of attribute names that should be considered for interning. These names
come from hints stored in the Table class definitions, and will have been
filtered so that only names corresponding to columns actually in the
table will be listed.
|
__init__(...)
x.__init__(...) initializes x; see help(type(x)) for signature |
|
|
|
|
a new object with type S, a subtype of T
|
|
|
append(...)
Append a sequence of tokens to the row builder, returning an iterator
for generating a sequence of new row instances. |
|
|
the next value, or raise StopIteration
|
|
Inherited from object :
__delattr__ ,
__format__ ,
__getattribute__ ,
__hash__ ,
__reduce__ ,
__reduce_ex__ ,
__repr__ ,
__setattr__ ,
__sizeof__ ,
__str__ ,
__subclasshook__
|
|
attributes
in-order tuple of attribute names
|
|
i
current attribute index
|
|
interns
names of attributes suitable for interning
|
|
row
current row object
|
|
rowtype
row class
|
Inherited from object :
__class__
|
__init__(...)
(Constructor)
|
|
x.__init__(...) initializes x; see help(type(x)) for signature
- Overrides:
object.__init__
|
- Returns: a new object with type S, a subtype of T
- Overrides:
object.__new__
|
Append a sequence of tokens to the row builder, returning an iterator
for generating a sequence of new row instances. The tokens argument
should be an iterable, producing a sequence of token objects. If fewer
tokens are yielded from the iterable than are required to construct a
complete row, then the row is stored in its partially-populated state and
its construction will continue upon the next invocation. Note that it is
possible that a call to this method will yield no new rows at all.
Example:
>>> for row in rows.append([10, 6.8, 15, 29.1]):
... print row.snr
...
|