本文整理汇总了Python中json.encoder.FLOAT_REPR属性的典型用法代码示例。如果您正苦于以下问题:Python encoder.FLOAT_REPR属性的具体用法?Python encoder.FLOAT_REPR怎么用?Python encoder.FLOAT_REPR使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类json.encoder
的用法示例。
在下文中一共展示了encoder.FLOAT_REPR属性的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Dump
# 需要导入模块: from json import encoder [as 别名]
# 或者: from json.encoder import FLOAT_REPR [as 别名]
def Dump(obj, fid, float_digits=-1, **params):
"""Wrapper of json.dump that allows specifying the float precision used.
Args:
obj: The object to dump.
fid: The file id to write to.
float_digits: The number of digits of precision when writing floats out.
**params: Additional parameters to pass to json.dumps.
"""
original_encoder = encoder.FLOAT_REPR
if float_digits >= 0:
encoder.FLOAT_REPR = lambda o: format(o, '.%df' % float_digits)
try:
json.dump(obj, fid, **params)
finally:
encoder.FLOAT_REPR = original_encoder
示例2: Dumps
# 需要导入模块: from json import encoder [as 别名]
# 或者: from json.encoder import FLOAT_REPR [as 别名]
def Dumps(obj, float_digits=-1, **params):
"""Wrapper of json.dumps that allows specifying the float precision used.
Args:
obj: The object to dump.
float_digits: The number of digits of precision when writing floats out.
**params: Additional parameters to pass to json.dumps.
Returns:
output: JSON string representation of obj.
"""
original_encoder = encoder.FLOAT_REPR
original_c_make_encoder = encoder.c_make_encoder
if float_digits >= 0:
encoder.FLOAT_REPR = lambda o: format(o, '.%df' % float_digits)
encoder.c_make_encoder = None
try:
output = json.dumps(obj, **params)
finally:
encoder.FLOAT_REPR = original_encoder
encoder.c_make_encoder = original_c_make_encoder
return output
示例3: language_eval
# 需要导入模块: from json import encoder [as 别名]
# 或者: from json.encoder import FLOAT_REPR [as 别名]
def language_eval(dataset, preds, model_id, split):
import sys
sys.path.append("coco-caption")
annFile = 'coco-caption/annotations/captions_val2014.json'
from pycocotools.coco import COCO
from pycocoevalcap.eval import COCOEvalCap
encoder.FLOAT_REPR = lambda o: format(o, '.3f')
if not os.path.isdir('eval_results'):
os.mkdir('eval_results')
cache_path = os.path.join('eval_results/', model_id + '_' + split + '.json')
coco = COCO(annFile)
valids = coco.getImgIds()
# filter results to only those in MSCOCO validation set (will be about a third)
preds_filt = [p for p in preds if p['image_id'] in valids]
print('using %d/%d predictions' % (len(preds_filt), len(preds)))
json.dump(preds_filt, open(cache_path, 'w')) # serialize to temporary json file. Sigh, COCO API...
cocoRes = coco.loadRes(cache_path)
cocoEval = COCOEvalCap(coco, cocoRes)
cocoEval.params['image_id'] = cocoRes.getImgIds()
cocoEval.evaluate()
# create output dictionary
out = {}
for metric, score in cocoEval.eval.items():
out[metric] = score
imgToEval = cocoEval.imgToEval
for p in preds_filt:
image_id, caption = p['image_id'], p['caption']
imgToEval[image_id]['caption'] = caption
with open(cache_path, 'w') as outfile:
json.dump({'overall': out, 'imgToEval': imgToEval}, outfile)
return out
示例4: __init__
# 需要导入模块: from json import encoder [as 别名]
# 或者: from json.encoder import FLOAT_REPR [as 别名]
def __init__(self, request, response, corpus_db, bow_db, lda_db):
super(GroupInBox, self).__init__(request, response)
JsonEncoder.FLOAT_REPR = lambda number : format(number, '.4g')
self.corpusDB = corpus_db
self.bowDB = bow_db
self.ldaDB = lda_db
self.bow = bow_db.db
self.db = lda_db.db
################################################################################
示例5: __init__
# 需要导入模块: from json import encoder [as 别名]
# 或者: from json.encoder import FLOAT_REPR [as 别名]
def __init__(self, request, response, corpus_db, bow_db, lda_db):
super(ScatterPlot1, self).__init__(request, response)
JsonEncoder.FLOAT_REPR = lambda number : format(number, '.4g')
self.corpusDB = corpus_db
self.bowDB = bow_db
self.ldaDB = lda_db
self.bow = bow_db.db
self.db = lda_db.db