本文整理匯總了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"