[gt-users] python iterators

Brent Pedersen bpederse at gmail.com
Tue Oct 6 03:50:57 CEST 2009


On Thu, Oct 1, 2009 at 7:38 AM, Brent Pedersen <bpederse at gmail.com> wrote:
> On Thu, Oct 1, 2009 at 2:24 AM, Sascha Steinbiss
> <steinbiss at zbh.uni-hamburg.de> wrote:
>> Hi Brent,
>>
>> thanks for your efforts!
>>
>>> here's another option that just makes the feature itself
>>> iterable--which actually is sorta nice:
>>>
>>> for sub_feature in feature:
>>>       print sub_feature
>>>
>>> i've attached and even smaller patch to allow that. i think it's a
>>> good way to keep the backwards compatiblity,
>>> and i actually like it better.
>>
>> So do I. Is there any way to make both traversal strategies (DFS and
>> direct children only) possible? There are iterators for both of them,
>> and your patch only uses the DFS one. The __iter__() function can not
>> take kwargs parameters, or can it? I tried the following:
>>
>> def __iter__(self, **kwargs):
>>  if kwargs and kwargs["method"] == 'direct':
>>    it = FeatureNodeIteratorDirect(self)
>>  else:
>>    it = FeatureNodeIteratorDepthFirst(self)
>>  f = it.next()
>>  while f is not None:
>>    yield f
>>    f = it.next()
>>
>> but when trying this:
>>
>> types = []
>> for f in fn(method="direct"):
>>  types.append(f.type)
>>
>> I got a "TypeError: 'FeatureNode' object is not callable". Any ideas?
>
> good idea.
> when you do fn(), you're accessing the __call__() method, so you could
> override that instead, so then it'd be:
> for f in fn(): ...
>
> otherwise, it could still use __iter__() and just decide where to use
> Direct or DepthFirst iterator
> by an attribute:
>
> if self.depth_first:
>    it = FeatureNodeIteratorDepthFirst()
> else:
>    it = FeatureNodeIteratorDirect()
>
>
> i prefer the latter.
>
> -b
>
>>
>>
>> Sascha
>>
>> --
>> Sascha Steinbiss
>> Center for Bioinformatics
>> University of Hamburg
>> Bundesstr. 43
>> 20146 Hamburg
>> Germany
>>
>> Email:  steinbiss at zbh.uni-hamburg.de
>> URL:    http://www.zbh.uni-hamburg.de/steinbiss
>> Phone:  +49 (40) 42838 7322
>> FAX:    +49 (40) 42838 7312
>>
>> _______________________________________________
>> gt-users mailing list
>> gt-users at genometools.org
>> http://genometools.org/mailman/listinfo/gt-users
>>
>

here's a patch with both __call__ and __iter__ where call can take
direct as a kwarg and iter defaults to depth-first.
-b
-------------- next part --------------
A non-text attachment was scrubbed...
Name: feature_iter.diff
Type: text/x-diff
Size: 3058 bytes
Desc: not available
Url : http://genometools.org/pipermail/gt-users/attachments/20091005/ce7a635d/attachment.bin 


More information about the gt-users mailing list