Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # py-to-js.py
- # SEE snippet.host/bccvur for the entire code since pb won't allow it all to be posted here
- # work in progress... mainly for beginners and for JS versions of Tk projects
- """
- Create an application that will convert a Python script to be a Javascript code
- """
- from tkinter import *
- from tkinter import filedialog
- from tkinter import messagebox
- root = Tk()
- root.title("py-to-js.py")
- root.geometry("400x400")
- import re
- def convert_to_js(file_name):
- with open(file_name, 'r') as f:
- content = f.read()
- # """This is a
- #. multiline comment.""" -> /* This is a
- #. s = """This is a string""" -> var s = "This is a string";
- content = re.sub(r'(.*) = \"\"\"(.*)\"\"\"', r'var \1 = "\2";', content)
- content = re.sub(r'\"\"\"(.*)\"\"\"', r'/* \1 */', content, flags=re.DOTALL)
- # '''This is a
- #. multiline comment.''' -> /* This is a
- #. multiline comment. */
- content = re.sub(r'\'\'\'(.*)\'\'\'', r'/* \1 */', content, flags=re.DOTALL)
- # # This is a comment. -> // This is a comment.
- content = re.sub(r'# (.*)', r'// \1', content)
- # s = '''This is a string''' -> var s = "This is a string";
- content = re.sub(r'(.*) = \'\'\'(.*)\'\'\'', r'var \1 = "\2";', content)
- # datetime.datetime.now() -> new Date()
- content = re.sub(r'datetime.datetime.now\(\)', r'new Date()', content)
- # datetime.datetime.now().strftime('%H:%M:%S') -> new Date().toISOString().slice(11, 19)
- content = re.sub(r'datetime.datetime.now\(\)\.strftime\(\'%H:%M:%S\'\)', r'new Date().toISOString().slice(11, 19)', content)
- # datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S') -> new Date().toISOString().slice(0, 19)
- content = re.sub(r'datetime.datetime.now\(\)\.strftime\(\'%Y/%m/%d %H:%M:%S\'\)', r'new Date().toISOString().slice(0, 19)', content)
- # datetime.datetime.now().strftime('%Y/%m/%d') -> new Date().toISOString().slice(0, 10)
- content = re.sub(r'datetime.datetime.now\(\)\.strftime\(\'%Y/%m/%d\'\)', r'new Date().toISOString().slice(0, 10)', content)
- # # -> //
- content = re.sub(r'#(.*)', r'//\1', content)
- # '#{:02x}{:02x}{:02x}'.format(r, g, b) -> '#' + r.toString(16) + g.toString(16) + b.toString(16)
- content = re.sub(r'\'#\{:02x\}\{:02x\}\{:02x\}\'.format\((.*), (.*), (.*)\)', r'\'#\' + \1.toString(16) + \2.toString(16) + \3.toString(16)', content)
- # ', '.join(x) -> x.join(', ')
- content = re.sub(r'(.*).join\((.*)\)', r'\1.join(\2)', content)
- # '{:.2f}'.format(x) -> x.toFixed(2)
- content = re.sub(r'\'\{:.2f\}\'.format\((.*)\)', r'\1.toFixed(2)', content)
- # '{:.1f}'.format(x) -> x.toFixed(1)
- content = re.sub(r'\'\{:.1f\}\'.format\((.*)\)', r'\1.toFixed(1)', content)
- # '{}'.format(x) -> x.toString()
- content = re.sub(r'\'\{\}\'.format\((.*)\)', r'\1.toString()', content)
- # (1, 2, 3) -> [1, 2, 3]
- content = re.sub(r'\((.*)\)', r'[\1]', content)
- ...
- ...
- ...
- ... SEE snippet.host/bccvur
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement