def __init__(self): import pydocs self.__dict__ = { __fields_types__: {}, __fields_docs__: pydocs.Fields() }
def __docs__(): """ create Mongodb parable expresion :return: """ import pydocs return pydocs.Fields()
def __filters__(): """ create Mongodb filterable expression :return: """ import pydocs return pydocs.Fields(None, True)
def switch(*args, **kwargs): """ $switch: { branches: [{ case: <expression>, then: <expression> },{ case: <expression>, then: <expression> },..],default: <expression>} :param args: :param kwargs: :return: """ import pydocs branches = [] default = __get_field_expr__(args[args.__len__() - 1]) # default = __get_field_expr__(args[args.__len__() - 1]) # list_of_args = [x for x in args if args.index(x) < args.__len__() - 1] # for item in list_of_args: # if isinstance(item,pydocs.Fields): # branches.append(item.to_mongodb()) # else: # branches.append(item) m = args.__len__() for i in pyrange(0, m - 1, 1): item = args[i] if isinstance(item, pydocs.Fields): branches.append(item.to_mongodb()) else: branches.append(item) # branches.append(__get_field_expr__(getattr(args[i],"__tree__"))) expr = {"$switch": {"branches": branches, "default": default}} ret = pydocs.Fields() setattr(ret, "__tree__", expr) return ret
def __getattr__(self, field): import pydocs ret = pydocs.Fields(field) if hasattr(self.__origin__, field): org = getattr(self.__origin__, field) if isinstance(org, list) and org.__len__() > 0: ret.__dict__.update({"__origin__": org[0]}) else: ret.__dict__.update({"__origin__": org}) return ret
def concat(*args, **kwargs): import pydocs expr = {"$concat": []} for item in args: if isinstance(item, pydocs.Fields): expr["$concat"].append(__get_field_expr__(item)) else: expr["$concat"].append(item) ret = pydocs.Fields() setattr(ret, "__tree__", expr) return ret
def case(test, result): """ { case: <expression>, then: <expression> } :param test: :param result: :return: """ import pydocs expr = { "case": __get_field_expr__(test), "then": __get_field_expr__(result) } ret = pydocs.Fields() setattr(ret, "__tree__", expr) return ret
def iif(test, true_case, flase_case): """ { $cond: { if: <boolean-expression>, then: <true-case>, else: <false-case-> } } :param test: :param true_case: :param flase_case: :return: """ import pydocs expr = { "$cond": { "if": __get_field_expr__(test), "then": __get_field_expr__(true_case), "else": __get_field_expr__(flase_case) } } ret = pydocs.Fields() setattr(ret, "__tree__", expr) return ret
def __getattr__(self, field): import pydocs ret = pydocs.Fields(field) org = getattr(self.__origin__,field) my_docs= self.__dict__.get("__document__",self) if isinstance(org,list) and org.__len__()>0: ret.__dict__.update ({ "__origin__": org[0], "__parent__":self, "__document__":my_docs }) else: ret.__dict__.update({ "__origin__":org, "__parent__": self, "__document__": my_docs }) ret.__dict__.update({ "__type__":type(self.__origin__).__dict__.get(ret.__name__,None), "__field_name__":field }) return ret
def __create__(expr): import pydocs ret = pydocs.Fields() setattr(ret, "__tree__", expr) return ret
import compilers import pyquery import pymongo from pymongo import MongoClient import pyfuncs import pydocs Fields = pydocs.Fields() # print pyfuncs.toDouble(X.name) # fields=pydoc.Fields() # x=pyfuncs.cmp(fields.amount,fields.name)==0 # print isinstance(x,pydoc.Fields) # # c=x.__owner__ # print x.__tree__ cnn = MongoClient(host="localhost", port=27017) db = cnn.get_database("hrm") db.authenticate(name="root", password="******") qr = pyquery.query(db, "test.coll001") qr = qr.where(pyfuncs.regex(Fields.fx, "^312313$")) # qr.project({ # Fields.Users.username:1, # Fields.Users.fullName:pyfuncs.concat(Fields.Users.firstName, " ",Fields.Users.lastname) # }) # qr=qr+2 # #.set(x=1,y=2) # import pprint # items=list(qr.objects) import pprint x = list(qr.objects)