Script To List Any Python Object

39 views
Skip to first unread message

tbp1...@gmail.com

unread,
Mar 15, 2022, 11:26:34 PM3/15/22
to leo-editor
Here is a utility script - in the attached outline - that will list the contents of an object's __dict__ in a fairly readable way including the docstrings.  You can use it for listing the methods, functions, and classes of c or g, or of any other Python object.  In the outline, the script inspects the contents of the ViewRenderedController3 of the viewrendered3 plugin, but you can use it for c, g, or anything else.

I actually developed it to view c and g to help me with other development projects.  It's pretty brute force, and I suppose it could be done better using the inspect module, but I was concentrating on formatting the docstrings.  You can use the format_docstr() function to format other docstrings, including in actual python files that have been read but not opened or imported - for example, Leo plugin files.

In the outline, I copy the output to the clipboard.  That's because it tends to be fairly long, and it's better to paste it into a node or another editor than to try to print it to the log pane or the console.

I won't pretend this is bullet proof or production ready, but it is useful as is.

Here is a sample output for a commander (lines may be wrapped here - could be more readable in a page with wider lines):

_currentPosition  class 'leo.core.leoNodes.Position'  
_prev_next  class 'leo.plugins.nav_qt.NavController'  
_style_deltas  class 'collections.defaultdict'  
  defaultdict(default_factory=None, /, [...]) --> dict with
  default factory The default factory is called without
  arguments to produce a new value when a key is not present,
  in __getitem__ only. ...

_topPosition    
abbrevCommands  class 'leo.commands.abbrevCommands.AbbrevCommandsClass'  
     A class to handle user-defined abbreviations.   See
  apropos-abbreviations for details.

abbrev_place_end  class 'str'  
abbrev_place_start  class 'str'  
abbrev_subst_end  class 'str'  
abbrev_subst_env  class 'dict'  
abbrev_subst_start  class 'bool'  
active_stylesheet  class 'str'  
atFileCommands  class 'leo.core.leoAtFile.AtFile'  A class implementing the atFile subcommander.
at_root_bodies_start_in_doc_mode  class 'bool'  
autoindent_in_nocolor  class 'bool'  
bufferCommands  class 'leo.commands.bufferCommands.BufferCommandsClass'  
     An Emacs instance does not have knowledge of what is
  considered a   buffer in the environment.

changed  class 'bool'  
chapterCommands    
chapterController  class 'leo.core.leoChapters.ChapterController'  A per-commander controller that manages chapters and related nodes.
collapse_nodes_after_move  class 'bool'  
collapse_on_lt_arrow  class 'bool'  
command_count  class 'int'  
command_function  class 'function'  
  Execute a *Leo* script, written in python.     Keyword
  args:     args=None        Not None: set
  script_args in the execution environment.     p=None
  Get the script from p.b, unless script is given. ...



list_obj.leo

Edward K. Ream

unread,
Mar 16, 2022, 3:16:59 PM3/16/22
to leo-editor
On Tue, Mar 15, 2022 at 10:26 PM tbp1...@gmail.com <tbp1...@gmail.com> wrote:
Here is a utility script - in the attached outline - that will list the contents of an object's __dict__ in a fairly readable way including the docstrings. 

Thanks for this.  An alternative might be g.printObj(o.__dict__).

Edward

tbp1...@gmail.com

unread,
Mar 16, 2022, 6:35:05 PM3/16/22
to leo-editor
Thanks.  Yet another case of "Who would have known?".  I think that command shows too much.  I was trying for more of a balance between readability and information.  That's a hard thing to get right!

Edward K. Ream

unread,
Mar 17, 2022, 7:21:43 AM3/17/22
to leo-editor
On Wed, Mar 16, 2022 at 5:35 PM tbp1...@gmail.com <tbp1...@gmail.com> wrote:
Thanks.  Yet another case of "Who would have known?".  I think that command shows too much.  I was trying for more of a balance between readability and information.  That's a hard thing to get right!

It might be useful to call g.printObj for only selected fields of o.__dict__.

Edward
Reply all
Reply to author
Forward
0 new messages