add/delete str/dt/cat dynamically from dir (fix for #9627) by mortada · Pull Request #9910 · pandas-dev/pandas (original) (raw)

Conversation

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters

[ Show hidden characters]({{ revealButtonHref }})

@mortada

@jreback this PR makes it easier to add/delete items from __dir__. The .str/.dt/.cat are now only present in __dir__ when they are appropriate types.

However, the IPython tab completion does not seem to only source the list from __dir__, and therefore even if an item is removed from __dir__ it is still showing up in tab completion for me.

@shoyer I'd probably need some help/feedback with this. Here's a related ticket #9617

Thanks.

jreback

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of these accessors (cat,dt,str) are mutually exclusive, so should set in a single routine. IOW, maybe change delete all, then add back in an if-then

shoyer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to actually make a separate method _check_str_accessor(). You could simply do:

try:
    self.str
except AttributeError:
    # ...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that to avoid having to instantiate StringMethods(self) when it is not needed, but I agree it's probably premature optimization

@shoyer

Some thoughts on the design here: we could add a class attribute _possible_accessors which corresponds to a set of all possibly accessors (e.g., Series._possible_acccessors = set(['cat', 'str', dt'])). Then we can have something like the following generic method defined on PandasObject:

def _dir_deletions(self): deletions = set() for accessor in self._possible_accessors: try: getattr(self, accessor) except AttributeError: deletions.add(accessor) return deletions

@mortada

@shoyer I like this idea! I noticed that there's already a _accessors attribute under Series though

_accessors = frozenset(['dt', 'cat', 'str'])

but it's not obvious to me how this is being used.

@shoyer

With regards to IPython auto-completion, this does seem trickier than I thought. Take a look at this example:

class A(object): something = 'asdf'

def __dir__(self):
    return ['other']

class B(object): def init(self): self.something = 'asdf'

def __dir__(self):
    return ['other']

a = A() b = B()

Autocomplete on a still picks up something, even though we excluded it from __dir__ (on b, it's excluded). I'm guessing IPython uses dir(A) somehow.... might be worth asking over there, since I'm pretty puzzled at this point :).

@shoyer

Yes, looks like _accessors is probably the appropriate attribute to use then :). You'll have to do some searching to figure out how it's currently used.

@mortada

@shoyer updated to incorporate your suggestions, now using self._accessors
@jreback updated to make use of the fact that .str/.dt/.cat are mutually exclusive

@jreback

lgtm

pls add a release note (use the pr number as the issue)

shoyer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can equivalently use the built-in function dir here instead.

@shoyer

LGTM, though you might test the built-in function dir directly instead of using __dir__

@mortada

@mortada

@shoyer that makes sense, it's updated

@shoyer

OK, looks good to me! Please ping when it Travis is green.

Too bad this won't work for IPython auto-complete yet... but at least they're working on it.

@mortada

travis is green now

yeah I hope the IPython auto-completion will work soon

shoyer added a commit that referenced this pull request

Apr 21, 2015

@shoyer

add/delete str/dt/cat dynamically from dir (fix for #9627)

@shoyer

@mortada

great news - the issue has been closed on the IPython side, we should soon have the tab completion working

Labels