本文整理匯總了Python中bson.json_util.object_hook方法的典型用法代碼示例。如果您正苦於以下問題:Python json_util.object_hook方法的具體用法?Python json_util.object_hook怎麽用?Python json_util.object_hook使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類bson.json_util
的用法示例。
在下文中一共展示了json_util.object_hook方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: query_mongo
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def query_mongo(username, id_string, query=None, hide_deleted=True):
# print("incoming query", query)
qry = query
qry["_deleted_at"] = {'$exists': False}
# query = None
# query = json.loads(query, object_hook=json_util.object_hook)\
# if query else {}
# query = dict_for_mongo(query)
# query[USERFORM_ID] = u'{0}_{1}'.format(username, id_string)
# if hide_deleted:
# query = {"$and": [query, {"_deleted_at": None}]}
# query = {"$and": [query, qry]}
# print(query)
print("cpount", xform_instances.find(qry).count())
print("qry", qry)
return xform_instances.find(qry)
示例2: list_healings
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def list_healings(args):
parser = _base_args("list-healings")
parser.add_argument("-n", "--quantity", default=20, required=False, type=int)
parsed_args = parser.parse_args(args)
result = proxy_request(parsed_args.service, "/admin/healings?quantity=" + str(parsed_args.quantity),
method="GET")
body = result.read().rstrip("\n")
if result.getcode() != 200:
sys.stderr.write("ERROR: " + body + "\n")
sys.exit(1)
try:
healings_list = []
healings_list = json.loads(body, object_hook=json_util.object_hook)
except Exception as e:
sys.stderr.write("ERROR: invalid json response - {}\n".format(e.message))
sys.exit(1)
healings_table = DisplayTable(['Instance', 'Machine', 'Start Time', 'Duration', 'Status'])
_render_healings_list(healings_table, healings_list)
示例3: mongo_aggregate
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def mongo_aggregate(cls, query, pipeline, hide_deleted=True):
"""Perform mongo aggregate queries
query - is a dict which is to be passed to $match, a pipeline operator
pipeline - list of dicts or dict of mongodb pipeline operators,
http://docs.mongodb.org/manual/reference/operator/aggregation-pipeline
"""
if isinstance(query, basestring):
query = json.loads(
query, object_hook=json_util.object_hook) if query else {}
if not (isinstance(pipeline, dict) or isinstance(pipeline, list)):
raise Exception(_(u"Invalid pipeline! %s" % pipeline))
if not isinstance(query, dict):
raise Exception(_(u"Invalid query! %s" % query))
query = dict_for_mongo(query)
if hide_deleted:
# display only active elements
deleted_at_query = {
"$or": [{"_deleted_at": {"$exists": False}},
{"_deleted_at": None}]}
# join existing query with deleted_at_query on an $and
query = {"$and": [query, deleted_at_query]}
k = [{'$match': query}]
if isinstance(pipeline, list):
k.extend(pipeline)
else:
k.append(pipeline)
results = xform_instances.aggregate(k)
return results['result']
示例4: query_mongo_minimal
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def query_mongo_minimal(
cls, query, fields, sort, start=0, limit=DEFAULT_LIMIT,
count=False, hide_deleted=True):
fields_to_select = {cls.USERFORM_ID: 0}
# TODO: give more detailed error messages to 3rd parties
# using the API when json.loads fails
query = json.loads(
query, object_hook=json_util.object_hook) if query else {}
query = dict_for_mongo(query)
if hide_deleted:
# display only active elements
# join existing query with deleted_at_query on an $and
query = {"$and": [query, {"_deleted_at": None}]}
# fields must be a string array i.e. '["name", "age"]'
fields = json.loads(
fields, object_hook=json_util.object_hook) if fields else []
# TODO: current mongo (2.0.4 of this writing)
# cant mix including and excluding fields in a single query
if type(fields) == list and len(fields) > 0:
fields_to_select = dict(
[(_encode_for_mongo(field), 1) for field in fields])
sort = json.loads(
sort, object_hook=json_util.object_hook) if sort else {}
cursor = xform_instances.find(query, fields_to_select)
if count:
return [{"count": cursor.count()}]
if start < 0 or limit < 0:
raise ValueError(_("Invalid start/limit params"))
cursor.skip(start).limit(limit)
if type(sort) == dict and len(sort) == 1:
sort_key = sort.keys()[0]
# TODO: encode sort key if it has dots
sort_dir = int(sort[sort_key]) # -1 for desc, 1 for asc
cursor.sort(_encode_for_mongo(sort_key), sort_dir)
# set batch size
cursor.batch_size = cls.DEFAULT_BATCHSIZE
return cursor
示例5: run_metadata
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def run_metadata(self, run_id, projection=None):
path = self._run_meta_path(run_id)
if not osp.exists(path):
raise strax.RunMetadataNotAvailable(
f"No file at {path}, cannot find run metadata for {run_id}")
with open(path, mode='r') as f:
md = json.loads(f.read(),
object_hook=json_util.object_hook)
md = strax.flatten_run_metadata(md)
if projection is not None:
md = {k: v
for k, v in md.items()
if k in projection}
return md
示例6: decode
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def decode(value):
from bson import json_util
return json_decode(value, encoding='utf-8', object_hook=json_util.object_hook)
示例7: decode
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def decode(self, s):
def object_hook(obj):
return bson_hook(obj)
return simplejson.loads(s, object_hook=self.object_hook)
示例8: _loads
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def _loads(content, fmt=None):
if fmt == 'toml':
return toml.loads(content)
elif fmt == 'json':
return json.loads(content, object_hook=json_util.object_hook)
elif fmt == 'python':
return ast.literal_eval(content)
elif fmt == 'pickle':
return pickle.loads(content)
else:
return content
示例9: parse_oids
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def parse_oids(oids):
if not isinstance(oids, list):
raise Exception("$oids takes an array as input.")
return [bson_object_hook({"$oid": oid}) for oid in oids]
示例10: datetime_parser
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def datetime_parser(dct):
for k, v in dct.items():
if isinstance(v, str):
m = date_regex.findall(v)
if len(m) > 0:
dct[k] = parse(m[0], yearfirst=True)
if "$humanTime" in dct:
return parse_human_time(dct["$humanTime"])
if "$oids" in dct:
return parse_oids(dct["$oids"])
return bson_object_hook(dct)
示例11: query_mongo
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import object_hook [as 別名]
def query_mongo(cls, username, id_string, query, fields, sort, start=0,
limit=DEFAULT_LIMIT, count=False, hide_deleted=True):
fields_to_select = {cls.USERFORM_ID: 0}
# TODO: give more detailed error messages to 3rd parties
# using the API when json.loads fails
if isinstance(query, basestring):
query = json.loads(query, object_hook=json_util.object_hook)
query = query if query else {}
query = dict_for_mongo(query)
query[cls.USERFORM_ID] = u'%s_%s' % (username, id_string)
# check if query contains and _id and if its a valid ObjectID
if '_uuid' in query and ObjectId.is_valid(query['_uuid']):
query['_uuid'] = ObjectId(query['_uuid'])
if hide_deleted:
# display only active elements
# join existing query with deleted_at_query on an $and
query = {"$and": [query, {"_deleted_at": None}]}
# fields must be a string array i.e. '["name", "age"]'
if isinstance(fields, basestring):
fields = json.loads(fields, object_hook=json_util.object_hook)
fields = fields if fields else []
# TODO: current mongo (2.0.4 of this writing)
# cant mix including and excluding fields in a single query
if type(fields) == list and len(fields) > 0:
fields_to_select = dict(
[(_encode_for_mongo(field), 1) for field in fields])
if isinstance(sort, basestring):
sort = json.loads(sort, object_hook=json_util.object_hook)
sort = sort if sort else {}
cursor = xform_instances.find(query, fields_to_select)
if count:
return [{"count": cursor.count()}]
if start < 0 or limit < 0:
raise ValueError(_("Invalid start/limit params"))
cursor.skip(start).limit(limit)
if type(sort) == dict and len(sort) == 1:
sort_key = sort.keys()[0]
# TODO: encode sort key if it has dots
sort_dir = int(sort[sort_key]) # -1 for desc, 1 for asc
cursor.sort(_encode_for_mongo(sort_key), sort_dir)
# set batch size
cursor.batch_size = cls.DEFAULT_BATCHSIZE
return cursor