简体   繁体   English

自定义dict类的ipython选项卡完成

[英]ipython tab completion for custom dict class

I've been using the following in my code: 我一直在我的代码中使用以下内容:

class Structure(dict,object):
""" A 'fancy' dictionary that provides 'MatLab' structure-like
referencing. 

"""
def __getattr__(self, attr):
    # Fake a __getstate__ method that returns None
    if attr == "__getstate__":
        return lambda: None
    return self[attr]

def __setattr__(self, attr, value):
    self[attr] = value

def set_with_dict(self, D):
    """ set attributes with a dict """
    for k in D.keys():
        self.__setattr__(k, D[k])

All in all it works for my purposes, but I've noticed that only way tab completion works is for methods in another custom class that inherited from Structure, and not for attributes. 总而言之,它适用于我的目的,但我注意到,只有方法选项卡完成才能用于继承自Structure的另一个自定义类中的方法,而不是属性。 I also did this test, and I find the result a little strange: 我也做了这个测试,我发现结果有点奇怪:

In [2]: d = Structure()
In [3]: d.this = 'that'
In [4]: d.this
Out[4]: 'that'
In [5]: d.th<tab>
NOTHING HAPPENS

In [6]: class T():
   ...:     pass
   ...: 

In [7]: t = T()
In [8]: t.this = 'cheese'
In [9]: t.th<tab>
COMPLETES TO t.this
Out[9]: 'cheese'

What would I need to add to my class to get the tab completion to work for the attributes? 我需要将哪些内容添加到我的类中以使标签完成为属性工作?

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

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