Skip to content

Idea: Rewrite Completer in pythonic iterable style #7

@insolor

Description

@insolor

https://github.com/pymorphy2-fork/DAWG-Python/blob/b7fc89348029b65b7ef3eb55b34bc4c230fdde12/dawg_python/wrapper.py#L97C13-L97C13

In fact, Completer is an implementation of the Iterator pattern. So, I think it makes sense to rewrite it as a Python iterator. Or, add .keys() and .values() methods, which would return iterators.

E.g. rewrite from:

class Completer:
   ...

   def init(self, ...):
       ...

   def next(self):
       ...

to

class Completer:
   ...

   def __iter__(self):
       ...

   def __next__(self):
      ...

So that it can be used as:

res = [item.decode("utf8") for item in completer]

instead of

res = []

while completer.next():
    key = completer.key.decode('utf8')
    res.append(key)

return res

Or

yield from (item.decode("utf8") for item in completer)

instead of

while completer.next():
    yield completer.key.decode('utf8')

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions