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