Code Monkey home page Code Monkey logo

Comments (4)

zmitchell avatar zmitchell commented on August 17, 2024 1

Sounds good, I'll put it together Β πŸ‘

from astor.

pmaupin avatar pmaupin commented on August 17, 2024

Yeah, that function's pretty simple right now -- it only works on top-level functions.

Having said that, you can have functions inside classes inside functions inside functions inside classes inside...

Honestly, I don't even remember my use-case for writing that, but I suspect it had to do with testing the code writer.

We'd certainly accept a patch for a more-functional version of this, but I don't have a use-case at the moment.

Thanks,
Pat

from astor.

zmitchell avatar zmitchell commented on August 17, 2024

I wrote a version of the CodeToAst class for myself that recurses down the AST. Here's basically how it works.

I make a list of AST nodes, block_types, that have a body field (ast.If, ast.For, etc), and thus could contain a function. I also make a list of AST nodes, func_types, that are function definitions (ast.FunctionDef or ast.AsyncFunctionDef).

Then I have a method that descends the AST (note self.asts is equivalent to cache, and self.filename is equivalent to fname):

def _find_funcs(self, parent_ast):
    for item in parent_ast.body:
        if type(item) in self.func_types:
            self.asts[(self.filename, item.lineno)] = item
            self._find_funcs(item)
        elif type(item) in self.block_types:
            self._find_funcs(item)

I haven't tried to port this back to astor yet, but I could do that without much trouble if it sounds like what you're looking for.

from astor.

berkerpeksag avatar berkerpeksag commented on August 17, 2024

@zmitchell that seems like a reasonable approach to me, thank you. I'd be happy to review if you submit a PR.

from astor.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.