Created
June 27, 2010 11:36
-
-
Save vishnevskiy/454840 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import types | |
class Column(object): | |
def __init__(self, name): | |
self._name = name | |
def __add__(self, other): | |
return {self._name: {'$inc': other}} | |
def __eq__(self, other): | |
return {self._name: other} | |
def in_(self, *vals): | |
return {self._name: {'$in': vals}} | |
class Query(object): | |
def __init__(self, *args, **kwargs): | |
self._spec = {} | |
def filter(self, *args): | |
for arg in args: | |
for k, v in arg.iteritems(): | |
if k in self._spec and type(v) is types.DictType\ | |
and type(self._spec[k]) is types.DictType: | |
self._spec[k].update(v) | |
else: | |
self._spec[k] = v | |
return self | |
def delete(self): | |
print 'deleted %s' % self.__repr__() | |
def __repr__(self): | |
return self._spec.__repr__() | |
class User(object): | |
username = Column('username') | |
friends = Column('friends') | |
Query(User).filter(User.friends.in_(5, 2, 3, 4)).filter(User.username == 'stanislav').delete() | |
# prints | |
# deleted {'username': 'stanislav', 'friends': {'$in': (5, 2, 3, 4)}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment