本文整理汇总了Python中six.get_method_function方法的典型用法代码示例。如果您正苦于以下问题:Python six.get_method_function方法的具体用法?Python six.get_method_function怎么用?Python six.get_method_function使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six
的用法示例。
在下文中一共展示了six.get_method_function方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: subscribe
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def subscribe(func):
'''
Add a subscriber function to option events
Parameters
----------
func : callable
A callable object that takes two parameters: key and value.
This function is called with the name and value of any option
that is set.
Returns
-------
None
'''
if isinstance(func, types.MethodType):
obj = six.get_method_self(func)
func = six.get_method_function(func)
_subscribers[func] = (weakref.ref(func), weakref.ref(obj))
else:
_subscribers[func] = (weakref.ref(func), None)
示例2: wrapper
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def wrapper(self):
if not self._wrapped:
if self._instance or self._class:
wrapped = self._func.__get__(self._instance, self._class)
if isinstance(self._func, staticmethod):
# we don't need instance or class, however we need scope
self.cache.scope = self._instance or self._class
self._instance = None
self._class = None
else:
wrapped = six.get_method_function(wrapped)
else:
wrapped = self._func
update_wrapper(self.cache, wrapped)
self.cache.function = wrapped
self.cache.instance = self._instance
self.cache.klass = self._class
self._wrapped = True
return self.cache
示例3: _get_op_handler
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def _get_op_handler(operation):
""" Import and load the operation handler """
try:
mod_to_import, attr_path = operation.split('#')
op = import_module(mod_to_import)
for part in attr_path.split('.'):
op = getattr(op, part)
if isinstance(op, types.FunctionType):
return op
return six.get_method_function(op)
except (ValueError, AttributeError):
raise ValueError("The operation '{}' is invalid.".format(operation))
示例4: _find_method
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def _find_method(obj, func):
if obj:
try:
func_self = six.get_method_self(func)
except AttributeError: # func has no __self__
pass
else:
if func_self is obj:
return six.get_method_function(func).__name__
raise ValueError("Function %s is not a method of: %s" % (func, obj))
示例5: _find_method
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def _find_method(obj, func):
if obj:
try:
func_self = six.get_method_self(func)
except AttributeError: # func has no __self__
pass
else:
if func_self is obj:
name = six.get_method_function(func).__name__
if _is_private_method(name):
return _mangle_private_name(obj, func, name)
return name
raise ValueError("Function %s is not a method of: %s" % (func, obj))
示例6: unbind
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def unbind(func):
"""Get Function from Method (if not already Function)."""
if isinstance(func, types.MethodType):
func = six.get_method_function(func)
return func
示例7: test_get_method_function
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def test_get_method_function():
class X(object):
def m(self):
pass
x = X()
assert six.get_method_function(x.m) is X.__dict__["m"]
pytest.raises(AttributeError, six.get_method_function, hasattr)
示例8: __get_unbound_function
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def __get_unbound_function(method):
try:
return six.get_method_function(method)
except AttributeError:
return method
示例9: test_get_method_function
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def test_get_method_function():
class X(object):
def m(self):
pass
x = X()
assert six.get_method_function(x.m) is X.__dict__["m"]
py.test.raises(AttributeError, six.get_method_function, hasattr)
示例10: __details__
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def __details__(self):
with utils.patch(
six.get_method_function(self.tasks.service.destroy),
'__doc__',
self.__doc__ + (self.tasks.service.destroy.__doc__ or ''),
):
return get_task_details(self.tasks.service.destroy)
示例11: object_build
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def object_build(self, node, obj):
"""recursive method which create a partial ast from real objects
(only function, class, and method are handled)
"""
if obj in self._done:
return self._done[obj]
self._done[obj] = node
for name in dir(obj):
try:
member = getattr(obj, name)
except AttributeError:
# damned ExtensionClass.Base, I know you're there !
attach_dummy_node(node, name)
continue
if inspect.ismethod(member):
member = six.get_method_function(member)
if inspect.isfunction(member):
_build_from_function(node, name, member, self._module)
elif inspect.isbuiltin(member):
if (not _io_discrepancy(member) and
self.imported_member(node, member, name)):
continue
object_build_methoddescriptor(node, member, name)
elif inspect.isclass(member):
if self.imported_member(node, member, name):
continue
if member in self._done:
class_node = self._done[member]
if class_node not in node.locals.get(name, ()):
node.add_local_node(class_node, name)
else:
class_node = object_build_class(node, member, name)
# recursion
self.object_build(class_node, member)
if name == '__class__' and class_node.parent is None:
class_node.parent = self._done[self._module]
elif inspect.ismethoddescriptor(member):
assert isinstance(member, object)
object_build_methoddescriptor(node, member, name)
elif inspect.isdatadescriptor(member):
assert isinstance(member, object)
object_build_datadescriptor(node, member, name)
elif isinstance(member, _CONSTANTS):
attach_const_node(node, name, member)
elif inspect.isroutine(member):
# This should be called for Jython, where some builtin
# methods aren't caught by isbuiltin branch.
_build_from_function(node, name, member, self._module)
else:
# create an empty node so that the name is actually defined
attach_dummy_node(node, name, member)
return None
示例12: _get_observer_key_value
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def _get_observer_key_value(observer):
"""
Subscribing methods of objects is tricky::
class TheObserver(object):
def __init__(self):
self.received_data = []
def on_new_data(self, data):
self.received_data.append(data)
observer1 = TheObserver()
observer2 = TheObserver()
subscribe(observer1.on_new_data)
subscribe(observer2.on_new_data)
subscribe(observer2.on_new_data)
Even if it looks like 2 different subscriptions they all
pass 3 different bound-method objects (different id()).
So, to differentiate them we need to "unwind" out of them:
1) self - 2 different id()
2) function object of class - all 3 have same id()
Observer key is pair: (self-id, function-id)
"""
try:
self_or_none = six.get_method_self(observer)
self_id = instance_id(self_or_none)
self_or_none = weakref.proxy(self_or_none)
except AttributeError:
self_id = 0 # default for not bound methods
self_or_none = None
try:
func = six.get_method_function(observer)
except AttributeError:
func = observer
function_id = instance_id(func)
observer_key = (self_id, function_id)
observer_value = (self_or_none, weakref.proxy(func))
return observer_key, observer_value
示例13: _get_subscriber_key_and_value
# 需要导入模块: import six [as 别名]
# 或者: from six import get_method_function [as 别名]
def _get_subscriber_key_and_value(subscriber):
"""
Allow Subscribers to be garbage collected while still subscribed inside Publisher
Subscribing methods of objects is tricky::
class TheObserver(object):
def __init__(self):
self.received_data = []
def on_new_data(self, data):
self.received_data.append(data)
observer1 = TheObserver()
observer2 = TheObserver()
subscribe(observer1.on_new_data)
subscribe(observer2.on_new_data)
subscribe(observer2.on_new_data)
Even if it looks like 2 different subscriptions they all
pass 3 different bound-method objects (different id()).
This is so since access via observer1.on_new_data
creates new object (bound method) on the fly.
We want to use weakref but weakref to bound method doesn't work
see: http://code.activestate.com/recipes/81253/
and : https://stackoverflow.com/questions/599430/why-doesnt-the-weakref-work-on-this-bound-method
When we wrap bound-method into weakref it may quickly disappear
if that is only reference to bound method.
So, we need to unbind it to have access to real method + self instance
Unbinding above 3 examples of on_new_data will give:
1) self - 2 different id()
2) function object of class - all 3 have same id()
Observer key is pair: (self-id, function-id)
"""
try:
self_or_none = six.get_method_self(subscriber)
self_id = instance_id(self_or_none)
self_or_none = weakref.proxy(self_or_none)
except AttributeError:
self_id = 0 # default for not bound methods
self_or_none = None
try:
func = six.get_method_function(subscriber)
except AttributeError:
func = subscriber
function_id = instance_id(func)
subscription_key = (self_id, function_id)
subscription_value = (self_or_none, weakref.proxy(func))
return subscription_key, subscription_value