You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
(I thought this was already an issue, but I checked and it seems it only existed in my head)
I want a way to pretty-print pertinent attributes of an object, like vars(...) but more intelligent - in particular it should try to inspect __slots__ if __dict__ is not available.
This might be as simple as sugar around the following POC, but there are probably edge cases I haven't thought about.
I guess we should expose this via debug.inspect().
Rough demo code of what I mean
from __future__ importannotationsfromdevtoolsimportdebugignored_attributes= {
'__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__',
'__init__', '__init_subclass__', '__le__', '__lt__', '__module__',
'__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
'__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '__slots__',
}
classDebugInspect:
__slots__= ('obj',)
def__init__(self, obj: Any):
self.obj=objdef__pretty__(self, fmt: Callable[[Any], Any], **kwargs: Any) ->Generator[Any, None, None]:
yieldself.obj.__class__.__name__+'('yield1# use __dict__ if possible to maintain order, also should be slightly fasterobj_dict=getattr(self.obj, '__dict__', None)
ifobj_dictisnotNone:
forname, valueinobj_dict.items():
yieldname+'='yieldfmt(value)
yield','yield0else:
fornameindir(self.obj):
ifnamenotinignored_attributes:
yieldname+'='yieldfmt(getattr(self.obj, name))
yield','yield0yield-1# closing bracket to keep it looking a bit like pythonyield')'classFoo:
def__init__(self):
self.x=1self.y=2self._private=3self.__custom_dunder__=4classBar:
__slots__='x', 'y', '_private', '__custom_dunder__'def__init__(self):
self.x=1self.y=2self._private=3self.__custom_dunder__=4f=Foo()
debug(DebugInspect(f))
b=Bar()
debug(DebugInspect(b))
(I thought this was already an issue, but I checked and it seems it only existed in my head)
I want a way to pretty-print pertinent attributes of an object, like
vars(...)
but more intelligent - in particular it should try to inspect__slots__
if__dict__
is not available.This might be as simple as sugar around the following POC, but there are probably edge cases I haven't thought about.
I guess we should expose this via
debug.inspect()
.Rough demo code of what I mean
prints:
The text was updated successfully, but these errors were encountered: