icecream
icecream copied to clipboard
Dump object structure
Is it possible for icecream
to dump object properties and their values?
ic(obj.__dict__)
should work for most classes, but it will only give your the direct attributes, not the full structure recursively.
Off the top of my head, the only library I can think of that directly prints the full structure of a specific object is https://github.com/panyanyany/beeprint
Yep. I could do this with a little bit of tweaking.
from beeprint import pp
pp(details, instance_repr_enable=False, max_depth=2)
Actually was not able to solve my problem with beeprint
.
I needed to find the line "show logs" in argparse
subparsers and I couldn't find it.
So I used https://github.com/ionelmc/python-hunter instead.
The class inheritance of argparse
is kind of crazy for Python.
So the line appeared in argparse._SubParsersAction._ChoicesPseudoAction
object which for some reason was not found/expanded by beeprint
.
Hunted.
/usr/lib64/python3.10/argparse.py:623 line if params.get('choices') is not None:
/usr/lib64/python3.10/argparse.py:626 line return self._get_help_string(action) % params
/usr/lib64/python3.10/argparse.py:652 call => _get_help_string(self=<argparse.RawTextHelpFormatter object at 0x7f00207c5210>,
action=<argparse._SubParsersAction._ChoicesPseudoAction object at 0x7f00207c58d0>)
/usr/lib64/python3.10/argparse.py:653 line return action.help
/usr/lib64/python3.10/argparse.py:653 return <= _get_help_string: 'show logs from services'
/usr/lib64/python3.10/argparse.py:626 return <= _expand_help: 'show logs from services'
https://github.com/ionelmc/python-hunter
I thought you needed the contents of an object? I don't understand what you're showing here.
Yes, I needed to find where the line "show logs from services" is stored inside of the object.
im lost here, too 🙂
@abitrolly does ic(obj.__dict__)
suffice for your needs?
if not, can you provide an example object and example output of what you'd like ic()
to output?
ic(obj.__dict__)
is not recursive.
if not, can you provide an example object and example output of what you'd like ic() to output?
It is theparser
object here https://github.com/containers/podman-compose/blob/1f989ed133f53ac6db61f45e0271c02bbce862bc/podman_compose.py#L1218 and I need to see sufficient details to know where the line "show log s from services" is stored. It is present in command line help.
@abitrolly Are you only looking for where in code it comes from? If so I found a spot. Though I might be somewhat confused on the exact ask
https://github.com/containers/podman-compose/blob/1f989ed133f53ac6db61f45e0271c02bbce862bc/podman_compose.py#L1596
@guywilsonjr I was not clear - I needed to see where the line "show logs from services" was actually used, and how it passed from where it is stored to the screen. I already found it, but thanks anyway.
@abitrolly I see. I think that would be tracing the usage of "show logs from services". I know @alexmojaki has quite a few projects of various capabilities that almost surely could do that. Anyways I think this can be closed. I'm glad it worked out
@guywilsonjr if hunter
could recursively dump the contents of the object in a visual form that is suitable to diffing, it could help me faster find changes every time the variable is accessed, and see which function does what.
I am used to things like Jupiter https://github.com/breuleux/hrepr and Spyder https://docs.spyder-ide.org/current/panes/variableexplorer.html and I was missing variable exploration UX in the places that hunter
found.
@abitrolly There are multiple issues to doing that. The biggest thing is unbounded recursion. That type of functionality is highly discouraged and doesn't exist in tracing libraries as that design has many pitfalls.
if you can use Jupyter or Spyder or even an IDE like PyCharm, then those apps are the best for your purpose. This is the best-case scenario for your purpose.
Ex) a=object();a.myproperty=a
If you recursively evaluate(dump attributes) a.myproperty
it will loop forever.
Ex) a=object();b=object();a.myproperty=b;b.myproperty=a
If you can't use any visual program, it gets more complicated and you would likely need to be familiar with or able to use debuggers.
In python-hunter
, You should be able to use.
See: VarsPrinter
Limiting recursion doesn't seem to be hard if it is possible maintain a list of referenced objects.