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