本文整理汇总了Python中decorator.FunctionMaker类的典型用法代码示例。如果您正苦于以下问题:Python FunctionMaker类的具体用法?Python FunctionMaker怎么用?Python FunctionMaker使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了FunctionMaker类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: generic
def generic(func):
typemap = {(object,) * len(dispatch_args): func}
def when_type(*types):
"Decorator to add a method that will be called for the given types"
if len(types) != len(dispatch_args):
raise TypeError('Length mismatch: expected %d types, got %d' %
(len(dispatch_args), len(types)))
def dec(f):
typemap[types] = f
return f
return dec
def dispatch(dispatch_args, *args, **kw):
types = tuple(type(arg) for arg in dispatch_args)
try: # fast path
return typemap[types](*args, **kw)
except KeyError:
pass
_gettypemap = typemap.get
for types in itertools.product(*(t.__mro__ for t in types)):
f = _gettypemap(types)
if f is not None:
return f(*args, **kw)
# else call the default implementation
return func(*args, **kw)
return FunctionMaker.create(
func, 'return _f_({}, %(shortsignature)s)'.format(dispatch_str),
dict(_f_=dispatch),
when_type=when_type,
default=func,
typemap=typemap)
示例2: generate_ui_command
def generate_ui_command(self, display_name, name=None, argspec=None, method=None, function=None):
if name is None:
name = display_name
if argspec.args[0] == "self":
argspec.args.pop(0)
signature = "%s(self, %s)" % (name, inspect.formatargspec(formatvalue=lambda val: "", *argspec)[1:-1])
# logger.error('sig=%s', signature)
def func(self, *args, **kwargs):
command = func.__command__
return self(_meth=command, *args, **kwargs)
func = FunctionMaker.create(
signature,
# "return _func_(self, command, %(shortsignature)s)",
"return _func_(%(shortsignature)s)",
dict(_func_=func),
__command__=name,
)
func = types.MethodType(func, self)
setattr(self, name, func)
self._generated_ui_commands[name] = func
示例3: decorator_apply
def decorator_apply(dec, func):
"""
Decorate a function by preserving the signature even if dec
is not a signature-preserving decorator.
"""
return FunctionMaker.create(
func, 'return decfunc(%(signature)s)',
dict(decfunc=dec(func)), __wrapped__=func)
示例4: decorator_apply
def decorator_apply(dec, func):
"""Decorate a function by preserving the signature even if dec
is not a signature-preserving decorator.
This recipe is derived from
http://micheles.googlecode.com/hg/decorator/documentation.html#id14
"""
return FunctionMaker.create(
func, 'return decorated(%(signature)s)',
dict(decorated=dec(func)), __wrapped__=func)
示例5: replace_funct
def replace_funct(fnc, body):
"""Returns a function with a new body that replaces the given function. The
signature of the original function is preserved in the new function.
"""
fname = fnc.__name__
spec = getargspec(fnc)
sig = formatargspec(*spec)
return FunctionMaker.create('%s%s' % (fname,sig), body, {}, defaults=spec[3],
doc=fnc.__doc__)
示例6: decorator_apply
def decorator_apply(dec, func):
"""
Decorate a function by preserving the signature even if dec
is not a signature-preserving decorator.
"""
if (type(func) == types.BuiltinFunctionType) or (type(func) == types.BuiltinMethodType):
return builtin_decorator_apply(dec, func)
# FunctionMaker doesn't seem to work for built-ins (i.e. compiled code, it should though).
return FunctionMaker.create(
func, 'return decorated(%(signature)s)',
dict(decorated=dec(func)), undecorated=func)
示例7: forwarder
def forwarder(cls, fnc, delegatename):
"""Returns a method that forwards calls on the scoping object to calls
on the delegate object. The signature of the delegate method is preserved
in the forwarding method.
"""
fname = fnc.__name__
spec = getargspec(fnc)
sig = formatargspec(*spec)
body = "return self.%s.%s(%s)" % (delegatename, fname, ",".join(spec[0][1:]))
f = FunctionMaker.create("%s%s" % (fname, sig), body, {}, defaults=spec[3], doc=fnc.__doc__)
return types.MethodType(f, None, cls)
示例8: _rpc
def _rpc(func):
global _rpc_next_id, _rpc_map
# Store function in the call map
func_id = _rpc_next_id
_rpc_next_id += 1
_rpc_map[func_id] = func
# For local usage of the function, append a _ in the name
#globals()['_' + func.__name__] = func
# Handle special parameter 'websocket'
func_meta = inspect.getargspec(func)
try:
ws_idx = func_meta.args.index('ws')
except ValueError:
ws_idx = -1
defaults = func_meta.defaults
def ws_proxy_call(*args, **kwar):
if len(args) > ws_idx:
(ws_fd, ws_uid) = _ws_serialize(args[ws_idx])
args = list(args)
args[ws_idx] = ws_uid
ws_locator = ws_idx
else:
try:
ws = kwar['ws']
except KeyError:
ws = defaults[ws_idx]
(ws_fd, ws_uid) = _ws_serialize(ws)
kwar['ws'] = ws_uid
ws_locator = None
msg = pickle.dumps((func_id, args, kwar, ws_locator), pickle.HIGHEST_PROTOCOL)
ret = fd_trick.send_with_fd(_call_endpoint, msg, ws_fd)
if ret != len(msg):
# TODO: Weird! Can this ever happen? Maybe if message is too big.
# Do something about it...
pass
def proxy_call(*args, **kwar):
msg = pickle.dumps((func_id, args, kwar), pickle.HIGHEST_PROTOCOL)
ret = _call_endpoint.send(msg)
if ret != len(msg):
# TODO: Weird! Can this ever happen? Maybe if message is too big.
# Do something about it...
pass
# Have no ideia of how this works, just copied from decorator.py:
evaldict = func.func_globals.copy()
evaldict['_call_'] = ws_proxy_call if ws_idx >= 0 else proxy_call
return FunctionMaker.create(func, "return _call_(%(shortsignature)s)", evaldict)
示例9: factory
def factory(name, params, templ, rx=re.compile(r'{([^}]+)\}')):
"Return a factory of Query objects"
def _makesql(namespace, kind, as_, globs):
def evaluator(match):
return str(eval(match.group(1), globs or {}, namespace))
evaluated_templ = rx.sub(evaluator, templ)
return Query(evaluated_templ, kind, as_)
signature = "{}({}, kind, as_, globs)".format(name, ', '.join(params))
body = 'return _makesql(dict({}), kind, as_, globs)'.format(
', '.join('%s=%s' % (p, p) for p in params))
makesql = FunctionMaker.create(
signature, body, dict(_makesql=_makesql), defaults=('all', None, None))
makesql.__doc__ = 'Build query objects from the template\n' + templ
makesql.templ = templ
return makesql
示例10: init_wrapper
def init_wrapper(cls, delegate_class_list):
"""Returns a function that calls the wrapped class' __init__ function
and then creates an instance of each delegate class in delegate_class_list.
"""
fnc = cls.__init__
spec = getargspec(fnc)
sig = formatargspec(*spec)
template = ["if not hasattr(self, '_delegates_'): self._delegates_ = {}"]
for name, delegate in delegate_class_list:
template.append('self.%s = %s(self)' % (name, delegate.__name__))
template.append("self._delegates_['%s'] = self.%s" % (name,name))
template.append('old_init__(%s)' % ','.join(spec[0]))
f = FunctionMaker.create('__init__%s' % sig, '\n'.join(template),
dict([(c.__name__,c) for n,c in delegate_class_list]+
[('old_init__',fnc)]),
doc=fnc.__doc__, defaults=spec[3])
return types.MethodType(f, None, cls)
示例11: litetask
def litetask(func):
"""
Add monitoring support to the decorated function. The last argument
must be a monitor object.
"""
def wrapper(*args):
monitor = args[-1]
monitor.flush = noflush
with monitor('total ' + func.__name__, measuremem=True):
result = func(*args)
delattr(monitor, 'flush')
return result
# NB: we need pickle=True because celery is using the worst possible
# protocol; once we remove celery we can try to remove pickle=True
return FunctionMaker.create(
func, 'return _s_(_w_, (%(shortsignature)s,), pickle=True)',
dict(_s_=safely_call, _w_=wrapper), task_func=func)
示例12: litetask_futures
def litetask_futures(func):
"""
Add monitoring support to the decorated function. The last argument
must be a monitor object.
"""
def wrapper(*args):
monitor = args[-1]
check_mem_usage(monitor) # check if too much memory is used
monitor.flush = NoFlush(monitor, func.__name__)
with monitor('total ' + func.__name__, measuremem=True), \
GroundShakingIntensityModel.forbid_instantiation():
result = func(*args)
rec_delattr(monitor, 'flush')
return result
# NB: we need pickle=True because celery is using the worst possible
# protocol; once we remove celery we can try to remove pickle=True
return FunctionMaker.create(
func, 'return _s_(_w_, (%(shortsignature)s,), pickle=True)',
dict(_s_=safely_call, _w_=wrapper), task_func=func)
示例13: decorator
def decorator(f):
# collect our argspec
sig = signature(f)
@wraps(f)
def _(*args, **kwargs):
bvals = sig.bind(*args, **kwargs)
# replace with instance if desired
for varname, val in bvals.arguments.items():
anno = sig.parameters[varname].annotation
if anno in classes or (len(classes) == 0 and anno != _empty):
bvals.arguments[varname] = anno(val)
return f(*bvals.args, **bvals.kwargs)
# create another layer by wrapping in a FunctionMaker. this is done
# to preserve the original signature
return FunctionMaker.create(
f, 'return _(%(signature)s)', dict(_=_, __wrapped__=f)
)
示例14: dec
def dec(f):
check_is_argument(conn_arg, f)
return FunctionMaker.create(f, '''\
with {}.opencopy() as {}, {}.begin():
return _f_(%(shortsignature)s)
'''.format(conn_arg, conn_arg, conn_arg), dict(_f_=f))
示例15: decorator_apply
def decorator_apply(dec, func):
return FunctionMaker.create(
func, 'return decorated(%(signature)s)',
dict(decorated=dec(func)), __wrapped__=func)