本文整理汇总了Python中ujson.load方法的典型用法代码示例。如果您正苦于以下问题:Python ujson.load方法的具体用法?Python ujson.load怎么用?Python ujson.load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ujson
的用法示例。
在下文中一共展示了ujson.load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: load_dataset
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def load_dataset(path):
"""Load json file and store fields separately."""
with open(path) as f:
data = json.load(f)['data']
output = {'qids': [], 'questions': [], 'answers': [],
'contexts': [], 'qid2cid': []}
for article in data:
for paragraph in article['paragraphs']:
output['contexts'].append(paragraph['context'])
for qa in paragraph['qas']:
output['qids'].append(qa['id'])
output['questions'].append(qa['question'])
output['qid2cid'].append(len(output['contexts']) - 1)
if 'answers' in qa:
output['answers'].append(qa['answers'])
return output
示例2: load_answers
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def load_answers(filename):
"""Load the answers only of a SQuAD dataset. Store as qid -> [answers]."""
# Load JSON file
with open(filename) as f:
examples = json.load(f)['data']
ans = {}
for article in examples:
for paragraph in article['paragraphs']:
for qa in paragraph['qas']:
ans[qa['id']] = list(map(lambda x: x['text'], qa['answers']))
return ans
# ------------------------------------------------------------------------------
# Dictionary building
# ------------------------------------------------------------------------------
示例3: configure
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def configure(ctx):
"""
Configure the following application settings:
(1) Default encoding to load files.
(2) HTTP/HTTPS proxy server URI (for url sub-command).
Configurations are written to '~/.sqlitebiter'.
You can remove these settings by deleting '~/.sqlitebiter'.
"""
initialize_logger("{:s} file".format(PROGRAM_NAME), ctx.obj[Context.LOG_LEVEL])
logger.debug("{} configuration file existence: {}".format(PROGRAM_NAME, app_config_mgr.exists))
sys.exit(app_config_mgr.configure())
示例4: __init__
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def __init__(self, config=None):
if isinstance(config, dict) or isinstance(config, OrderedDict):
self.config = config
elif isinstance(config, str):
try:
self.config = json.load(open(config, "r"))
except:
self.config = {}
self.username = self.config.get("username", "data_security_es_45")
self.password = self.config.get("password", "Nb6121ca7ffe3")
es_url = self.config.get("es_url", ['http://zsearch.alipay.com:9999'])
if isinstance(es_url, list):
self.es_url = es_url
else:
self.es_url = [es_url]
self.es = Elasticsearch(self.es_url, http_auth=(self.username, self.password))
示例5: populate_new_fields
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def populate_new_fields(apps: StateApps, schema_editor: DatabaseSchemaEditor) -> None:
# Open the JSON file which contains the data to be used for migration.
MIGRATION_DATA_PATH = os.path.join(os.path.dirname(os.path.dirname(__file__)), "management", "data")
path_to_unified_reactions = os.path.join(MIGRATION_DATA_PATH, "unified_reactions.json")
unified_reactions = ujson.load(open(path_to_unified_reactions))
Reaction = apps.get_model('zerver', 'Reaction')
for reaction in Reaction.objects.all():
reaction.emoji_code = unified_reactions.get(reaction.emoji_name)
if reaction.emoji_code is None:
# If it's not present in the unified_reactions map, it's a realm emoji.
reaction.emoji_code = reaction.emoji_name
if reaction.emoji_name == 'zulip':
# `:zulip:` emoji is a zulip special custom emoji.
reaction.reaction_type = 'zulip_extra_emoji'
else:
reaction.reaction_type = 'realm_emoji'
reaction.save()
示例6: team_view
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def team_view(request: HttpRequest) -> HttpResponse:
if not settings.ZILENCER_ENABLED:
return HttpResponseRedirect('https://zulip.com/team/', status=301)
try:
with open(settings.CONTRIBUTOR_DATA_FILE_PATH) as f:
data = ujson.load(f)
except FileNotFoundError:
data = {'contrib': {}, 'date': "Never ran."}
return TemplateResponse(
request,
'zerver/team.html',
context={
'page_params': {
'contrib': data['contrib'],
},
'date': data['date'],
},
)
示例7: create_language_name_map
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def create_language_name_map(self) -> None:
join = os.path.join
deploy_root = settings.DEPLOY_ROOT
path = join(deploy_root, 'locale', 'language_options.json')
output_path = join(deploy_root, 'locale', 'language_name_map.json')
with open(path) as reader:
languages = ujson.load(reader)
lang_list = []
for lang_info in languages['languages']:
lang_info['name'] = lang_info['name_local']
del lang_info['name_local']
lang_list.append(lang_info)
lang_list.sort(key=lambda lang: lang['name'])
with open(output_path, 'w') as output_file:
ujson.dump({'name_map': lang_list}, output_file, indent=4, sort_keys=True)
output_file.write('\n')
示例8: export_usermessages_batch
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def export_usermessages_batch(input_path: Path, output_path: Path,
consent_message_id: Optional[int]=None) -> None:
"""As part of the system for doing parallel exports, this runs on one
batch of Message objects and adds the corresponding UserMessage
objects. (This is called by the export_usermessage_batch
management command)."""
with open(input_path) as input_file:
output = ujson.load(input_file)
message_ids = [item['id'] for item in output['zerver_message']]
user_profile_ids = set(output['zerver_userprofile_ids'])
del output['zerver_userprofile_ids']
realm = Realm.objects.get(id=output['realm_id'])
del output['realm_id']
output['zerver_usermessage'] = fetch_usermessages(realm, set(message_ids), user_profile_ids,
output_path, consent_message_id)
write_message_export(output_path, output)
os.unlink(input_path)
示例9: read_metric
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def read_metric(path: str) -> List[int]:
"""
Read metric file in JSON or Rdump format.
Return dimensions of entry "inv_metric".
"""
if path.endswith('.json'):
with open(path, 'r') as fd:
metric_dict = json.load(fd)
if 'inv_metric' in metric_dict:
dims = np.asarray(metric_dict['inv_metric'])
return list(dims.shape)
else:
raise ValueError(
'metric file {}, bad or missing'
' entry "inv_metric"'.format(path)
)
else:
dims = list(read_rdump_metric(path))
if dims is None:
raise ValueError(
'metric file {}, bad or missing'
' entry "inv_metric"'.format(path)
)
return dims
示例10: Apps
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def Apps(self, data=None):
self.destroy()
self.create_window(show=False)
self.btngroup = ButtonGroup(self.window, 40, 30, 240, 40, 10)
self.widgets.append(self.btngroup)
for app in uos.listdir('/apps'):
try:
with open('/apps/{}/app.json'.format(app)) as fp:
data = json.load(fp)
except Exception as e:
print(e)
continue
if 'name' in data:
self.btngroup.add(data['name'], self.run_app, data=app)
self.btngroup.end()
self.window.show()
示例11: process_file
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def process_file(filename, config, word_counter=None, char_counter=None):
data = json.load(open(filename, 'r'))
examples = []
eval_examples = {}
outputs = Parallel(n_jobs=12, verbose=10)(delayed(_process_article)(article, config) for article in data)
# outputs = [_process_article(article, config) for article in data]
examples = [e[0] for e in outputs]
for _, e in outputs:
if e is not None:
eval_examples[e['id']] = e
# only count during training
if word_counter is not None and char_counter is not None:
for example in examples:
for token in example['ques_tokens'] + example['context_tokens']:
word_counter[token] += 1
for char in token:
char_counter[char] += 1
random.shuffle(examples)
print("{} questions in total".format(len(examples)))
return examples, eval_examples
示例12: setUpClass
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def setUpClass(self):
t0 = time.time()
# Test data comes from NED
with open(os.path.join(test_dir, 'ned_output.json'), 'r') as f:
self._test_data = json.load(f)
# Set up SFD query object
self._sfd = sfd.SFDQuery()
t1 = time.time()
print('Loaded SFD test data in {:.5f} s.'.format(t1-t0))
示例13: read_as_pickle
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def read_as_pickle(data_path, **kwargs):
'''Submethod to read data as pickle'''
with open(data_path, 'rb') as f:
data = pickle.load(f)
return data
示例14: read_as_plain
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def read_as_plain(data_path, **kwargs):
'''Submethod to read data as plain type'''
open_file = open(data_path, 'r')
ext = get_file_ext(data_path)
if ext == '.json':
data = ujson.load(open_file, **kwargs)
elif ext == '.yml':
data = yaml.load(open_file, **kwargs)
else:
data = open_file.read()
open_file.close()
return data
示例15: load_text
# 需要导入模块: import ujson [as 别名]
# 或者: from ujson import load [as 别名]
def load_text(filename):
"""Load the paragraphs only of a SQuAD dataset. Store as qid -> text."""
# Load JSON file
with open(filename) as f:
examples = json.load(f)['data']
texts = {}
for article in examples:
for paragraph in article['paragraphs']:
for qa in paragraph['qas']:
texts[qa['id']] = paragraph['context']
return texts