本文整理匯總了Python中bson.json_util.loads方法的典型用法代碼示例。如果您正苦於以下問題:Python json_util.loads方法的具體用法?Python json_util.loads怎麽用?Python json_util.loads使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類bson.json_util
的用法示例。
在下文中一共展示了json_util.loads方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: preparations_data
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def preparations_data(self, data):
"""
Data preparation.
Args:
`data` : data from AMQP.
Raises:
`n6QueueProcessingException` when except processing data.
"""
try:
self.raw = loads(data)
# calculate md5, inplace its fastest
self.headers['meta'].update({
'md5': hashlib.md5(dumps(self.raw, sort_keys=True)).hexdigest()})
except Exception as exc:
LOGGER.error('exception when processing: %r %r %r (%r)',
self.dbm.currdb, self.dbm.currcoll, data, exc)
raise
else:
self.write()
示例2: test_binary
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def test_binary(self):
bin_type_dict = {"bin": Binary(b"\x00\x01\x02\x03\x04")}
md5_type_dict = {
"md5": Binary(b" n7\x18\xaf\t/\xd1\xd1/\x80\xca\xe7q\xcc\xac",
MD5_SUBTYPE)
}
custom_type_dict = {"custom": Binary(b"hello", USER_DEFINED_SUBTYPE)}
self.round_trip(bin_type_dict)
self.round_trip(md5_type_dict)
self.round_trip(custom_type_dict)
json_bin_dump = bsonjs_dumps(md5_type_dict)
# Order should be $binary then $type.
self.assertEqual(
('{ "md5" : { "$binary" : "IG43GK8JL9HRL4DK53HMrA==", '
'"$type" : "05" } }'),
json_bin_dump)
json_bin_dump = bsonjs_dumps(custom_type_dict)
self.assertTrue('"$type" : "80"' in json_bin_dump)
# Check loading invalid binary
self.assertRaises(ValueError, bsonjs.loads,
'{"a": {"$binary": "invalid", "$type": "80"}}')
示例3: test_utils_montyexport
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def test_utils_montyexport(tmp_monty_repo):
database = "dump_db_JSON"
collection = "dump_col_JSON"
with open_repo(tmp_monty_repo):
montyexport(database, collection, JSON_DUMP)
loaded_examples = list()
loaded_exported = list()
with open(JSON_DUMP, "r") as dump:
data = dump.read().strip()
for d, s in zip(data.split("\n"), SERIALIZED.split("\n")):
loaded_exported.append(json_util.loads(d))
loaded_examples.append(json_util.loads(s))
sort = (lambda l: sorted(l, key=lambda i: i["_id"]))
for d, s in zip(sort(loaded_exported), sort(loaded_examples)):
assert d == s
os.remove(JSON_DUMP)
示例4: __prepare_doc
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def __prepare_doc(self, policy):
"""
Prepare Policy object as a document for insertion.
"""
# todo - add dict inheritance
doc = b_json.loads(policy.to_json())
if policy.type == TYPE_STRING_BASED:
for field in self.condition_fields:
compiled_regexes = []
for el in doc[field]:
if policy.start_tag in el and policy.end_tag in el:
compiled = compile_regex(el, policy.start_tag, policy.end_tag).pattern
else:
compiled = el
compiled_regexes.append(compiled)
doc[self.condition_field_compiled_name(field)] = compiled_regexes
doc['_id'] = policy.uid
return doc
示例5: writeToCollection
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def writeToCollection(collection, df, id = None):
jsonStrings = df.to_json(orient='records')
bsonStrings = json_util.loads(jsonStrings)
for string in bsonStrings:
if id is not None:
id_string = ''.join([string[item] for item in id])
string['_id'] = id_string
collection.save(string)
示例6: writeToCollectionExtend
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def writeToCollectionExtend(collection, symbol, df, metadata=None):
jsonStrings = {"_id":symbol, "symbol":symbol, "data":df.to_json(orient='records'), "metadata":metadata}
#bsonStrings = json_util.loads(jsonStrings)
collection.save(jsonStrings)
示例7: test
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def test(self, rss, page, state, result, label):
with patch('n6.collectors.generic.CollectorWithStateMixin.__init__'), \
patch.object(self.COLLECTOR_CLASS, 'config', self.mocked_config, create=True):
instance = self.COLLECTOR_CLASS()
instance._download_retry = Mock(return_value=rss)
instance._download_retry_external = Mock(return_value=page)
instance.load_state = Mock(return_value=state)
if label in ('no_url', 'not_updated_page'):
with self.assertRaises(NoNewDataException):
self.COLLECTOR_CLASS.get_output_data_body(instance)
else:
output_data_body = self.COLLECTOR_CLASS.get_output_data_body(instance)
self.assertDictEqual(loads(output_data_body), result)
示例8: post
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def post(self):
if 'file' in request.files:
f = AttachedFile.from_upload(request.files['file'])
else:
data = loads(request.data)
if 'file' in data:
f = AttachedFile.from_upload(data['file'])
else:
abort(400)
return render({'filename': url_for('api.AttachedFiles:get', id=f.id)})
示例9: multidelete
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def multidelete(self):
data = loads(request.data)
ids = iterify(data['ids'])
for i, inv in enumerate(Investigation.objects(links__id__in=ids)):
inv.modify({
"links__id": id
},
set__links__S__id="local-{}-{}".format(time.time(), i))
self.objectmanager.objects(id__in=ids).delete()
return render({"deleted": ids})
示例10: multiupdate
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def multiupdate(self):
data = loads(request.data)
ids = data['ids']
new_description = data['new']['description']
updated = []
for link in self.objectmanager.objects(id__in=ids):
# link.select_related() #does not work
# must call src and dst to dereference dbrefs and not raise an exception
link.src
link.dst
link.description = new_description
link.save()
updated.append(link.id)
return render({"updated": updated})
示例11: multidelete
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def multidelete(self):
"""Deletes multiple entries from the database
:query [ObjectID] ids: Array of Element IDs
:>json [ObjectID] deleted: Array of Element IDs that were successfully deleted
"""
data = loads(request.data)
ids = iterify(data['ids'])
self.objectmanager.objects(id__in=ids).delete()
return render({"deleted": ids})
示例12: multiupdate
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def multiupdate(self):
"""Updates multiple entries from the database
:query [ObjectID] ids: Array of Element IDs
:query [Object] new: JSON object representing fields to update
:>json [ObjectID] updated: Array of Element IDs that were successfully updated
"""
data = loads(request.data)
ids = iterify(data['ids'])
new_data = data['new']
self.objectmanager.objects(id__in=ids).update(new_data)
return render({
"updated": list(self.objectmanager.objects(ids__in=ids))
})
示例13: remove
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def remove(self, id):
i = get_object_or_404(self.objectmanager, id=id)
data = loads(request.data)
i.remove(iterify(data['links']), iterify(data['nodes']))
return render(i.info())
示例14: bulk_add
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def bulk_add(self, id):
i = get_object_or_404(self.objectmanager, id=id)
data = loads(request.data)
nodes = []
response = {'status': 'ok', 'message': ''}
try:
for node in data['nodes']:
if node['type'] in globals() and issubclass(
globals()[node['type']], Observable):
_type = globals()[node['type']]
try:
n = _type.get_or_create(value=node['value'])
except ObservableValidationError as e:
logging.error((node, e))
continue
if node['new_tags']:
n.tag(node['new_tags'].split(', '))
nodes.append(n)
i.add([], nodes)
except Exception as e:
response = {'status': 'error', 'message': str(e)}
return render(response)
示例15: bsonjs_loads
# 需要導入模塊: from bson import json_util [as 別名]
# 或者: from bson.json_util import loads [as 別名]
def bsonjs_loads(json_str):
"""Provide same API as json_util.loads"""
return to_object(bsonjs.loads(json_str))