Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helper function to list properties of a node #317

Open
briot opened this issue Sep 5, 2019 · 1 comment
Open

helper function to list properties of a node #317

briot opened this issue Sep 5, 2019 · 1 comment

Comments

@briot
Copy link

briot commented Sep 5, 2019

When developing with libadalang/python, I have found the following function very useful. I just pass it a node, and it dumps all useful properties on the console. I wonder whether the libadalang developers already have something similar or better that could be made more official... A small part of the complexity here is to work around a few libadalang crashes when calling some of the functions (although I have noticed less and less of these in the last few months :-)

def dump(v):
    """
    Debug only
    """
    import inspect

    methods = set()
    staticmethods = set()
    fields = {}

    for name in dir(v):
        if name in ('p_int_type', 'p_bool_type', 'dump', 'dump_str',
                    'p_universal_int_type', 'p_universal_real_type',
                    'find', 'findall', 'finditer', 'is_a',
                    'p_complete', 'p_gnat_xref', 'p_as_symbol_array',
                    'p_referenced_decl_internal',
                    ):
            continue

        if name.startswith('_'):
            continue

        try:
            value = getattr(v, name)
        except:
            value = '??? Error when computing'

        if inspect.ismethod(value):
            methods.add(name)
        elif inspect.isfunction(value):
            staticmethods.add(name)
        else:
            fields[name] = value

    print("=== Dump: %s" % v)
    print("  inheritance tree: %s" %
            map(lambda n: n.__name__, inspect.getmro(type(v))))
    if methods:
        print("  methods:          %s" % sorted(methods))
    if staticmethods:
        print("  staticmethods:    %s" % sorted(staticmethods))

    for name in sorted(fields):
        value = fields[name]
        if name in ('p_referenced_decl', 'p_referenced_id',
                    'p_is_dot_call', 'p_is_static_expr', 'p_name_is',
                    ):
            try:
                name = name + '()'
                value = value()
            except:
                value = '??? Error when calling'

        try:
            value = str(value)
        except:
            value = "??? Exception when converting to string"

        print('  %-18s: %s' % (name, value))
@raph-amiard
Copy link
Member

That's an interesting function! To be honest, I don't need such a function because I usually explore the structure from an interactive REPL with completion, such as the playground, but I can see the benefits of seeing all the info at once. I will keep this open to see if there's something to be done inside LAL. Thanks for sharing it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants