icecream icon indicating copy to clipboard operation
icecream copied to clipboard

Dump object structure

Open abitrolly opened this issue 3 years ago • 13 comments

Is it possible for icecream to dump object properties and their values?

abitrolly avatar Nov 14 '21 21:11 abitrolly

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

alexmojaki avatar Nov 14 '21 22:11 alexmojaki

Yep. I could do this with a little bit of tweaking.

from beeprint import pp
pp(details, instance_repr_enable=False, max_depth=2)

abitrolly avatar Nov 14 '21 23:11 abitrolly

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.

image

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

abitrolly avatar Nov 15 '21 11:11 abitrolly

I thought you needed the contents of an object? I don't understand what you're showing here.

alexmojaki avatar Nov 15 '21 12:11 alexmojaki

Yes, I needed to find where the line "show logs from services" is stored inside of the object.

abitrolly avatar Nov 15 '21 12:11 abitrolly

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?

gruns avatar Nov 19 '21 22:11 gruns

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 avatar Nov 20 '21 08:11 abitrolly

@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 avatar Jan 07 '22 22:01 guywilsonjr

@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 avatar Jan 08 '22 00:01 abitrolly

@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 avatar Jan 08 '22 01:01 guywilsonjr

@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 avatar Jan 08 '22 07:01 abitrolly

@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.mypropertyit 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

Python Debugging

guywilsonjr avatar Jan 08 '22 08:01 guywilsonjr

Limiting recursion doesn't seem to be hard if it is possible maintain a list of referenced objects.

abitrolly avatar Jan 08 '22 18:01 abitrolly