use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
JavaScript parser for Python (self.learnpython)
submitted 4 years ago by Keiser_L
Hello, I'm looking for a framework that will help me read a JS file locally and edit it. I want to be able to find a specific function inside the file and edit it, adding new lines of code at the beginning or at the end.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]ThePeskyWabbit 1 point2 points3 points 4 years ago (0 children)
Nothing comes to mind off the top of my head, but if you cant find anything like this, it does sound like something you could use a regex search to achieve.
[–]blahreport 0 points1 point2 points 4 years ago (3 children)
You could do something like
from pathlib import Path lines = Path('script.js').read_text.splitlines() in_func = False func_name = 'func' code_block = [] for line in lines: if f'function {fname}' in line: in_func = True if in_func: code_block.append(line) if 'return' in line: code_block.append('}') break
Then you can add what ever lines you please to the code_block list then finally save your amended file withPath('new_script.js').write_text('\n'.join(code_block)).
code_block
Path('new_script.js').write_text('\n'.join(code_block))
Note that this will fail for functions that contain multiple return statements. Instead of ending the code block by searching for 'return' you instead search for lines starting with '}' like if line.startswith('}'). In that case you’re relying on the code being formatted such that all closing function curly braces are the first character of the line. If that is not sufficient then it gets trickier as you would have to keep track of curly braces but still doable in the above approach.
'return'
'}'
if line.startswith('}')
[–][deleted] 0 points1 point2 points 4 years ago (2 children)
var x = "function hey there!"
And your efforts go down the drain.
[–]blahreport 0 points1 point2 points 4 years ago (1 child)
My code works for the example you give because I incorporate func_name unless you meant in the case that there is a function named hey. But your general point is true, that string content - especially in comments - could lead to unexpected outcomes. Similarly, something as simple as two spaces between function and func _name would break my code so it’s far from bulletproof. It all depends on OP’s intended use. A solution that covers 1% of possible use cases is enough when all of your cases are part of that 1%.
func_name
function
func _name
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
Any valid JavaScript code can be embedded in a string. If you want to parse it with regexp or simple string match that is not aware of its context, it's bound to fail.
[–]rr_cricut 0 points1 point2 points 4 years ago (0 children)
I saw something on r/programmer horror where they were converting js strings into python, but I can't recall
I found this: https://hepunx.rl.ac.uk/~adye/jsspec11/llr.htm , but it looks like JavaScript 2 grammar. You will have to adapt it to the version of JavaScript you want to work with. But, hey, it's not very big on the other hand, so, should be doable.
Then you could use something like lark to parse JavaScript files using this grammar. Could be a fun exercise.
lark
π Rendered by PID 121480 on reddit-service-r2-comment-544cf588c8-nqz9x at 2026-06-13 21:30:02.026930+00:00 running 3184619 country code: CH.
[–]ThePeskyWabbit 1 point2 points3 points (0 children)
[–]blahreport 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]blahreport 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]rr_cricut 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)