当前位置: 首页>>代码示例>>Python>>正文


Python new.instancemethod方法代码示例

本文整理汇总了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 
开发者ID:kenorb-contrib,项目名称:BitTorrent,代码行数:22,代码来源:jelly.py

示例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. 
开发者ID:singhj,项目名称:locality-sensitive-hashing,代码行数:7,代码来源:pyversion.py

示例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 
开发者ID:kuri65536,项目名称:python-for-android,代码行数:23,代码来源:jelly.py

示例4: addStr

# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def addStr(anInstance):
    anInstance.__str__ = new.instancemethod(__str__, anInstance, anInstance.__class__)

# Test it 
开发者ID:ActiveState,项目名称:code,代码行数:6,代码来源:recipe-52192.py

示例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))) 
开发者ID:crowdata,项目名称:crowdata,代码行数:4,代码来源:middleware.py

示例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__) 
开发者ID:elsigh,项目名称:browserscope,代码行数:11,代码来源:test_result.py

示例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__) 
开发者ID:singhj,项目名称:locality-sensitive-hashing,代码行数:5,代码来源:pyversion.py

示例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) 
开发者ID:coin3d,项目名称:pivy,代码行数:7,代码来源:Memoize.py

示例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) 
开发者ID:coin3d,项目名称:pivy,代码行数:32,代码来源:Util.py

示例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 
开发者ID:kdart,项目名称:pycopia,代码行数:38,代码来源:CLI.py

示例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 
开发者ID:kdart,项目名称:pycopia,代码行数:39,代码来源:POMparse.py

示例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 
开发者ID:kdart,项目名称:pycopia,代码行数:38,代码来源:CLI.py

示例13: instancemethod

# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def instancemethod(*args):
        return args[0] 
开发者ID:lrq3000,项目名称:pyFileFixity,代码行数:4,代码来源:compat.py

示例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) 
开发者ID:ActiveState,项目名称:code,代码行数:7,代码来源:recipe-572213.py

示例15: curry1

# 需要导入模块: import new [as 别名]
# 或者: from new import instancemethod [as 别名]
def curry1(func, arg):
    return new.instancemethod(func, arg, object) 
开发者ID:ActiveState,项目名称:code,代码行数:4,代码来源:recipe-229472.py


注:本文中的new.instancemethod方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。