Skip to content

Commit

Permalink
Make it possible to iterate over all the known documents/records of a…
Browse files Browse the repository at this point in the history
… BaseModel-derived class with persistence settings
  • Loading branch information
JohnVinyard committed Jun 5, 2016
1 parent 0f94ac3 commit 651f44f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
2 changes: 1 addition & 1 deletion featureflow/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '1.11.10'
__version__ = '1.12.10'

from model import BaseModel

Expand Down
19 changes: 12 additions & 7 deletions featureflow/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ def _add_features(cls, features):
except AttributeError:
pass

@staticmethod
def _ensure_persistence_settings(cls):
if not issubclass(cls, PersistenceSettings):
raise NoPersistenceSettingsError(
'The class {cls} is not a PersistenceSettings subclass'
.format(cls=cls.__name__))

def __iter__(cls):
cls._ensure_persistence_settings(cls)
for _id in cls.database:
yield cls(_id)


class NoPersistenceSettingsError(Exception):
"""
Expand All @@ -42,13 +54,6 @@ def __init__(self, _id=None):
if _id:
self._id = _id

@staticmethod
def _ensure_persistence_settings(cls):
if not issubclass(cls, PersistenceSettings):
raise NoPersistenceSettingsError(
'The class {cls} is not a PersistenceSettings subclass'
.format(cls=cls.__name__))

def __getattribute__(self, key):
f = object.__getattribute__(self, key)

Expand Down
16 changes: 16 additions & 0 deletions featureflow/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,19 @@ class MultipleRoots(BaseModel):


class BaseTest(object):
def test_can_iter_over_document_class(self):
class D(BaseModel, self.Settings):
stream = Feature(TextStream, store=True)
words = Feature(Tokenizer, needs=stream, store=False)
count = JSONFeature(WordCount, needs=words, store=True)

D.process(stream='mary')
D.process(stream='humpty')
D.process(stream='cased')

l = list(doc for doc in D)
self.assertEqual(3, len(l))
self.assertTrue(all(isinstance(x, D) for x in l))

def test_can_use_node_that_validates_its_dependency_list(self):
class D1(BaseModel, self.Settings):
Expand Down Expand Up @@ -851,6 +864,9 @@ class Doc(MultipleRoots, self.Settings):
KeyError,
lambda: Doc.process(stream1='mary'))

def test_raises_when_no_settings_base_class_and_iter_called(self):
self.assertRaises(NoPersistenceSettingsError, lambda: list(Document))

def test_get_meaningful_error_when_no_settings_base_class_and_process_is_called(
self):
self.assertRaises(
Expand Down

0 comments on commit 651f44f

Please sign in to comment.