TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Show HN: Python Source Code Refactoring Toolkit via AST

110 pointsby treesciencebotalmost 4 years ago

8 comments

hsbauauvhabzbalmost 4 years ago
I’ve been thinking about a similar problem I have on a regular basis: I audit a lot of code in many languages, sometimes I’m not even able to build solutions. my toolset pretty much includes git and rudimentary regex. A recent example is a PHP function which is insecure when it’s used at least 3 times in a string concat.<p>Any suggestion on an AST ‘framework’ that would help me parse code easier? Language specific or generic, even if it only sort-of fits (I don’t even know what I want).<p>Automated Code analysers exist, but I want something manual.
评论 #28033504 未加载
评论 #28033060 未加载
评论 #28032826 未加载
bmc7505almost 4 years ago
I wonder if something like this exists for other languages? Most rewriting&#x2F;refactoring tools make too many assumptions about the surrounding project. Something simple which supports pattern matching against a CST subtree and invertible parsing&#x2F;unparsing would be the ideal polyglot macro-system. Suppose I want to find and rename all variables with a certain type or rewrite an expression of a certain shape in multiple languages at once (e.g. Java&#x2F;JavaScript&#x2F;C&#x2F;C++). Something between a regular expressions (too brittle) and a full parser would be great a great solution to have for this use case.
评论 #28047286 未加载
gratonalmost 4 years ago
A similar type project. Though I haven&#x27;t seen much activity recently:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;facebookincubator&#x2F;Bowler" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;facebookincubator&#x2F;Bowler</a>
评论 #28029388 未加载
dreamer7almost 4 years ago
Would this be a good tool to attempt simple transpiling of typescript code to python?<p>For my use-cases, not much changes between the two - 1. Replace opening and closing braces with colon. 2. Remove semi colons 3. Replace function with def 4. Remove type definitions 5. Replace array manipulation functions with python equivalents. 6. Remove tokens like const, let and var
评论 #28033682 未加载
milkbikisalmost 4 years ago
Nice, I&#x27;ve always thought Python could do better by adopting source transformation tools like babel. I implemented something similar using lib2to3, which preserves whitespace and comments more accurately (than the ast module): <a href="https:&#x2F;&#x2F;github.com&#x2F;banga&#x2F;prefactor" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;banga&#x2F;prefactor</a>
评论 #28029350 未加载
dreamer7almost 4 years ago
Would this be a good tool to attempt simple transpiling of typescript code to python?<p>For my use-cases, not much changes between the two -<p>1. Replace opening and closing braces with colon.<p>2. Remove semi colons<p>3. Replace function with def<p>4. Remove type definitions<p>5. Replace array manipulation functions with python equivalents.<p>6. Remove tokens like const, let and var
评论 #28047235 未加载
TekMolalmost 4 years ago
The one thing I hope for when it comes to Python is that PyPy becomes the default runtime everywhere.<p>CPython is so unbelievably slow when compared to languages like PHP and Javascript.<p>When using PyPy, I keep encountering road bumps because many tools are still expecting that one uses CPython.<p>Dear world - please accelerate the conversion to PyPy!
评论 #28027335 未加载
评论 #28027301 未加载
评论 #28029067 未加载
评论 #28031393 未加载
boxedalmost 4 years ago
You should check out parso which is a fully roundtrip-able AST. That way your tool won&#x27;t throw away comments and formatting.
评论 #28038663 未加载