Hide obsolete methods after tab completion

I would like to control what methods appear when the user uses tabs on the user object in ipython - in particular, I want to hide functions that I have deprecated. I still want these methods to be callable, but I don't want users to see them and start using them if they validate the object. Is it possible?

+3
source share
4 answers

Partial answer for you. I will send an example code and then explain why its only a partial answer.
The code:

class hidden(object): # or whatever its parent class is
    def __init__(self):
        self.value = 4
    def show(self):
        return self.value
    def change(self,n):
        self.value = n
    def __getattr__(self, attrname):
        # put the dep'd method/attribute names here
        deprecateds = ['dep_show','dep_change']
        if attrname in deprecateds:
            print("These aren't the methods you're looking for.")
            def dep_change(n):
                self.value = n
            def dep_show():
                return self.value
            return eval(attrname)
        else:
            raise AttributeError, attrname

, : ( ). ( ), im_class, im_func im_self , . , , , dep'd __getattr__. ( __getattribute__, ), ​​ . ( , ) , if, , , , , , .

:

1) ( ),

import types
return types.MethodType(eval(attrname), self)

return eval(attrname)

self defs. instancemethods ( im_class, im_func im_self ).

2) __getattr__ , ( ) (albiet, , ): __init__ __dir__. :

class hidden(object):
    def __init__(self):
        self.value = 4
        from types import MethodType
        def dep_show(self):
            return self.value
        self.__setattr__('dep_show', MethodType(dep_show, self))
        def dep_change(self, n):
            self.value = n
        self.__setattr__('dep_change', MethodType(dep_change, self))
    def show(self):
        return self.value
    def change(self, n):
        self.value = n
    def __dir__(self):
        heritage = dir(super(self.__class__, self)) # inherited attributes
        hide = ['dep_show', 'dep_change']
        show = [k for k in self.__class__.__dict__.keys() + self.__dict__.keys() if not k in heritage + private]
        return sorted(heritage + show)

, , . , , , "" ( ). , __dir__ dir(hiddenObj) , , IPython, __dict__, .

+4

DeprecationWarning , , , , , .

AST , DeprecationWarning, , C, DeprecationWarning .

0

Source: https://habr.com/ru/post/1738827/


All Articles