def addMethods(self): # Add the auxiliary function specs to this Generator's namespace for auxfnname in self.funcspec._pyauxfns: fninfo = self.funcspec._pyauxfns[auxfnname] if not hasattr(self, fninfo[1]): # user-defined auxiliary functions # (built-ins are provided explicitly) if self._solver: fnstr = fninfo[0].replace(self._solver.name, "ds._solver") ## self._funcreg[self._solver.name] = self._solver else: fnstr = fninfo[0] try: exec(fnstr) except: print("Error in supplied auxiliary function code") self._funcreg[fninfo[1]] = ("self", fnstr) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self)) # user auxiliary function interface wrapper try: uafi_code = self.funcspec._user_auxfn_interface[auxfnname] try: exec(uafi_code) except: print("Error in auxiliary function wrapper") raise setattr(self.auxfns, auxfnname, six.create_bound_method(locals()[auxfnname], self.auxfns)) self._funcreg[auxfnname] = ("", uafi_code) except KeyError: # not a user-defined aux fn pass if self.funcspec.targetlang == "python": # Add the spec function to this Generator's namespace fninfo = self.funcspec.spec if self._solver: fnstr = fninfo[0].replace(self._solver.name, "ds._solver") else: fnstr = fninfo[0] try: exec(fnstr) except: print("Error in supplied functional specification code") raise self._funcreg[fninfo[1]] = ("self", fnstr) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self)) # Add the auxiliary spec function (if present) to this # Generator's namespace if self.funcspec.auxspec != "": fninfo = self.funcspec.auxspec if self._solver: fnstr = fninfo[0].replace(self._solver.name, "ds._solver") else: fnstr = fninfo[0] try: exec(fnstr) except: print("Error in supplied auxiliary variable code") raise self._funcreg[fninfo[1]] = ("self", fnstr) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self))
def addMethods(self): """Add Python-specific functions to this object's methods.""" # Add the auxiliary function specs to this Generator's namespace for auxfnname in self.funcspec._pyauxfns: fninfo = self.funcspec._pyauxfns[auxfnname] if not hasattr(self, fninfo[1]): # user-defined auxiliary functions # (built-ins are provided explicitly) try: exec(fninfo[0]) except: print('Error in supplied auxiliary function code') self._funcreg[fninfo[1]] = ('self', fninfo[0]) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self)) # user auxiliary function interface wrapper try: uafi_code = self.funcspec._user_auxfn_interface[auxfnname] try: exec(uafi_code) except: print('Error in auxiliary function wrapper') raise setattr( self.auxfns, auxfnname, six.create_bound_method(locals()[auxfnname], self.auxfns)) self._funcreg[auxfnname] = ('', uafi_code) except KeyError: # not a user-defined aux fn pass # Add the spec function to this Generator's namespace if # target language is python (otherwise integrator exposes it anyway) if self.funcspec.targetlang == 'python': fninfo = self.funcspec.spec try: exec(fninfo[0]) except: print('Error in supplied functional specification code') raise self._funcreg[fninfo[1]] = ('self', fninfo[0]) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self)) # Add the auxiliary spec function (if present) to this # Generator's namespace if self.funcspec.auxspec != '': fninfo = self.funcspec.auxspec try: exec(fninfo[0]) except: print('Error in supplied auxiliary variable code') raise self._funcreg[fninfo[1]] = ('self', fninfo[0]) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self))
def cypher_prop(self): # noinspection PyUnresolvedReferences from py2neo.neo4j import CypherQuery cypher_ns = SimpleNamespace() cypher_ns.stream = six.create_bound_method( lambda s, q, **ps: CypherQuery(s, q).stream(**ps), self.graph_db) cypher_ns.execute = six.create_bound_method( lambda s, q, **ps: CypherQuery(s, q).execute(**ps), self.graph_db) return cypher_ns
def __init__(self, in_attrs, client): """Construct a resource :param in_attrs: A dictionary of attributes, usually obtained from a JSON response. :param client: an instance of gocardless.Client """ attrs = in_attrs.copy() self._raw_attrs = attrs.copy() self.id = attrs["id"] self.client = client if "sub_resource_uris" in attrs: #For each subresource_uri create a method which grabs data #from the URI and uses it to instantiate the relevant class #and return it. for name, uri in six.iteritems(attrs.pop("sub_resource_uris")): path = re.sub(".*/api/v1", "", uri) sub_klass = self._get_klass_from_name(name) def create_get_resource_func(the_path, the_klass): # In python functions close over their environment so in # order to create the correct closure we need a function # creator, see # http://stackoverflow.com/questions/233673/ # lexical-closures-in-python/235764#235764 def get_resources(inst, **params): data = inst.client.api_get(the_path, params=params) return [the_klass(attrs, self.client) for attrs in data] return get_resources res_func = create_get_resource_func(path, sub_klass) func_name = "{0}".format(name) res_func.name = func_name setattr(self, func_name, six.create_bound_method(res_func, self)) for fieldname in self.date_fields: val = attrs.pop(fieldname) if val is not None: setattr(self, fieldname, datetime.datetime.strptime(val, "%Y-%m-%dT%H:%M:%SZ")) else: setattr(self, fieldname, None) for fieldname in self.reference_fields: id = attrs.pop(fieldname) def create_get_func(the_klass, the_id): def get_referenced_resource(inst): return the_klass.find_with_client(the_id, self.client) return get_referenced_resource name = fieldname.replace("_id", "") klass = self._get_klass_from_name(name) func = create_get_func(klass, id) setattr(self, name, six.create_bound_method(func, self)) for key, value in six.iteritems(attrs): setattr(self, key, value)
def addMethods(self): """Add Python-specific functions to this object's methods.""" # Add the auxiliary function specs to this Generator's namespace for auxfnname in self.funcspec._pyauxfns: fninfo = self.funcspec._pyauxfns[auxfnname] if not hasattr(self, fninfo[1]): # user-defined auxiliary functions # (built-ins are provided explicitly) try: exec(fninfo[0]) except: print('Error in supplied auxiliary function code') self._funcreg[fninfo[1]] = ('self', fninfo[0]) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self)) # user auxiliary function interface wrapper try: uafi_code = self.funcspec._user_auxfn_interface[auxfnname] try: exec(uafi_code) except: print('Error in auxiliary function wrapper') raise setattr(self.auxfns, auxfnname, six.create_bound_method(locals()[auxfnname], self.auxfns)) self._funcreg[auxfnname] = ('', uafi_code) except KeyError: # not a user-defined aux fn pass # Add the spec function to this Generator's namespace if # target language is python (otherwise integrator exposes it anyway) if self.funcspec.targetlang == 'python': fninfo = self.funcspec.spec try: exec(fninfo[0]) except: print('Error in supplied functional specification code') raise self._funcreg[fninfo[1]] = ('self', fninfo[0]) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self)) # Add the auxiliary spec function (if present) to this # Generator's namespace if self.funcspec.auxspec != '': fninfo = self.funcspec.auxspec try: exec(fninfo[0]) except: print('Error in supplied auxiliary variable code') raise self._funcreg[fninfo[1]] = ('self', fninfo[0]) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self))
def create_multi_node_evaluator(actual_evaluator, communicator): """Create a multi node evaluator from a normal evaluator. Actually this method patches the evaluator to work in multi node environment. This method adds several hidden attributes starting with `_mn_` prefix. Args: actual_evaluator: evaluator to be patched (e.g., ``chainer.training.extensions.Evaluator``) communicator: ChainerMN communicator Returns: The multi-node patched ``actual_evaluator``. .. note:: After patched, original evaluator does not work correctly in non-MPI environment. """ actual_evaluator._mn_original_evaluate = actual_evaluator.evaluate actual_evaluator._mn_communicator = communicator def new_evaluate(self): local_mean_dict = self._mn_original_evaluate() # ChainerX support: # We need convert chainerx ndarray to Native array because # (1) allreduce_obj is used to compute global mean values, since # a simple allreduce operation cannot be applied in evaluation. # (2) allreduce_obj calls mpi4py.allreduce, which pickles the object # (3) chainerx.ndarray preserves CUDA device internally when pickled # (4) An error will occur when an ndarray is unpickled in another # process arrays = list(local_mean_dict.values()) if len(arrays) > 0: array0 = list(local_mean_dict.values())[0] xp = backend.get_array_module(array0) if xp == chx and array0.device.backend.name == 'cuda': # Results of evaluation is fairly small, so # the ndarray is transferred to CPU and allreduce()-ed. # NOTE: Matrices for evaluation are transferred to the # host memory and sent via MPI instead of NCCL. # Although evaluation matrices are small in most cases, # this is a potential performance issue. local_mean_dict = { name: chx.to_numpy(value) for name, value in local_mean_dict.items() } global_mean_dict = { name: self._mn_communicator.allreduce_obj(value) / self._mn_communicator.size for name, value in sorted(local_mean_dict.items()) } return global_mean_dict actual_evaluator.evaluate = six.create_bound_method( new_evaluate, actual_evaluator) return actual_evaluator
def __getattr__(self, name): fun = None if re.match("__.*__", name): # This is a python internal name, skip it raise AttributeError # try it bare llib = getattr(self, "lib") try: fun = getattr(llib, name) except AttributeError: for prefix in self.prefixes: try: # wrap it fun = getattr(llib, prefix + name) break except AttributeError: pass if fun is None: # Return a proxy class to generate a good error on call error_printer = ErrorPrinter(name, self.prefixes) setattr(self, name, error_printer) return error_printer if not callable(fun): # pragma: no cover setattr(self, name, fun) return fun new_fun = self.check_wrap(fun, name) new_method = six.create_bound_method(new_fun, self) # Store the wrapper function into the instance # to prevent a second lookup setattr(self, name, new_method) return new_method
def test_365_calendar(self): f = mock.MagicMock(lbtim=SplittableInt(4, { 'ia': 2, 'ib': 1, 'ic': 0 }), lbyr=2013, lbmon=1, lbdat=1, lbhr=12, lbmin=0, lbsec=0, t1=cftime.datetime(2013, 1, 1, 12, 0, 0), t2=cftime.datetime(2013, 1, 2, 12, 0, 0), spec=PPField3) f.time_unit = six.create_bound_method(PPField3.time_unit, f) f.calendar = cf_units.CALENDAR_365_DAY (factories, references, standard_name, long_name, units, attributes, cell_methods, dim_coords_and_dims, aux_coords_and_dims) = convert(f) def is_t_coord(coord_and_dims): coord, dims = coord_and_dims return coord.standard_name == 'time' coords_and_dims = list(filter(is_t_coord, aux_coords_and_dims)) self.assertEqual(len(coords_and_dims), 1) coord, dims = coords_and_dims[0] self.assertEqual(guess_coord_axis(coord), 'T') self.assertEqual(coord.units.calendar, '365_day')
def test_365_calendar(self): f = mock.MagicMock( lbtim=SplittableInt(4, {"ia": 2, "ib": 1, "ic": 0}), lbyr=2013, lbmon=1, lbdat=1, lbhr=12, lbmin=0, lbsec=0, spec=PPField3, ) f.time_unit = six.create_bound_method(PPField3.time_unit, f) f.calendar = cf_units.CALENDAR_365_DAY ( factories, references, standard_name, long_name, units, attributes, cell_methods, dim_coords_and_dims, aux_coords_and_dims, ) = convert(f) def is_t_coord(coord_and_dims): coord, dims = coord_and_dims return coord.standard_name == "time" coords_and_dims = list(filter(is_t_coord, aux_coords_and_dims)) self.assertEqual(len(coords_and_dims), 1) coord, dims = coords_and_dims[0] self.assertEqual(guess_coord_axis(coord), "T") self.assertEqual(coord.units.calendar, "365_day")
def get_request(self, package=None, perms="", user=None, use_base_url=False, path=None): """ Construct a fake request """ request = MagicMock() request.registry.fallback = self.fallback request.registry.always_show_upstream = self.always_show_upstream request.registry.fallback_url = self.fallback_url request.registry.fallback_base_url = (self.fallback_base_url if use_base_url else None) request.userid = user request.access.can_update_cache = lambda: "c" in perms request.access.has_permission.side_effect = lambda n, p: "r" in perms request.is_logged_in = user is not None request.request_login = six.create_bound_method( _request_login, request) pkgs = [] if package is not None: pkgs.append(package) if path is not None: request.path = path request.db.all.return_value = pkgs return request
def load(Class, path): if not exists(path): raise IOError if path.endswith('.zip'): import geometryIO from crosscompute_table import pandas as pd [ proj4, geometries, field_packs, field_definitions, ] = geometryIO.load(path) # Convert to (longitude, latitude) normalize_geometry = geometryIO.get_transformGeometry( proj4 or geometryIO.proj4LL) geometries = [normalize_geometry(x) for x in geometries] # Convert to (latitude, longitude) geometries = transform_geometries(geometries, drop_z) geometries = transform_geometries(geometries, flip_xy) # Generate table table = pd.DataFrame( field_packs, columns=[x[0] for x in field_definitions]) table['WKT'] = [x.wkt for x in geometries] else: table = super(GeotableType, Class).load(path) # TODO: Consider whether there is a better way to do this table.interpret = create_bound_method(_interpret, table) return table
def create_multi_node_evaluator(actual_evaluator, communicator): """Create a multi node evaluator from a normal evaluator. Actually this method patches the evaluator to work in multi node environment. This method adds several hidden attributes starting with `_mn_` prefix. Args: actual_evaluator: evaluator to be patched (e.g., ``chainer.training.extensions.Evaluator``) communicator: ChainerMN communicator Returns: The multi-node patched ``actual_evaluator``. .. note:: After patched, original evaluator does not work correctly in non-MPI environment. """ actual_evaluator._mn_original_evaluate = actual_evaluator.evaluate actual_evaluator._mn_communicator = communicator def new_evaluate(self): local_mean_dict = self._mn_original_evaluate() global_mean_dict = { name: self._mn_communicator.allreduce_obj(value) / self._mn_communicator.size for name, value in sorted(local_mean_dict.items()) } return global_mean_dict actual_evaluator.evaluate = six.create_bound_method( new_evaluate, actual_evaluator) return actual_evaluator
def get_request( self, package=None, perms="", user=None, use_base_url=False, path=None ): """ Construct a fake request """ request = MagicMock() request.registry.fallback = self.fallback request.registry.always_show_upstream = self.always_show_upstream request.registry.fallback_url = self.fallback_url request.registry.fallback_base_url = ( self.fallback_base_url if use_base_url else None ) request.userid = user request.access.can_update_cache = lambda: "c" in perms request.access.has_permission.side_effect = lambda n, p: "r" in perms request.is_logged_in = user is not None request.request_login = six.create_bound_method(_request_login, request) pkgs = [] if package is not None: pkgs.append(package) if path is not None: request.path = path request.db.all.return_value = pkgs return request
def __get__(self, instance, owner): if not self.registry.get(instance, False): method = create_bound_method(self.method, instance) self.registry[instance] = listener = MethodProxy(method) else: listener = self.registry[instance] return listener
def __init__(self, suite, processes, failfast): # type: (TestSuite, int, bool) -> None super(ParallelTestSuite, self).__init__(suite, processes, failfast) self.subsuites = SubSuiteList(self.subsuites) # type: SubSuiteList # ParallelTestSuite expects this to be a bound method so it can access # __func__ when passing it to multiprocessing. method = partial(run_subsuite, suite.collect_coverage) self.run_subsuite = six.create_bound_method(cast(types.FunctionType, method), self)
def createResource(self, name, lockType): s = six.StringIO("%s:%s" % (name, lockType)) s.seek(0) def switchLockType(self, lockType): raise Exception("I NEVER SWITCH!!!") s.switchLockType = six.create_bound_method(switchLockType, s) return s
def bind_default_handler(self, obj, name): """Bind the default handler method to `obj` as attribute `name`. This could also be implemented as a mixin class. """ assert self.default_handler is not None, '%s has no default handler method, cannot bind' % self.__class__.__name__ setattr(obj, name, six.create_bound_method(self.default_handler, obj)) log.debug("Bound default message handler '%s.%s' to %s", self.__class__.__name__, self.default_handler.__name__, getattr(obj, name))
def test_create_bound_method(): class X(object): pass def f(self): return self x = X() b = six.create_bound_method(f, x) assert isinstance(b, types.MethodType) assert b() is x
def register_extension_method(ext, base, *args, **kwargs): """Register the given extension method as a public attribute of the given base. README: The expected protocol here is that the given extension method is an unbound function. It will be bound to the specified base as a method, and then set as a public attribute of that base. """ bound_method = create_bound_method(ext.plugin, base) setattr(base, ext.name.lstrip('_'), bound_method)
def to_graph(func, recursive=True): new_func = tf.autograph.to_graph( func, recursive=recursive, experimental_optional_features=tf.autograph.experimental.Feature.ALL) # TODO(b/127686409): Remove this. if inspect.ismethod(func): return six.create_bound_method(new_func, func.__self__) return new_func
def createResource(self, name, lockType): s = six.StringIO("%s:%s" % (name, lockType)) s.seek(0) def close(self): raise Exception("I NEVER CLOSE!!!") s.close = six.create_bound_method(close, s) return s
def client(app): client = app.test_client() def get_session_cookie(self, path="/"): return self.cookie_jar._cookies["localhost.local"][path]["session"] client.get_session_cookie = six.create_bound_method( get_session_cookie, client) yield client
def _set_method(target, name, method): """ Set a mocked method onto the target. Target may be either an instance of a Session object of the requests.Session class. First we Bind the method if it's an instance. """ if not isinstance(target, type): method = six.create_bound_method(method, target) setattr(target, name, method)
def __get__(self, instance, owner): if inspect.ismethoddescriptor(self.value) or inspect.isdatadescriptor(self.value): return self.value.__get__(instance, owner) if inspect.isfunction(self.value): if instance is None: return self else: return six.create_bound_method(self.value, instance) else: return self.value
def _init_pyhooks(self): hooks = log.PyHooks.get('DFT', None) if hooks is None: return for label, hook in hooks.items(): name = "{}_hook".format(label) _hook = six.get_method_function(hook) if six.get_method_self(hook) else hook method = six.create_bound_method(_hook, DFT) setattr(self, name, method)
def client(app): client = app.test_client() def get_session_cookie(self): return self.cookie_jar._cookies['localhost.local']['/']['session'] client.get_session_cookie = six.create_bound_method( get_session_cookie, client, ) return client
def client(app): client = app.test_client() def get_session_cookie(self, path='/'): return self.cookie_jar._cookies['localhost.local'][path]['session'] client.get_session_cookie = six.create_bound_method( get_session_cookie, client, ) return client
def test_autospec_on_bound_builtin_function(self): meth = six.create_bound_method(time.ctime, time.time()) self.assertIsInstance(meth(), str) mocked = create_autospec(meth) # no signature, so no spec to check against mocked() mocked.assert_called_once_with() mocked.reset_mock() mocked(4, 5, 6) mocked.assert_called_once_with(4, 5, 6)
def _create_action_method(self, i, action): # PP saving style action. No return value, e.g. "pp.lbft = 3". ns = locals().copy() exec( compile('def _f(self, field, f, pp, grib, cm): %s' % (action, ), '<string>', 'exec'), globals(), ns) # Make it a method of ours. method = six.create_bound_method(ns['_f'], self) setattr(self, '_exec_action_%d' % (i, ), method) # Add to our list of actions. self._exec_actions.append(method)
def getLogger(name): """Create logger with custom exception() method """ def exception(self, msg, *args, **kwargs): extra = kwargs.setdefault('extra', {}) extra['exc_fullstack'] = self.isEnabledFor(logging.DEBUG) kwargs['exc_info'] = True self.log(logging.ERROR, msg, *args, **kwargs) logger = logging.getLogger(name) logger.exception = six.create_bound_method(exception, logger) return logger
def add_context_manager_support(obj): """ Add empty __enter__ and __exit__ methods on a given object. This function is required to be called on any object used by the data() methods on the stores. They are supposed to be file-like byte streams. Adding the support for using them as context managers allows us to make sure we clean the resources when a proper __exit__ method is available. """ def __enter__(self): return self def __exit__(self, *args): pass if not hasattr(obj, '__enter__'): obj.__enter__ = create_bound_method(__enter__, obj) if not hasattr(obj, '__exit__'): obj.__exit__ = create_bound_method(__exit__, obj) return obj
def _create_action_method(self, i, action): # CM loading style action. Returns an object, such as a coord. ns = locals().copy() exec( compile( 'def _f(self, field, f, pp, grib, cm): return %s' % (action, ), '<string>', 'exec'), globals(), ns) # Make it a method of ours. method = six.create_bound_method(ns['_f'], self) setattr(self, '_exec_action_%d' % (i, ), method) # Add to our list of actions. self._exec_actions.append(method)
def session_maker(*args, **kwargs): session = sessionmaker(*args, **kwargs) # XXX in case we want to use session manager somehow bound # to request environment. For example, to generate user-specific # URLs. #session.file_manager = \ # kwargs.get('file_manager', file_manager) session.file_manager = file_manager session.find_file_manager = six.create_bound_method( find_file_manager, session) return session
def __getattr__(self, name): """Look up and return attributes that are not defined on this instance. This function is called when code tries to read from an attribute that is not defined in this instance's __dict__. This function looks up the attribute on self.lib, and adds a variety of prefixes to the attribute name if necessary. If the attribute is not found on self.lib, an AttributeError is raised; if it is found, it is returned. However, if the attribute is callable, the attribute is first bound to this instance as a method. :param name: the name of the attribute to find :raises AttributeError: if the attribute (possibly with prefixes) is not found on self.lib """ fun = None if re.match("__.*__", name): # This is a python internal name, skip it raise AttributeError if name == "lib": # If "lib" is not defined in self.__dict__, this function would # enter an infinite recursion if it tried to access self.lib. raise AttributeError # try it bare llib = self.lib try: fun = getattr(llib, name) except AttributeError: for prefix in self.prefixes: try: # wrap it fun = getattr(llib, prefix + name) break except AttributeError: pass if fun is None: # Return a proxy class to generate a good error on call error_printer = ErrorPrinter(name, self.prefixes) setattr(self, name, error_printer) return error_printer if not callable(fun): # pragma: no cover setattr(self, name, fun) return fun new_fun = self.check_wrap(fun, name) new_method = six.create_bound_method(new_fun, weakref.proxy(self)) # Store the wrapper function into the instance # to prevent a second lookup setattr(self, name, new_method) return new_method
def _create_conditions_method(self): # Bundle all the conditions into one big string. conditions = '(%s)' % ') and ('.join(self._conditions) if not conditions: conditions = 'None' # Create a method to evaluate the conditions. # NB. This creates the name '_f' in the 'ns' namespace, which is then # used below. code = 'def _f(self, field, f, pp, grib, cm): return %s' % conditions ns = locals().copy() exec(compile(code, '<string>', 'exec'), globals(), ns) # Make it a method of ours. self._exec_conditions = six.create_bound_method(ns['_f'], self)
def __get__(self, instance, owner): assert issubclass(owner, Stub), 'stubbed methods belong in Stub classes...' assert instance, 'implemented for instance methods only' real_method = getattr(owner.stubbed, self.stub.__name__) assert isinstance( real_method.im_func, types.FunctionType), 'stubbed methods are for methods...' real_args = inspect.getargspec(real_method.im_func) stub_args = inspect.getargspec(self.stub) assert real_args == stub_args, 'argument specification mismatch' return six.create_bound_method(self.stub, instance)
def _create_action_method(self, i, action): # PP saving style action. No return value, e.g. "pp.lbft = 3". rules_globals = _rules_execution_environment() compile_locals = {} exec( compile('def _f(self, field, f, pp, grib, cm): %s' % (action, ), '<string>', 'exec'), rules_globals, compile_locals) # Make it a method of ours. _f = compile_locals['_f'] method = six.create_bound_method(_f, self) setattr(self, '_exec_action_%d' % (i, ), method) # Add to our list of actions. self._exec_actions.append(method)
def _create_action_method(self, i, action): # PP saving style action. No return value, e.g. "pp.lbft = 3". ns = locals().copy() exec(compile('def _f(self, field, f, pp, grib, cm): %s' % (action, ), '<string>', 'exec'), globals(), ns) # Make it a method of ours. method = six.create_bound_method(ns['_f'], self) setattr(self, '_exec_action_%d' % (i, ), method) # Add to our list of actions. self._exec_actions.append(method)
def _create_action_method(self, i, action): # PP saving style action. No return value, e.g. "pp.lbft = 3". rules_globals = _rules_execution_environment() compile_locals = {} exec(compile('def _f(self, field, f, pp, grib, cm): %s' % (action, ), '<string>', 'exec'), rules_globals, compile_locals) # Make it a method of ours. _f = compile_locals['_f'] method = six.create_bound_method(_f, self) setattr(self, '_exec_action_%d' % (i, ), method) # Add to our list of actions. self._exec_actions.append(method)
def _give_columns_django_field_attributes(self): """ Add Django Field attributes to each cqlengine.Column instance. So that the Django Options class may interact with it as if it were a Django Field. """ methods_to_add = ( django_field_methods.value_from_object, django_field_methods.value_to_string, django_field_methods.get_attname, django_field_methods.get_cache_name, django_field_methods.pre_save, django_field_methods.get_prep_value, django_field_methods.get_choices, django_field_methods.get_choices_default, django_field_methods.save_form_data, django_field_methods.formfield, django_field_methods.get_db_prep_value, django_field_methods.get_db_prep_save, django_field_methods.db_type_suffix, django_field_methods.select_format, django_field_methods.get_internal_type, django_field_methods.get_attname_column, django_field_methods.check, django_field_methods._check_field_name, django_field_methods._check_db_index, django_field_methods.deconstruct, django_field_methods.run_validators, django_field_methods.clean, django_field_methods.get_db_converters, django_field_methods.get_prep_lookup, django_field_methods.get_db_prep_lookup, django_field_methods.get_filter_kwargs_for_object, django_field_methods.set_attributes_from_name, django_field_methods.db_parameters, django_field_methods.get_pk_value_on_save, django_field_methods.get_col, ) for name, cql_column in six.iteritems(self._defined_columns): self._set_column_django_attributes(cql_column=cql_column, name=name) for method in methods_to_add: try: method_name = method.func_name except AttributeError: # python 3 method_name = method.__name__ new_method = six.create_bound_method(method, cql_column) setattr(cql_column, method_name, new_method)
def __call__(self): """ Return the original listener method, or None if it no longer exists. """ obj = self.obj if obj is None: # Unbound method. return six.create_unbound_method(self.func, self.cls) else: objc = obj() if objc is None: # Bound method whose object has been garbage collected. return return six.create_bound_method(self.func, objc)
def _create_conditions_method(self): # Bundle all the conditions into one big string. conditions = '(%s)' % ') and ('.join(self._conditions) if not conditions: conditions = 'None' # Create a method to evaluate the conditions. # NB. This creates the name '_f' in the 'compile_locals' namespace, # which is then used below. code = 'def _f(self, field, f, pp, grib, cm): return %s' % conditions rules_globals = _rules_execution_environment() compile_locals = {} exec(compile(code, '<string>', 'exec'), rules_globals, compile_locals) # Make it a method of ours. _f = compile_locals['_f'] self._exec_conditions = six.create_bound_method(_f, self)
def _create_action_method(self, i, action): # CM loading style action. Returns an object, such as a coord. # Compile a new method for the operation. rules_globals = _rules_execution_environment() compile_locals = {} exec( compile("def _f(self, field, f, pp, grib, cm): return %s" % (action,), "<string>", "exec"), rules_globals, compile_locals, ) # Make it a method of ours. _f = compile_locals["_f"] method = six.create_bound_method(_f, self) setattr(self, "_exec_action_%d" % (i,), method) # Add to our list of actions. self._exec_actions.append(method)
def __init__(self, *args, **kwargs): """Inits the document with given data and validates the fields (field validation bad idea during init?). If you define ``__init__`` method for your document class, make sure to call this :: class MyDoc(BaseDocument, dot_notation=True): foo = Field(str) bar = Field(int, required=False) def __init__(self, *args, **kwargs): super(MyDoc, self).__init__(*args, **kwargs) # do other stuff """ # if input dict, merge (not updating) into kwargs if args and not isinstance(args[0], dict): raise TypeError('dict or dict subclass argument expected') elif args: for field_name, field_value in args[0].items(): if field_name not in kwargs: kwargs[field_name] = field_value super(BaseDocument, self).__init__() for field_name, field in self.nanomongo.fields.items(): if hasattr(field, 'default_value'): val = field.default_value dict.__setitem__(self, field_name, val() if callable(val) else val) # attach get_<field_name>_field methods for DBRef fields if field.data_type in [DBRef] + DBRef.__subclasses__(): getter_name = 'get_%s_field' % field_name doc_class = field.document_class if hasattr(field, 'document_class') else None getter = ref_getter_maker(field_name, document_class=doc_class) setattr(self, getter_name, six.create_bound_method(getter, self)) for field_name in kwargs: if self.nanomongo.has_field(field_name): self.nanomongo.validate(field_name, kwargs[field_name]) dict.__setitem__(self, field_name, kwargs[field_name]) else: raise ExtraFieldError('Undefined field %s=%s in %s' % (field_name, kwargs[field_name], self.__class__)) for field_name, field_value in self.items(): # transform dict to RecordingDict so we can track diff in embedded docs if isinstance(field_value, dict): dict.__setitem__(self, field_name, RecordingDict(field_value))
def test_365_calendar(self): f = mock.MagicMock(lbtim=SplittableInt(4, {'ia': 2, 'ib': 1, 'ic': 0}), lbyr=2013, lbmon=1, lbdat=1, lbhr=12, lbmin=0, lbsec=0, t1=netcdftime.datetime(2013, 1, 1, 12, 0, 0), t2=netcdftime.datetime(2013, 1, 2, 12, 0, 0), spec=PPField3) f.time_unit = six.create_bound_method(PPField3.time_unit, f) f.calendar = cf_units.CALENDAR_365_DAY (factories, references, standard_name, long_name, units, attributes, cell_methods, dim_coords_and_dims, aux_coords_and_dims) = convert(f) def is_t_coord(coord_and_dims): coord, dims = coord_and_dims return coord.standard_name == 'time' coords_and_dims = list(filter(is_t_coord, aux_coords_and_dims)) self.assertEqual(len(coords_and_dims), 1) coord, dims = coords_and_dims[0] self.assertEqual(guess_coord_axis(coord), 'T') self.assertEqual(coord.units.calendar, '365_day')
def __get__(self, obj, obj_type=None): """ To allow each instance of a class to have different callbacks registered we store a callback registry on the instance itself. Keying off of the id of the decorator allows us to have multiple methods support callbacks on the same instance simultaneously. """ # method is being called on the class instead of an instance if obj is None: # when target was decorated, it had not been bound yet, but now it # is, so update _target_is_method. self._target_is_method = True return self if (obj not in self._callback_registries): callback_registry = SupportsCallbacks(self, target_is_method=True) self._callback_registries[obj] = proxy(callback_registry) else: callback_registry = self._callback_registries[obj] return six.create_bound_method(callback_registry, obj)
def create_multi_node_evaluator(actual_evaluator, communicator): """Create a multi node evaluator from a normal evaluator. Actually this method patches the evaluator to work in multi node environment. This method adds several hidden attributes starting with `_mn_` prefix. Args: actual_evaluator: evaluator to be patched (e.g., ``chainer.training.extensions.Evaluator``) communicator: ChainerMN communicator Returns: The multi-node patched ``actual_evaluator``. .. note:: After patched, original evaluator does not work correctly in non-MPI environment. """ actual_evaluator._mn_original_evaluate = actual_evaluator.evaluate actual_evaluator._mn_communicator = communicator def new_evaluate(self): local_mean_dict = self._mn_original_evaluate() global_mean_dict = { name: self._mn_communicator.allreduce_obj( value) / self._mn_communicator.size for name, value in sorted(local_mean_dict.items()) } return global_mean_dict actual_evaluator.evaluate = six.create_bound_method( new_evaluate, actual_evaluator) return actual_evaluator
def addMethods(self, usePsyco=False): """Add Python-specific functions to this object's methods, accelerating them with psyco, if it is available.""" # Add the auxiliary function specs to this Generator's namespace for auxfnname in self.funcspec._pyauxfns: fninfo = self.funcspec._pyauxfns[auxfnname] if not hasattr(self, fninfo[1]): # user-defined auxiliary functions # (built-ins are provided explicitly) try: exec(fninfo[0]) except: print("Error in supplied auxiliary function code") self._funcreg[fninfo[1]] = ("self", fninfo[0]) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self)) # user auxiliary function interface wrapper try: uafi_code = self.funcspec._user_auxfn_interface[auxfnname] try: exec(uafi_code) except: print("Error in auxiliary function wrapper") raise setattr(self.auxfns, auxfnname, six.create_bound_method(locals()[auxfnname], self.auxfns)) self._funcreg[auxfnname] = ("", uafi_code) except KeyError: # not a user-defined aux fn pass # bind all the auxfns here if HAVE_PSYCO and usePsyco: psyco.bind(getattr(self, fninfo[1])) try: psyco.bind(self.auxfns[auxfnname]) except KeyError: # not a user-defined aux fn pass # Add the spec function to this Generator's namespace if # target language is python (otherwise integrator exposes it anyway) if self.funcspec.targetlang == "python": fninfo = self.funcspec.spec try: exec(fninfo[0]) except: print("Error in supplied functional specification code") raise self._funcreg[fninfo[1]] = ("self", fninfo[0]) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self)) if HAVE_PSYCO and usePsyco: psyco.bind(getattr(self, fninfo[1])) # Add the auxiliary spec function (if present) to this # Generator's namespace if self.funcspec.auxspec != "": fninfo = self.funcspec.auxspec try: exec(fninfo[0]) except: print("Error in supplied auxiliary variable code") raise self._funcreg[fninfo[1]] = ("self", fninfo[0]) setattr(self, fninfo[1], six.create_bound_method(locals()[fninfo[1]], self)) if HAVE_PSYCO and usePsyco: psyco.bind(getattr(self, fninfo[1]))
def __get__(self, obj, owner): return type(self)(six.create_bound_method(self._should_ignore_ex, obj))
def _generate_operator_funcs(self, yaql_operators, engine): binary_doc = '' unary_doc = '' precedence_dict = {} for up, bp, op_name, op_alias in yaql_operators.operators.values(): self._aliases[op_name] = op_alias if up: l = precedence_dict.setdefault( (abs(up), 'l' if up > 0 else 'r'), []) l.append('UNARY_' + op_name if bp else op_name) unary_doc += ('value : ' if not unary_doc else '\n| ') spec_prefix = '{0} value' if up > 0 else 'value {0}' if bp: unary_doc += (spec_prefix + ' %prec UNARY_{0}').format( op_name) else: unary_doc += spec_prefix.format(op_name) if bp: l = precedence_dict.setdefault( (abs(bp), 'l' if bp > 0 else 'r'), []) l.append(op_name) binary_doc += ('value : ' if not binary_doc else '\n| ') + \ 'value {0} value'.format(op_name) # noinspection PyProtectedMember def p_binary(this, p): alias = this._aliases.get(p.slice[2].type) p[0] = expressions.BinaryOperator(p[2], p[1], p[3], alias) # noinspection PyProtectedMember def p_unary(this, p): if p[1] in yaql_operators.operators: alias = this._aliases.get(p.slice[1].type) p[0] = expressions.UnaryOperator(p[1], p[2], alias) else: alias = this._aliases.get(p.slice[2].type) p[0] = expressions.UnaryOperator(p[2], p[1], alias) p_binary.__doc__ = binary_doc self.p_binary = six.create_bound_method(p_binary, self) p_unary.__doc__ = unary_doc self.p_unary = six.create_bound_method(p_unary, self) precedence = [] for i in range(1, len(precedence_dict) + 1): for oa in ('r', 'l'): value = precedence_dict.get((i, oa)) if value: precedence.append( (('left',) if oa is 'l' else ('right',)) + tuple(value) ) precedence.insert(1, ('left', 'LIST', 'INDEXER', 'MAP')) precedence.insert(0, ('left', 'MAPPING')) precedence.insert(0, ('left', ',')) precedence.reverse() self.precedence = tuple(precedence) def p_value_call(this, p): """ func : value '(' args ')' """ arg = () if len(p) > 4: arg = p[3] p[0] = expressions.Function('#call', p[1], *arg) if engine.allow_delegates: self.p_value_call = six.create_bound_method(p_value_call, self)