0

I am trying to convert an older library that maps dependencies called snakefood over to Python 3. I have 2 parts I can't figure out what to substitute as I can't find documentation on what either function does - from compiler.ast import Discard, Const I can't seem to find an equivalent in Python 3 in the ast library. Here are the function calls from Python 2, see both here being used in isinstance() calls:

    def default(self, node):
        pragma = None
        if self.recent:
            if isinstance(node, Discard):
                children = node.getChildren()
                if len(children) == 1 and isinstance(children[0], Const):
                    const_node = children[0]
                    pragma = const_node.value

        self.accept_imports(pragma)

Apologies for not understanding this stuff, I just learned about AST calls trying to use this library. Much appreciated.

1 Answer 1

1

Discard (took me a while to figure out what it does) is now Expr (though this includes more things than previously)

I got this hint from the source of compiler/codegen.py:

    def visitDiscard(self, node):
        # XXX Discard means it's an expression.  Perhaps this is a bad
        # name.

Const has been replaced with several different types which represent various constants, notably Num, Str, JoinedStr, and a few others. The documentation for all the ast types can be found here.

Sign up to request clarification or add additional context in comments.

3 Comments

So a Discard node that contains only a Const child would be something like a docstring.
@jasonharper I also see it for numbers: >>> compiler.parse('2') Module(None, Stmt([Discard(Const(2))]))
From the documentation for 3.8, different types of constants are now all represented by Constant. docs.python.org/3/library/ast.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.