简体   繁体   English

如何在ipython中允许自定义Class参数制表符完成?

[英]How to allow custom Class parameter to be tab-completed in ipython?

for example, if I have the following classes 例如,如果我有以下课程

class Part(object):
    def __init__(self,part_number,description,rev=None,color=None):
        self.dims = {}
        self.dim_id = 0
        self.rev = rev
        if type(color) is tuple:
            self.color=(color[0]/255.,color[1]/255.,color[2]/255.)
        else:
            self.color = color

     def add_dimension(self,description,value,tol,tol_type = 'sym',dwg_sheet = None, dwg_zone = None,quality = 3):
        self.dims[description] = Dimension(description=description,part=self,value=value,tol=tol,tol_type=tol_type,quality=quality,dwg_sheet=None,dwg_zone=None)


    def __getattr__(self, description):
        return self.dims[description]

class Dimension(object):
    def __init__(self,part,value,tol,tol_type = 'sym', dwg_sheet = None, dwg_zone = None, quality = 3, description = None):
        self.value = value
        self.part=part
        self.tol = tol
        self.tol_type = tol_type
        self.description = description
        self.quality = quality
        self.sigma = self.tol/float(quality)


    def __str__(self):
    return self.description

Then I run the following code: 然后我运行以下代码:

a = Part('pn','this is a part')
a.add_dimension('this_is_a_dimension',1.00,0.05)
print a.this_is_a_dimension

It returns: 它返回:

this_is_a_dimension

the problem is that when I try to do tab completion after typing " a. ", I get only the following options 问题是当我尝试输入“ a. ”后执行制表符补全时,我仅获得以下选项

a.add_dimension
a.color
a.dim_id
a.dims
a.rev

I would like to be able to tab-complete to see my new parameter. 我希望可以使用制表符完成以查看新参数。 It would be similar to how column names in Pandas Dataframes work. 这将类似于Pandas Dataframes中列名的工作方式。

Any thoughts? 有什么想法吗?

You can. 您可以。 You must implement a __dir__() method for your class, as discussed here . 您必须实现一个__dir__()方法的类,如讨论在这里

This is an example of it working: 这是它工作的一个示例:

class bar(object):
    def __init__(self,):
        self.a = 4
        self._morelements=['b']
    def __dir__(self):
        return sorted(set(dir(type(self)) + list(self.__dict__) + self._morelements))

foo=bar()

Now if you write foo. 现在,如果您编写foo. and press TAB in Ipython you will have as available members both a and b , although b does not exist. 并在Ipython中按TAB键,虽然b不存在,但您将同时拥有ab这两个成员。

The code for __dir__() and a discussion of the limitations of this approach is here . __dir__()的代码和这种方法的局限性讨论在这里 In your case, you want to add description to the list self._morelements inside the function add_dimension() . 在您的情况下,您想在add_dimension()函数内部的列表self._morelements添加description

EDIT: And of course you also need to initialize self._morelements=[] in the __init__() , and add the __dir__() function of the example above to your class Part 编辑:当然,您还需要在__init__()初始化self._morelements=[] ,并将上述__dir__()函数添加到类Part

Python is interpreted. Python被解释。 So, it only knows about code it has already interpreter. 因此,它只知道它已经具有解释器的代码。 An example of this is: if you try to run the code you posted, you will get an error at the line: 这样的一个示例是:如果您尝试运行发布的代码,则将在此行收到错误消息:

self.dims[description] = Dimension(description=description,part=self,value=value,tol=tol,tol_type=tol_type,quality=quality,dwg_sheet=None,dwg_zone=None)

the error: global name 'Dimension' is not defined . 错误: 未定义全局名称“ Dimension” That's because you define the class Dimension after. 那是因为您在之后定义了Dimension类。 At the time the interpreter reach that line, the class Dimension does not exists. 解释器到达该行时,类别Dimension不存在。

this_is_a_dimension attribute of a only exists at run time and after the line: this_is_a_dimension的属性a只存在于运行时间和行之后:

a.add_dimension('this_is_a_dimension',1.00,0.05)

For accomplish what you want, you'll need some IDE that interprets the python code at edit time. 为了完成您想要的,您将需要一些IDE在编辑时解释python代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM