本文整理汇总了Python中new.instancemethod方法的典型用法代码示例。如果您正苦于以下问题:Python new.instancemethod方法的具体用法?Python new.instancemethod怎么用?Python new.instancemethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类new
的用法示例。
在下文中一共展示了new.instancemethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _unjelly_method
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def _unjelly_method(self, rest):
''' (internal) unjelly a method
'''
im_name = rest[0]
im_self = self.unjelly(rest[1])
im_class = self.unjelly(rest[2])
if type(im_class) is not types.ClassType:
raise InsecureJelly("Method found with non-class class.")
if im_class.__dict__.has_key(im_name):
if im_self is None:
im = getattr(im_class, im_name)
elif isinstance(im_self, NotKnown):
im = _InstanceMethod(im_name, im_self, im_class)
else:
im = instancemethod(im_class.__dict__[im_name],
im_self,
im_class)
else:
raise 'instance method changed'
return im
示例2: force_unicode
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def force_unicode(s, encoding='UTF-8'):
return str(s)
# new.instancemethod() is obsolete for new-style classes (Python 3.x)
# We need to use descriptor methods instead.
示例3: _unjelly_method
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def _unjelly_method(self, rest):
"""
(internal) Unjelly a method.
"""
im_name = rest[0]
im_self = self.unjelly(rest[1])
im_class = self.unjelly(rest[2])
if type(im_class) is not types.ClassType:
raise InsecureJelly("Method found with non-class class.")
if im_name in im_class.__dict__:
if im_self is None:
im = getattr(im_class, im_name)
elif isinstance(im_self, NotKnown):
im = _InstanceMethod(im_name, im_self, im_class)
else:
im = instancemethod(im_class.__dict__[im_name],
im_self,
im_class)
else:
raise TypeError('instance method changed')
return im
示例4: addStr
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def addStr(anInstance):
anInstance.__str__ = new.instancemethod(__str__, anInstance, anInstance.__class__)
# Test it
示例5: _do_set_current_user
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def _do_set_current_user(user_fun):
setattr(_thread_locals, USER_ATTR_NAME, instancemethod(user_fun, _thread_locals, type(_thread_locals)))
示例6: AddAdjustResults
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def AddAdjustResults(test_set):
def AdjustResults(self, results):
for values in results.values():
# Add the raw value to be expando'd and store a munged value in score.
values['expando'] = values['raw_score']
values['raw_score'] = int(round(values['raw_score'] / 2.0))
return results
test_set.AdjustResults = new.instancemethod(
AdjustResults, test_set, test_set.__class__)
示例7: make_instancemethod
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def make_instancemethod(function, instance):
return new.instancemethod(function.im_func, instance,
instance.__class__)
示例8: __init__
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def __init__(cls, name, bases, cls_dict):
cls.use_metaclass = 1
def fake_method(self):
pass
new.instancemethod(fake_method, None, cls)
示例9: AddMethod
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def AddMethod(object, function, name = None):
"""
Adds either a bound method to an instance or an unbound method to
a class. If name is ommited the name of the specified function
is used by default.
Example:
a = A()
def f(self, x, y):
self.z = x + y
AddMethod(f, A, "add")
a.add(2, 4)
print a.z
AddMethod(lambda self, i: self.l[i], a, "listIndex")
print a.listIndex(5)
"""
import new
if name is None:
name = function.__name__
else:
function = RenameFunction(function, name)
try:
klass = object.__class__
except AttributeError:
# "object" is really a class, so it gets an unbound method.
object.__dict__[name] = new.instancemethod(function, None, object)
else:
# "object" is really an instance, so it gets a bound method.
object.__dict__[name] = new.instancemethod(function, object, klass)
示例10: get_generic_cmd
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def get_generic_cmd(obj, ui, cliclass=GenericCLI, aliases=None, gbl=None):
"""get a GenericCLI (or other) command set wrapping any class instance
object. The wrapped objects public methods have CLI command counterparts
automatically created."""
import new
from methodholder import MethodHolder
cmd = cliclass(ui, aliases)
if gbl is None:
gbl = globals()
hashfilter = {}
for name in _get_methodnames(obj):
if hasattr(cmd, name):
continue # don't override already defined methods
# all this mess does is introspect the object methods and map it to a CLI
# object method of the same name, with a docstring showing the attributes
# and their default values, and the actual code mirroring the
# _generic_call method in the GenericCLI class.
else:
obj_meth = getattr(obj, name)
if id(obj_meth.im_func) in hashfilter: # filter out aliases
continue
else:
hashfilter[id(obj_meth.im_func)] = True
mh = MethodHolder(obj_meth)
doc = "%s *\n%s" % (mh, obj_meth.__doc__ or "")
code = cliclass._generic_call.func_code
nc = new.code(code.co_argcount, code.co_nlocals, code.co_stacksize,
code.co_flags, code.co_code,
(doc,)+code.co_consts[1:], # replace docstring
code.co_names, code.co_varnames, code.co_filename,
code.co_name, code.co_firstlineno, code.co_lnotab)
f = new.function(nc, gbl, name)
m = new.instancemethod(f, cmd, cliclass)
setattr(cmd, name, m)
cmd._setup(obj, "Object:%s> " % (obj.__class__.__name__,))
return cmd
示例11: get_parser
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def get_parser(document=None, namespaces=0, validate=0, external_ges=1,
logfile=None, doc_factory=POM.new_document):
import xml
if hasattr(xml, "use_pyxml"):
xml.use_pyxml()
import xml.sax.sax2exts
import xml.sax.handler
import new
handler = ContentHandler(document, doc_factory=doc_factory, logfile=logfile)
errorhandler = ErrorHandler(logfile)
# create parser
parser = xml.sax.sax2exts.XMLParserFactory.make_parser()
parser.setFeature(xml.sax.handler.feature_namespaces, namespaces)
parser.setFeature(xml.sax.handler.feature_validation, validate)
parser.setFeature(xml.sax.handler.feature_external_ges, external_ges)
parser.setFeature(xml.sax.handler.feature_external_pes, 0)
parser.setFeature(xml.sax.handler.feature_string_interning, 1)
# set handlers
parser.setContentHandler(handler)
parser.setDTDHandler(handler)
parser.setEntityResolver(handler)
parser.setErrorHandler(errorhandler)
# since the xml API provides some generic parser I can't just
# subclass I have to "patch" the object in-place with this trick.
# This is to a) make the API compatible with the HTMLParser, and b)
# allow specifing the encoding and other headers in the request.
parser.parse_orig = parser.parse
def parse(self, url, data=None, encoding=POM.DEFAULT_ENCODING,
useragent=None, accept=None):
from pycopia.WWW import urllibplus
fo = urllibplus.urlopen(url, data, encoding, useragent=useragent, accept=accept)
if logfile:
from pycopia import UserFile
fo = UserFile.FileWrapper(fo, logfile=logfile)
return self.parse_orig(fo)
parser.parse = new.instancemethod(parse, parser, parser.__class__)
return parser
示例12: get_generic_cmd
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def get_generic_cmd(obj, ui, cliclass=GenericCLI, aliases=None, gbl=None):
"""get a GenericCLI (or other) command set wrapping any class instance
object. The wrapped objects public methods have CLI command counterparts
automatically created."""
import new
from pycopia.methodholder import MethodHolder
cmd = cliclass(ui, aliases)
if gbl is None:
gbl = globals()
hashfilter = {}
for name in _get_methodnames(obj):
if hasattr(cmd, name):
continue # don't override already defined methods
# all this mess does is introspect the object methods and map it to a CLI
# object method of the same name, with a docstring showing the attributes
# and their default values, and the actual code mirroring the
# _generic_call method in the GenericCLI class.
else:
obj_meth = getattr(obj, name)
if id(obj_meth.__func__) in hashfilter: # filter out aliases
continue
else:
hashfilter[id(obj_meth.__func__)] = True
mh = MethodHolder(obj_meth)
doc = "%s *\n%s" % (mh, obj_meth.__doc__ or "")
code = cliclass._generic_call.func_code
nc = new.code(code.co_argcount, code.co_nlocals, code.co_stacksize,
code.co_flags, code.co_code,
(doc,)+code.co_consts[1:], # replace docstring
code.co_names, code.co_varnames, code.co_filename,
code.co_name, code.co_firstlineno, code.co_lnotab)
f = new.function(nc, gbl, name)
m = new.instancemethod(f, cmd, cliclass)
setattr(cmd, name, m)
cmd._setup(obj, "Object:%s> " % (obj.__class__.__name__,))
return cmd
示例13: instancemethod
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def instancemethod(*args):
return args[0]
示例14: save_instancemethod
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def save_instancemethod(self, obj):
""" Save an instancemethod object """
# Instancemethods are re-created each time they are accessed so this will not be memoized
args = (obj.im_func, obj.im_self, obj.im_class)
self.save_reduce(new.instancemethod, args)
示例15: curry1
# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def curry1(func, arg):
return new.instancemethod(func, arg, object)