本文整理汇总了Python中simplejson.OrderedDict方法的典型用法代码示例。如果您正苦于以下问题:Python simplejson.OrderedDict方法的具体用法?Python simplejson.OrderedDict怎么用?Python simplejson.OrderedDict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类simplejson
的用法示例。
在下文中一共展示了simplejson.OrderedDict方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: import simplejson [as 别名]
# 或者: from simplejson import OrderedDict [as 别名]
def main():
if len(sys.argv) == 1:
infile = sys.stdin
outfile = sys.stdout
elif len(sys.argv) == 2:
infile = open(sys.argv[1], 'r')
outfile = sys.stdout
elif len(sys.argv) == 3:
infile = open(sys.argv[1], 'r')
outfile = open(sys.argv[2], 'w')
else:
raise SystemExit(sys.argv[0] + " [infile [outfile]]")
with infile:
try:
obj = json.load(infile,
object_pairs_hook=json.OrderedDict,
use_decimal=True)
except ValueError:
raise SystemExit(sys.exc_info()[1])
with outfile:
json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True)
outfile.write('\n')
示例2: test_object_pairs_hook
# 需要导入模块: import simplejson [as 别名]
# 或者: from simplejson import OrderedDict [as 别名]
def test_object_pairs_hook(self):
s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
("qrt", 5), ("pad", 6), ("hoy", 7)]
self.assertEqual(json.loads(s), eval(s))
self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p)
self.assertEqual(json.load(StringIO(s),
object_pairs_hook=lambda x: x), p)
od = json.loads(s, object_pairs_hook=OrderedDict)
self.assertEqual(od, OrderedDict(p))
self.assertEqual(type(od), OrderedDict)
# the object_pairs_hook takes priority over the object_hook
self.assertEqual(json.loads(s,
object_pairs_hook=OrderedDict,
object_hook=lambda x: None),
OrderedDict(p))
示例3: main
# 需要导入模块: import simplejson [as 别名]
# 或者: from simplejson import OrderedDict [as 别名]
def main():
if len(sys.argv) == 1:
infile = sys.stdin
outfile = sys.stdout
elif len(sys.argv) == 2:
infile = open(sys.argv[1], 'rb')
outfile = sys.stdout
elif len(sys.argv) == 3:
infile = open(sys.argv[1], 'rb')
outfile = open(sys.argv[2], 'wb')
else:
raise SystemExit(sys.argv[0] + " [infile [outfile]]")
try:
obj = json.load(infile,
object_pairs_hook=json.OrderedDict,
use_decimal=True)
except ValueError, e:
raise SystemExit(e)
示例4: render_notification_block
# 需要导入模块: import simplejson [as 别名]
# 或者: from simplejson import OrderedDict [as 别名]
def render_notification_block(config, env_key, skip_envs=None):
j2 = get_datadog_jinja_environment()
template = j2.get_template('notification_block.j2')
envs = config.env_notifications
if skip_envs:
# copy but maintain ordering
envs = OrderedDict(sorted(config.env_notifications.items()))
for env in skip_envs:
envs.pop(env, None)
return template.render(
env_key=env_key,
catchall_alert_channel=config.catchall_alert_channel,
envs=envs,
start_block=BLOCK.format(env_key=env_key, start_or_end='START'),
end_block=BLOCK.format(env_key=env_key, start_or_end='END'),
)
示例5: main
# 需要导入模块: import simplejson [as 别名]
# 或者: from simplejson import OrderedDict [as 别名]
def main():
if len(sys.argv) == 1:
infile = sys.stdin
outfile = sys.stdout
elif len(sys.argv) == 2:
infile = open(sys.argv[1], 'rb')
outfile = sys.stdout
elif len(sys.argv) == 3:
infile = open(sys.argv[1], 'rb')
outfile = open(sys.argv[2], 'wb')
else:
raise SystemExit(sys.argv[0] + " [infile [outfile]]")
try:
try:
obj = json.load(infile,
object_pairs_hook=json.OrderedDict,
use_decimal=True)
except ValueError, e:
raise SystemExit(e)
json.dump(obj, outfile, sort_keys=True, indent=' ', use_decimal=True)
outfile.write('\n')
示例6: test_object_pairs_hook_with_unicode
# 需要导入模块: import simplejson [as 别名]
# 或者: from simplejson import OrderedDict [as 别名]
def test_object_pairs_hook_with_unicode(self):
s = u'{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
p = [(u"xkd", 1), (u"kcw", 2), (u"art", 3), (u"hxm", 4),
(u"qrt", 5), (u"pad", 6), (u"hoy", 7)]
self.assertEqual(json.loads(s), eval(s))
self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p)
od = json.loads(s, object_pairs_hook=json.OrderedDict)
self.assertEqual(od, json.OrderedDict(p))
self.assertEqual(type(od), json.OrderedDict)
# the object_pairs_hook takes priority over the object_hook
self.assertEqual(json.loads(s,
object_pairs_hook=json.OrderedDict,
object_hook=lambda x: None),
json.OrderedDict(p))
示例7: get_config
# 需要导入模块: import simplejson [as 别名]
# 或者: from simplejson import OrderedDict [as 别名]
def get_config(config_path):
with open(config_path, 'r') as config_file:
config = Config(yaml.safe_load(config_file))
config.env_notifications = OrderedDict(sorted(config.env_notifications.items()))
return config