def test_json_loads_unicode(): try: anyjson.force_implementation("json") except ImportError: return assert "foo" in anyjson.loads(u'{"foo": "bar"}')
def test_forced_deserialization(): for name in modnames: try: anyjson.force_implementation(name) except ImportError: continue # module can't be tested, try next assert anyjson.deserialize("[1,2,3]") == [1,2,3] assert anyjson.loads("[1,2,3]") == [1,2,3]
def test_forced_serialization(): for name in modnames: try: anyjson.force_implementation(name) except ImportError: continue # module can't be tested, try next assert anyjson.serialize([1,2,3]).replace(" ", "") == "[1,2,3]" assert anyjson.dumps([1,2,3]).replace(" ", "") == "[1,2,3]"
def test_exceptions(): for name in modnames: try: anyjson.force_implementation(name) except ImportError: continue # module can't be tested, try next assert_raises(TypeError, anyjson.serialize, [object()]) assert_raises(TypeError, anyjson.dumps, [object()]) assert_raises(ValueError, anyjson.deserialize, "[") assert_raises(ValueError, anyjson.loads, "[")
from __future__ import absolute_import, print_function import anyjson import os import sys from kombu.exceptions import VersionMismatch # avoid json implementation inconsistencies. try: import json # noqa anyjson.force_implementation('json') except ImportError: anyjson.force_implementation('simplejson') def find_distribution_modules(name=__name__, file=__file__): current_dist_depth = len(name.split('.')) - 1 current_dist = os.path.join(os.path.dirname(file), *([os.pardir] * current_dist_depth)) abs = os.path.abspath(current_dist) dist_name = os.path.basename(abs) for dirpath, dirnames, filenames in os.walk(abs): package = (dist_name + dirpath[len(abs):]).replace('/', '.') if '__init__.py' in filenames: yield package for filename in filenames: if filename.endswith('.py') and filename != '__init__.py': yield '.'.join([package, filename])[:-3]
pass return json.dumps(to_primitive(value)) def loads(s): return json.loads(s) try: import anyjson except ImportError: pass else: anyjson._modules.append(("engine.utils", "dumps", TypeError, "loads", ValueError)) anyjson.force_implementation("engine.utils") _semaphores = {} class _NoopContextManager(object): def __enter__(self): pass def __exit__(self, exc_type, exc_val, exc_tb): pass def synchronized(name, external=False): """Synchronization decorator.
else: if any(test(value) for test in _nasty_type_tests): return six.text_type(value) return value except TypeError: # Class objects are tricky since they may define something like # __iter__ defined but it isn't callable as list(). return six.text_type(value) def dumps(value, default=to_primitive, **kwargs): return json.dumps(value, default=default, **kwargs) def loads(s): return json.loads(s) def load(s): return json.load(s) try: import anyjson except ImportError: pass else: anyjson._modules.append((__name__, 'dumps', TypeError, 'loads', ValueError, 'load')) anyjson.force_implementation(__name__)
pass return json.dumps(to_primitive(value)) def loads(s): return json.loads(s) try: import anyjson except ImportError: pass else: anyjson._modules.append( ("nova.utils", "dumps", TypeError, "loads", ValueError)) anyjson.force_implementation("nova.utils") _semaphores = {} class _NoopContextManager(object): def __enter__(self): pass def __exit__(self, exc_type, exc_val, exc_tb): pass def synchronized(name, external=False): """Synchronization decorator.
import argparse import os import socket import anyjson import yaml try: import ujson ujson.loads("{}") if anyjson._modules[0][0] != 'ujson': anyjson._modules.insert( 0, ("ujson", "dumps", TypeError, "loads", ValueError, "load")) anyjson.force_implementation('ujson') except ImportError: ujson = None CONFIG_PATH = '/etc/moira/config.yml' REDIS_HOST = "localhost" REDIS_PORT = 6379 LOG_DIRECTORY = "stdout" HTTP_PORT = 8081 HTTP_ADDR = '' GRAPHITE = [] GRAPHITE_PREFIX = 'DevOps.moira' GRAPHITE_INTERVAL = 10 NODATA_CHECK_INTERVAL = 60 CHECK_INTERVAL = 5 CHECK_LOCK_TTL = 30 STOP_CHECKING_INTERVAL = 30
#!/usr/bin/env python import time import anyjson #import demjson if __name__ == "__main__": print "Reading JSON data file into memory:", start_time = time.time() with open("data.json", "r") as data_file: data = data_file.read() finish_time = time.time() print "Read %0.2fMiB in %0.2f seconds." % (len(data) / 1024.0 / 1024.0, finish_time - start_time) anyjson.force_implementation('yajl') python_obj = anyjson.deserialize(data) json_string = anyjson.serialize(python_obj) # print "Creating Python object from JSON data using demjson:", # start_time = time.time() # python_obj = demjson.decode(data) # finish_time = time.time() # print "Finished in %0.2f seconds." % (finish_time - start_time) # print "Creating JSON data from Python object using demjson:", # start_time = time.time() # json_string = demjson.encode(python_obj, strict=True) # finish_time = time.time() # print "Finished in %0.2f seconds." % (finish_time - start_time) # del data # print "Beginning main benchmarks at 10 iterations per operation:\n" for implementation in anyjson._modules: name = implementation[0]
from __future__ import absolute_import import anyjson import os import sys from kombu.exceptions import VersionMismatch # avoid json implementation inconsistencies. try: import json # noqa anyjson.force_implementation("json") except ImportError: anyjson.force_implementation("simplejson") def find_distribution_modules(name=__name__, file=__file__): current_dist_depth = len(name.split(".")) - 1 current_dist = os.path.join(os.path.dirname(file), *([os.pardir] * current_dist_depth)) abs = os.path.abspath(current_dist) dist_name = os.path.basename(abs) for dirpath, dirnames, filenames in os.walk(abs): package = (dist_name + dirpath[len(abs):]).replace("/", ".") if "__init__.py" in filenames: yield package for filename in filenames: if filename.endswith(".py") and filename != "__init__.py": yield ".".join([package, filename])[:-3]
#!/usr/bin/env python # -*- coding: utf-8 -*- import anyjson from anyjson import force_implementation, dumps, loads force_implementation('jsonlib2') import json from collections import OrderedDict ordered_decoder = json.JSONDecoder(object_pairs_hook=OrderedDict) class OrderedJSONDecoder(json.JSONDecoder): """ As `JSONDecoder`, but passing `collections.OrderedDict` for `object_pairs_hook` by default. """ def __init__(self, encoding=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=OrderedDict ): super(OrderedJSONDecoder, self).__init__( encoding=encoding, object_hook=object_hook,
def patch(): anyjson._modules.append(('ujson', 'dumps', TypeError, 'loads', ValueError)) anyjson.force_implementation('ujson')
#!/usr/bin/env python import time import anyjson #import demjson if __name__ == "__main__": print "Reading JSON data file into memory:", start_time = time.time() with open("data.json", "r") as data_file: data = data_file.read() finish_time = time.time() print "Read %0.2fMiB in %0.2f seconds." % (len(data) / 1024.0 / 1024.0, finish_time - start_time) anyjson.force_implementation('yajl') python_obj = anyjson.deserialize(data) json_string = anyjson.serialize(python_obj) # print "Creating Python object from JSON data using demjson:", # start_time = time.time() # python_obj = demjson.decode(data) # finish_time = time.time() # print "Finished in %0.2f seconds." % (finish_time - start_time) # print "Creating JSON data from Python object using demjson:", # start_time = time.time() # json_string = demjson.encode(python_obj, strict=True) # finish_time = time.time() # print "Finished in %0.2f seconds." % (finish_time - start_time) # del data # print "Beginning main benchmarks at 10 iterations per operation:\n" for implementation in anyjson._modules: name = implementation[0] # if name == 'cjson':
from __future__ import absolute_import import anyjson import atexit import os import sys from kombu.exceptions import VersionMismatch # avoid json implementation inconsistencies. try: import json # noqa anyjson.force_implementation('json') except ImportError: anyjson.force_implementation('simplejson') def teardown(): # Workaround for multiprocessing bug where logging # is attempted after global already collected at shutdown. cancelled = set() try: import multiprocessing.util cancelled.add(multiprocessing.util._exit_function) except (AttributeError, ImportError): pass try: atexit._exithandlers[:] = [ e for e in atexit._exithandlers if e[0] not in cancelled ]
import yaml try: import ujson ujson.loads("{}") if anyjson._modules[0][0] != 'ujson': anyjson._modules.insert( 0, ("ujson", "dumps", TypeError, "loads", ValueError, "load")) anyjson.force_implementation('ujson') except ImportError: ujson = None CONFIG_PATH = '/etc/moira/config.yml' REDIS_HOST = "localhost" REDIS_PORT = 6379 LOG_DIRECTORY = "stdout" HTTP_PORT = 8081 HTTP_ADDR = '' GRAPHITE = [] GRAPHITE_PREFIX = 'DevOps.moira' GRAPHITE_INTERVAL = 10 NODATA_CHECK_INTERVAL = 60 CHECK_INTERVAL = 5 CHECK_LOCK_TTL = 30
import os import sys import time os.environ['NOSETPS'] = 'yes' import anyjson JSONIMP = os.environ.get('JSONIMP') if JSONIMP: anyjson.force_implementation(JSONIMP) print('anyjson implementation: %r' % (anyjson.implementation.name, )) from celery import Celery, group DEFAULT_ITS = 40000 BROKER_TRANSPORT = os.environ.get('BROKER', 'librabbitmq') if hasattr(sys, 'pypy_version_info'): BROKER_TRANSPORT = 'amqplib' celery = Celery(__name__) celery.conf.update(BROKER_TRANSPORT=BROKER_TRANSPORT, BROKER_POOL_LIMIT=10, CELERYD_POOL='solo', CELERY_PREFETCH_MULTIPLIER=0, CELERY_DISABLE_RATE_LIMITS=True, CELERY_DEFAULT_DELIVERY_MODE=1, CELERY_QUEUES = { 'bench.worker': { 'exchange': 'bench.worker',
pass return json.dumps(to_primitive(value)) def loads(s): return json.loads(s) try: import anyjson except ImportError: pass else: anyjson._modules.append(("nova.utils", "dumps", TypeError, "loads", ValueError)) anyjson.force_implementation("nova.utils") _semaphores = {} class _NoopContextManager(object): def __enter__(self): pass def __exit__(self, exc_type, exc_val, exc_tb): pass def synchronized(name, external=False): """Synchronization decorator.
from __future__ import print_function import os import sys import time os.environ.update( NOSETPS='yes', USE_FAST_LOCALS='yes', ) import anyjson JSONIMP = os.environ.get('JSONIMP') if JSONIMP: anyjson.force_implementation(JSONIMP) print('anyjson implementation: {0!r}'.format(anyjson.implementation.name)) from celery import Celery, group from celery.five import range DEFAULT_ITS = 40000 BROKER_TRANSPORT = os.environ.get('BROKER', 'librabbitmq') if hasattr(sys, 'pypy_version_info'): BROKER_TRANSPORT = 'pyamqp' app = Celery('bench_worker') app.conf.update( BROKER_TRANSPORT=BROKER_TRANSPORT, BROKER_POOL_LIMIT=10,
pass return json.dumps(to_primitive(value)) def loads(s): return json.loads(s) try: import anyjson except ImportError: pass else: anyjson._modules.append( ("engine.utils", "dumps", TypeError, "loads", ValueError)) anyjson.force_implementation("engine.utils") _semaphores = {} class _NoopContextManager(object): def __enter__(self): pass def __exit__(self, exc_type, exc_val, exc_tb): pass def synchronized(name, external=False): """Synchronization decorator.
#!/usr/bin/env python # -*- coding: utf-8 -*- import anyjson from anyjson import force_implementation, dumps, loads force_implementation('jsonlib2') import json from collections import OrderedDict ordered_decoder = json.JSONDecoder(object_pairs_hook=OrderedDict) class OrderedJSONDecoder(json.JSONDecoder): """ As `JSONDecoder`, but passing `collections.OrderedDict` for `object_pairs_hook` by default. """ def __init__(self, encoding=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, strict=True, object_pairs_hook=OrderedDict): super(OrderedJSONDecoder, self).__init__(encoding=encoding, object_hook=object_hook, parse_float=parse_float, parse_int=parse_int, parse_constant=parse_constant,