本文整理汇总了Python中orjson.dumps方法的典型用法代码示例。如果您正苦于以下问题:Python orjson.dumps方法的具体用法?Python orjson.dumps怎么用?Python orjson.dumps使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类orjson
的用法示例。
在下文中一共展示了orjson.dumps方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: encode
# 需要导入模块: import orjson [as 别名]
# 或者: from orjson import dumps [as 别名]
def encode(self, o):
o = self.encode_object(o)
if self.sort_keys is True or orjson is None:
return super(ObjectJSONEncoder, self).encode(o).encode("utf8")
else:
return orjson.dumps(o)
示例2: render
# 需要导入模块: import orjson [as 别名]
# 或者: from orjson import dumps [as 别名]
def render(self, content: Any) -> bytes:
return orjson.dumps(content)
示例3: render
# 需要导入模块: import orjson [as 别名]
# 或者: from orjson import dumps [as 别名]
def render(self, content: Any) -> bytes:
assert orjson is not None, "orjson must be installed to use ORJSONResponse"
return orjson.dumps(content)
示例4: dumps
# 需要导入模块: import orjson [as 别名]
# 或者: from orjson import dumps [as 别名]
def dumps(value: Any) -> str:
return orjson.dumps(value).decode()
示例5: send_bad_request_response
# 需要导入模块: import orjson [as 别名]
# 或者: from orjson import dumps [as 别名]
def send_bad_request_response(
error_dict: Dict[str, Any],
has_content_length: bool,
headers: Optional[Sequence[Header]] = None,
content_type: ContentType = ContentType.APPLICATION_JSON,
) -> ASGICallableResults:
body = orjson.dumps({'error': error_dict})
content_length = len(body) if has_content_length else None
return (
RESPONSES_MAP[content_type](
content_length, HTTPStatus.BAD_REQUEST, make_asgi_headers(headers)
),
body,
)
示例6: dumps
# 需要导入模块: import orjson [as 别名]
# 或者: from orjson import dumps [as 别名]
def dumps(obj: Any, *args, **kw) -> bytes:
kw["default"] = _orjson_default
return _orjson.dumps(obj, *args, **kw)
示例7: dumps_str
# 需要导入模块: import orjson [as 别名]
# 或者: from orjson import dumps [as 别名]
def dumps_str(obj: Any, *args, **kw) -> str:
return dumps(obj, *args, **kw).decode("utf-8")
示例8: write
# 需要导入模块: import orjson [as 别名]
# 或者: from orjson import dumps [as 别名]
def write(self, elems):
for elem in elems:
self.fh.write(orjson.dumps(elem) + b"\n")
示例9: orjson_dumps
# 需要导入模块: import orjson [as 别名]
# 或者: from orjson import dumps [as 别名]
def orjson_dumps(v, *, default):
# orjson.dumps returns bytes, to match standard json.dumps we need to decode
return orjson.dumps(v, default=default).decode()
示例10: classify_bug
# 需要导入模块: import orjson [as 别名]
# 或者: from orjson import dumps [as 别名]
def classify_bug(model_name: str, bug_ids: Iterable[int], bugzilla_token: str) -> str:
from bugbug_http.app import JobInfo
# This should be called in a process worker so it should be safe to set
# the token here
bug_ids_set = set(map(int, bug_ids))
bugzilla.set_token(bugzilla_token)
bugs = bugzilla.get(bug_ids)
missing_bugs = bug_ids_set.difference(bugs.keys())
for bug_id in missing_bugs:
job = JobInfo(classify_bug, model_name, bug_id)
# TODO: Find a better error format
setkey(job.result_key, orjson.dumps({"available": False}))
if not bugs:
return "NOK"
model = MODEL_CACHE.get(model_name)
if not model:
LOGGER.info("Missing model %r, aborting" % model_name)
return "NOK"
model_extra_data = model.get_extra_data()
# TODO: Classify could choke on a single bug which could make the whole
# job to fails. What should we do here?
probs = model.classify(list(bugs.values()), True)
indexes = probs.argmax(axis=-1)
suggestions = model.le.inverse_transform(indexes)
probs_list = probs.tolist()
indexes_list = indexes.tolist()
suggestions_list = suggestions.tolist()
for i, bug_id in enumerate(bugs.keys()):
data = {
"prob": probs_list[i],
"index": indexes_list[i],
"class": suggestions_list[i],
"extra_data": model_extra_data,
}
job = JobInfo(classify_bug, model_name, bug_id)
setkey(job.result_key, orjson.dumps(data))
# Save the bug last change
setkey(job.change_time_key, bugs[bug_id]["last_change_time"].encode())
return "OK"