Produce a dictionary of an object's attributes. Builds on dir2 by checking that a getattr() call actually succeeds.
(obj)
| 56 | return show_all or str.startswith("__") or not str.startswith("_") |
| 57 | |
| 58 | def dict_dir(obj): |
| 59 | """Produce a dictionary of an object's attributes. Builds on dir2 by |
| 60 | checking that a getattr() call actually succeeds.""" |
| 61 | ns = {} |
| 62 | for key in dir2(obj): |
| 63 | # This seemingly unnecessary try/except is actually needed |
| 64 | # because there is code out there with metaclasses that |
| 65 | # create 'write only' attributes, where a getattr() call |
| 66 | # will fail even if the attribute appears listed in the |
| 67 | # object's dictionary. Properties can actually do the same |
| 68 | # thing. In particular, Traits use this pattern |
| 69 | try: |
| 70 | ns[key] = getattr(obj, key) |
| 71 | except AttributeError: |
| 72 | pass |
| 73 | return ns |
| 74 | |
| 75 | def filter_ns(ns, name_pattern="*", type_pattern="all", ignore_case=True, |
| 76 | show_all=True): |
no test coverage detected
searching dependent graphs…