def test_pickle_by_solving(discretization): d = discretization d2 = loads(dumps(d)) d.disable_caching() d2.disable_caching() for mu in d.parameter_space.sample_randomly(3, seed=234): assert np.all(d.solve(mu).almost_equal(d2.solve(mu)))
def test_pickle_by_evaluation(function): f = function f2 = loads(dumps(f)) mus = parameters_of_type(f.parameter_type, 47) for arg in function_argument(f, 10, 42): mu = next(mus) assert np.all(f.evaluate(arg, mu) == f2.evaluate(arg, mu))
def test_pickle_by_solving(model): m = model m2 = loads(dumps(m)) m.disable_caching() m2.disable_caching() for mu in m.parameter_space.sample_randomly(3, seed=234): assert np.all(almost_equal(m.solve(mu), m2.solve(mu)))
def assert_picklable_without_dumps_function(o): def dumps_function_raise(function): raise PicklingError('Cannot pickle function {}'.format(function)) old_code = dumps_function.__code__ dumps_function.__code__ = dumps_function_raise.__code__ try: s = dumps(o) o2 = loads(s) assert_is_equal(o, o2) finally: dumps_function.__code__ = old_code
def assert_picklable_without_dumps_function(o): def dumps_function_raise(function): raise PicklingError("Cannot pickle function {}".format(function)) old_code = dumps_function.func_code dumps_function.func_code = dumps_function_raise.func_code try: s = dumps(o) o2 = loads(s) assert_is_equal(o, o2) finally: dumps_function.func_code = old_code
def assert_picklable_without_dumps_function(o): def dumps_function_raise(function): raise PicklingError(f'Cannot pickle function {function}') old_code = dumps_function.__code__ dumps_function.__code__ = dumps_function_raise.__code__ try: s = dumps(o) o2 = loads(s) assert_is_equal(o, o2) finally: dumps_function.__code__ = old_code
def event_loop(): """Launches an MPI-based event loop. Events can be sent by either calling :func:`call` on rank 0 to execute an arbitrary method on all ranks or by calling :func:`quit` to exit the loop. """ assert not rank0 while True: try: method, args, kwargs = loads(comm.bcast(None)) if method == 'QUIT': break else: method(*args, **kwargs) except: import traceback print("Caught exception on MPI rank {}:".format(rank)) traceback.print_exception(*sys.exc_info())
def event_loop(): """Launches an MPI-based event loop. Events can be sent by either calling :func:`call` on rank 0 to execute an arbitrary method on all ranks or by calling :func:`quit` to exit the loop. """ assert not rank0 while True: try: method, args, kwargs = loads(comm.bcast(None)) if method == 'QUIT': break else: method(*args, **kwargs) except: import traceback print(f"Caught exception on MPI rank {rank}:") traceback.print_exception(*sys.exc_info())
def assert_picklable(o): s = dumps(o) o2 = loads(s) assert_is_equal(o, o2)
def __setstate__(self, state): if state.pop('_picklable'): state['_mapping'] = loads(state['_mapping']) else: state['_mapping'] = loads_function(state['_mapping']) self.__dict__.update(state)