-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DM-7199: afwTable's .getX()/.getY() do not appear in dir() #238
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -283,6 +283,27 @@ def asAstropy(self, cls=None, copy=False, unviewable="copy"): | |
) | ||
return cls(columns, meta=meta, copy=False) | ||
|
||
def __dir__(self): | ||
""" | ||
This custom dir is necessary due to the custom getattr below. | ||
Without it, not all of the methods available are returned with dir. | ||
See DM-7199 | ||
""" | ||
def recursive_get_class_dir(cls): | ||
""" | ||
Return a set containing the names of all methods | ||
for a given class *and* all of its subclasses. | ||
""" | ||
result = set() | ||
if cls.__bases__: | ||
for subcls in cls.__bases__: | ||
result |= recursive_get_class_dir(subcls) | ||
result |= set(cls.__dict__.keys()) | ||
return result | ||
|
||
return sorted(set(dir(self.columns)) | set(dir(self.table)) | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know about the set operators (pipe for union, in this case). That's cool. Separately, I think this return should be separated from the above by a line, since it's not part of that inner function. |
||
recursive_get_class_dir(type(self)) | set(self.__dict__.keys())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't you just do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed so, at least in python 3. I'm not sure if this would work in python 2 though? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you're right. Burned by py2 again. |
||
|
||
def __getattr__(self, name): | ||
# Catalog forwards unknown method calls to its table and column view | ||
# for convenience. (Feature requested by RHL; complaints about magic | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess this works, but the use of
__bases__
feels exceptionally clever.Does the MRO matter here at all? Probably not, since it's all going into a set.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the... MRO?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrawls https://utcc.utoronto.ca/~cks/space/blog/python/MethodResolutionOrder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. I don't think this matters, because it's all going into a set, and ultimately lands in a sorted list that excludes duplicates. And I choose to take "exceptionally clever" as a compliment, largely on Russell's behalf.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that you are often much more clever writing code than trying to understand it later. That was the nature of my comment.