Tip to filter results of a dir command
Answers
-
To check all available methods and properties of an object we often use the dir() command.
For example, to check what's available for an object referenced by "connect", we'll use:
dir(connect)
If the returned list is long, it is very useful to filter it by some keywords that we're interested in. For this, the filter() method can be used. For example, to get all methods and properties that have a "Geometry" keyword in their name, use:
filter(lambda x: ("Geometry" in x), dir(connect))
4 -
I had created a button sometime back on this using what Pernelle mentioned above. Could be useful tool too.
All you need to do is import the button in mechanical to use this.
How to use:
- Click on an object in mechanical tree
- Click on this button
- Enter a keyword to search for methods and properties
- Returns list of all methods and properties available.
button link below:
1 -
As an addendum to Pernelle's answer:
In Python 3, rather than using
filter
andlambda
it is more pythonic (and faster) to use a list comprehension. So,filter(lambda x: ("Geometry" in x), dir(connect))
becomes
filtered_list = [x for x in dir(connect) if "Geometry" in x]
However in ACT and ironpython it is usually better to use
map
/filter
/reduce
andlambda
, as explained here.This is because in Python 2 the list comprehension will overwrite the
x
variable if it is already in use, or will enter it into the namespace if it is not. In Python 3 this is not the case, andx
remains a local variable inside the comprehension.0