Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
for test
# coding=utf-8
__author__ = 'shiheng'
from functools import partial
class Bangumi(object):
u'''
Bangumi的一个查询
'''
def __init__(self, dic):
self.obj = dic
def __getattr__(self, item):
if '__' in item:
return getattr(self.get, item)
else:
return partial(_Filter, key=item, j=[], obj=self.obj)
class _Filter(object):
def __init__(self, value, key, j, obj):
self._filter = j + [(key, value)]
self._obj = obj
def __str__(self):
return "_Filter: \n" + "\n".join(map(lambda x: "key: " + str(x[0]) + "\tvalue: "+ str(x[1]),self._filter))
def __getattr__(self, item):
if item == "get":
return _Callable(obj=self._obj, f=self._filter, m="GET")
else:
return partial(_Filter, key=item, j=self._filter, obj=self._obj)
class _Callable(object):
def __init__(self, obj, f, m):
self._obj = obj
self._path = f
self._method = m
def __call__(self):
if self._method in METHOD_MAP.keys():
return METHOD_MAP[self._method](
obj=self._obj,
f=self._path)
def find_in_json(f, obj):
def filter_interval(m, key):
if key in m.keys():
return judge(m[key])
else:
return False
result = obj
for p in f:
key = p[0]
judge = p[1]
result = filter(partial(filter_interval, key=key), result)
return result
def gt(x):
return lambda y: y > x
def lt(x):
return lambda y: y < x
def eq(x):
return lambda y: x == y
def geq(x):
return lambda y: gt(x)(y) or eq(x)(y)
def leq(x):
return lambda y: lt(x)(y) or eq(x)(y)
def contain(x):
return lambda y: x in y
METHOD_MAP = {'GET': find_in_json}
if __name__ == "__main__":
a = [
{'aa': 12, 'heh': "abcde"},
{'aa': 14, 'heh': "opi"},
{'aa': 16},
{'aa': 10, 'heh': "abcde"},
]
k = Bangumi(a)
print k.heh(contain("abc")).aa(leq(12)).get()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.