adewes / blitzdb

Blitz is a document-oriented database for Python that is backend-agnostic. It comes with a flat-file database for JSON documents and provides MongoDB-like querying capabilities.
http://blitzdb.readthedocs.org
MIT License
330 stars 37 forks source link

Filter expressions example from README fails for file backend #58

Closed kylewm closed 9 years ago

kylewm commented 9 years ago
from blitzdb import Document
from blitzdb.backends.file import Backend

class Movie(Document):
    pass

backend = Backend('_test')
Movie({'name': 'The Godfather', 'year': 1972}).save(backend)
Movie({'name': 'Goodfellas', 'year': 1990}).save(backend)
Movie({'name': 'Star Wars', 'year': 1977}).save(backend)
backend.commit()

movies = backend.filter(Movie, {'year': lambda year: year >= 1970 and year <= 1979})

gives

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    'year': lambda year: year >= 1970 and year <= 1979,
  File "/home/kmahan/projects/blitzdb/blitzdb/backends/file/backend.py", line 586, in filter
    query_set = compiled_query(query_function)
  File "/home/kmahan/projects/blitzdb/blitzdb/backends/file/queries.py", line 38, in _get
    return query_function(key, expression)
  File "/home/kmahan/projects/blitzdb/blitzdb/backends/file/backend.py", line 569, in query_function
    indexes[key].get_keys_for(expression)
  File "/home/kmahan/projects/blitzdb/blitzdb/backends/file/queryset.py", line 42, in __init__
    self.keys = list(keys)
TypeError: 'bool' object is not iterable

The problem seems to be that Index.get_keys_for has value=lambda year: year>=1970..., and calls value(self) on it, but changing the get_keys_for function seemed to break assumptions for a bunch of other query styles.

adewes commented 9 years ago

Thanks for reporting this @kylewm , I'll fix it and add a unit test for it!