本文整理汇总了Python中twisted.python.util.mergeFunctionMetadata函数的典型用法代码示例。如果您正苦于以下问题:Python mergeFunctionMetadata函数的具体用法?Python mergeFunctionMetadata怎么用?Python mergeFunctionMetadata使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了mergeFunctionMetadata函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: patch_noargs_decorator
def patch_noargs_decorator(decorator):
def new_decorator(func):
wrapper = decorator(func)
wrapper.__wrapped__ = func
return wrapper
util.mergeFunctionMetadata(decorator, new_decorator)
return new_decorator
示例2: onlyOnce
def onlyOnce(fn):
'Set up FN to only run once within an interpreter instance'
def wrap(*args, **kwargs):
if hasattr(fn, 'called'):
return
fn.called = 1
return fn(*args, **kwargs)
util.mergeFunctionMetadata(fn, wrap)
return wrap
示例3: _maybeUnhandled
def _maybeUnhandled(fn):
def wrap(self, *args, **kwargs):
d = fn(self, *args, **kwargs)
if self._start_unhandled_deferreds is not None:
self._start_unhandled_deferreds.append(d)
return d
wrap.__wrapped__ = fn
twutil.mergeFunctionMetadata(fn, wrap)
return wrap
示例4: _f
def _f(f):
def g(*args, **kw):
_close_db()
try:
ret = f(*args, **kw)
finally:
_close_db()
return ret
mergeFunctionMetadata(f, g)
return g
示例5: _f
def _f(f):
def g(self, *args, **kw):
if not hasattr(self, 'identify_successful'):
raise managementprotocol.InternalServerError('no identify_successful attribute found')
elif not isinstance(self.identify_successful, bool):
raise managementprotocol.InternalServerError('identify_successful attribute not boolean')
elif not self.identify_successful:
raise managementprotocol.ProtocolStateError('request requires successful Identify')
else:
return f(self, *args, **kw)
mergeFunctionMetadata(f, g)
return g
示例6: _f
def _f(f):
def g(*args, **kw):
starttime = datetime.datetime.utcnow()
dbase = database
if dbase is None:
raise Exception('dbase is None')
_transact_begin_log_helper('@untransact', f, dbase)
if (dbase is not None) and hasattr(dbase, 'store') and (dbase.store is not None):
# hasattr + store check is to fix #621, this is also an API issue with rdf.py
t = dbase.begin_untransaction()
else:
t = None
global toplevel_transaction
# Log transact locktime here if active
if t is not None:
# XXX: There is no sensible date value to show here,
# because we are not actually logging anything for
# current untransaction: we log the locktime of the
# toplevel transaction which is committed here. Using
# current time as both timestamps for now.
_transact_end_log_helper('@transact (in untransact)', toplevel_transaction, dbase, True, datetime.datetime.utcnow(), datetime.datetime.utcnow(), t.get_txn_locked_time(), TRANSACT_LOCK_TIME_WARNING_LIMIT, untransact_point=f)
try:
ret = f(*args, **kw)
except:
t_locked_time = None
if t is not None:
t_locked_time = t.get_locked_time()
t.commit()
endtime = datetime.datetime.utcnow()
_transact_end_log_helper('@untransact', f, dbase, False, starttime, endtime, t_locked_time, UNTRANSACT_LOCK_TIME_WARNING_LIMIT)
raise
t_locked_time = None
if t is not None:
t_locked_time = t.get_locked_time()
t.commit()
endtime = datetime.datetime.utcnow()
_transact_end_log_helper('@untransact', f, dbase, True, starttime, endtime, t_locked_time, UNTRANSACT_LOCK_TIME_WARNING_LIMIT)
return ret
mergeFunctionMetadata(f, g)
return g
示例7: deferredGenerator
def deferredGenerator(f):
"""
deferredGenerator and waitForDeferred help you write L{Deferred}-using code
that looks like a regular sequential function. If your code has a minimum
requirement of Python 2.5, consider the use of L{inlineCallbacks} instead,
which can accomplish the same thing in a more concise manner.
There are two important functions involved: L{waitForDeferred}, and
L{deferredGenerator}. They are used together, like this::
def thingummy():
thing = waitForDeferred(makeSomeRequestResultingInDeferred())
yield thing
thing = thing.getResult()
print thing #the result! hoorj!
thingummy = deferredGenerator(thingummy)
L{waitForDeferred} returns something that you should immediately yield; when
your generator is resumed, calling C{thing.getResult()} will either give you
the result of the L{Deferred} if it was a success, or raise an exception if it
was a failure. Calling C{getResult} is B{absolutely mandatory}. If you do
not call it, I{your program will not work}.
L{deferredGenerator} takes one of these waitForDeferred-using generator
functions and converts it into a function that returns a L{Deferred}. The
result of the L{Deferred} will be the last value that your generator yielded
unless the last value is a L{waitForDeferred} instance, in which case the
result will be C{None}. If the function raises an unhandled exception, the
L{Deferred} will errback instead. Remember that C{return result} won't work;
use C{yield result; return} in place of that.
Note that not yielding anything from your generator will make the L{Deferred}
result in C{None}. Yielding a L{Deferred} from your generator is also an error
condition; always yield C{waitForDeferred(d)} instead.
The L{Deferred} returned from your deferred generator may also errback if your
generator raised an exception. For example::
def thingummy():
thing = waitForDeferred(makeSomeRequestResultingInDeferred())
yield thing
thing = thing.getResult()
if thing == 'I love Twisted':
# will become the result of the Deferred
yield 'TWISTED IS GREAT!'
return
else:
# will trigger an errback
raise Exception('DESTROY ALL LIFE')
thingummy = deferredGenerator(thingummy)
Put succinctly, these functions connect deferred-using code with this 'fake
blocking' style in both directions: L{waitForDeferred} converts from a
L{Deferred} to the 'blocking' style, and L{deferredGenerator} converts from the
'blocking' style to a L{Deferred}.
"""
def unwindGenerator(*args, **kwargs):
return _deferGenerator(f(*args, **kwargs), Deferred())
return mergeFunctionMetadata(f, unwindGenerator)
示例8: fastInline
def fastInline(f):
"""This decorator is mostly equivalent to defer.inlineCallbacks except
that speed is the primary priority. while inlineCallbacks checks a few
different things to make sure you're doing it properly, this method simply
assumes you are.
changes:
* this decorator no longer checks that you're wrapping a generator
* this decorator no longer checks that _DefGen_Return is thrown from the
decoratored generator only
* the generator loop no longer double checks that you're yielding
deferreds and simply assumes it, catching the exception if you aren't
* the generator stops calling isinstance for logic - profiling reveals
that isinstance consumes a nontrivial amount of computing power during
heavy use
You can force this method to behave exactly like defer.inlineCallbacks by
providing the FORCE_STANDARD_INLINE_CALLBACKS environment variable
"""
def unwind(*args, **kwargs):
try:
gen = f(*args, **kwargs)
except defer._DefGen_Return: # pylint: disable=W0212
raise TypeError("defer.returnValue used from non-generator")
return _inlineCallbacks(None, None, gen, defer.Deferred())
return mergeFunctionMetadata(f, unwind)
示例9: goodDecorator
def goodDecorator(fn):
"""
Decorate a function and preserve the original name.
"""
def nameCollision(*args, **kwargs):
return fn(*args, **kwargs)
return mergeFunctionMetadata(fn, nameCollision)
示例10: decorator
def decorator(function):
def decorated(*a, **kw):
with context_factory(*args, **kwargs):
return function(*a, **kw)
return mergeFunctionMetadata(function, decorated)
示例11: fireWhenDoneFunc
def fireWhenDoneFunc(d, f):
"""Returns closure that when called calls f and then callbacks d.
"""
def newf(*args, **kw):
rtn = f(*args, **kw)
d.callback('')
return rtn
return util.mergeFunctionMetadata(f, newf)
示例12: make_it_green
def make_it_green(f):
def unwindGenerator(*args, **kwargs):
g = greenlet(f)
# Похоже, что такой тупой ход парента не меняет.
# Также похоже, что и без него работает, оставляя выполнение в текущем гринлете.
#g.parent = main
return _inline_greens(None, g, (args, kwargs), defer.Deferred())
return mergeFunctionMetadata(f, unwindGenerator)
示例13: __getattr__
def __getattr__(self, name):
maybe_method = getattr(self._transport, name)
if not callable(maybe_method):
return maybe_method
def defer_it(*args, **kwargs):
return defer.maybeDeferred(maybe_method, *args, **kwargs)
return mergeFunctionMetadata(maybe_method, defer_it)
示例14: _deco
def _deco(meth):
methodName = meth.__name__
def _soapMethod(self, *args, **kwargs):
def _parse(body):
return body[0]
request = getattr(NS, methodName)
request, parser = meth(self, request, *args, **kwargs)
return self.call(_uj(_nsuri(NS), methodName), request).addCallback(_parse).addCallback(parser)
return util.mergeFunctionMetadata(meth, _soapMethod)
示例15: atSpecifiedTime
def atSpecifiedTime(when, func):
def inner(*a, **kw):
orig = time.time
time.time = lambda: when
try:
return func(*a, **kw)
finally:
time.time = orig
return util.mergeFunctionMetadata(func, inner)