Advertisement
here2share

# py-to-js.py # SEE snippet.host/bccvur

Sep 13th, 2022
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.85 KB | None | 0 0
  1. # py-to-js.py
  2. # SEE snippet.host/bccvur for the entire code since pb won't allow it all to be posted here
  3. # work in progress... mainly for beginners and for JS versions of Tk projects
  4.  
  5. """
  6. Create an application that will convert a Python script to be a Javascript code
  7. """
  8.  
  9.  
  10. from tkinter import *
  11. from tkinter import filedialog
  12. from tkinter import messagebox
  13.  
  14. root = Tk()
  15. root.title("py-to-js.py")
  16. root.geometry("400x400")
  17.  
  18. import re
  19. def convert_to_js(file_name):
  20.     with open(file_name, 'r') as f:
  21.         content = f.read()
  22.         #  """This is a
  23.         #. multiline comment.""" -> /* This is a
  24.         #. s = """This is a string""" -> var s = "This is a string";
  25.         content = re.sub(r'(.*) = \"\"\"(.*)\"\"\"', r'var \1 = "\2";', content)
  26.         content = re.sub(r'\"\"\"(.*)\"\"\"', r'/* \1 */', content, flags=re.DOTALL)
  27.         #  '''This is a
  28.         #. multiline comment.''' -> /* This is a
  29.         #. multiline comment. */
  30.         content = re.sub(r'\'\'\'(.*)\'\'\'', r'/* \1 */', content, flags=re.DOTALL)
  31.         # # This is a comment. -> // This is a comment.
  32.         content = re.sub(r'# (.*)', r'// \1', content)
  33.         # s = '''This is a string''' -> var s = "This is a string";
  34.         content = re.sub(r'(.*) = \'\'\'(.*)\'\'\'', r'var \1 = "\2";', content)
  35.         # datetime.datetime.now() -> new Date()
  36.         content = re.sub(r'datetime.datetime.now\(\)', r'new Date()', content)
  37.         # datetime.datetime.now().strftime('%H:%M:%S') -> new Date().toISOString().slice(11, 19)
  38.         content = re.sub(r'datetime.datetime.now\(\)\.strftime\(\'%H:%M:%S\'\)', r'new Date().toISOString().slice(11, 19)', content)
  39.         # datetime.datetime.now().strftime('%Y/%m/%d %H:%M:%S') -> new Date().toISOString().slice(0, 19)
  40.         content = re.sub(r'datetime.datetime.now\(\)\.strftime\(\'%Y/%m/%d %H:%M:%S\'\)', r'new Date().toISOString().slice(0, 19)', content)
  41.         # datetime.datetime.now().strftime('%Y/%m/%d') -> new Date().toISOString().slice(0, 10)
  42.         content = re.sub(r'datetime.datetime.now\(\)\.strftime\(\'%Y/%m/%d\'\)', r'new Date().toISOString().slice(0, 10)', content)
  43.         # # -> //
  44.         content = re.sub(r'#(.*)', r'//\1', content)
  45.         # '#{:02x}{:02x}{:02x}'.format(r, g, b) -> '#' + r.toString(16) + g.toString(16) + b.toString(16)
  46.         content = re.sub(r'\'#\{:02x\}\{:02x\}\{:02x\}\'.format\((.*), (.*), (.*)\)', r'\'#\' + \1.toString(16) + \2.toString(16) + \3.toString(16)', content)
  47.         # ', '.join(x) -> x.join(', ')
  48.         content = re.sub(r'(.*).join\((.*)\)', r'\1.join(\2)', content)
  49.         # '{:.2f}'.format(x) -> x.toFixed(2)
  50.         content = re.sub(r'\'\{:.2f\}\'.format\((.*)\)', r'\1.toFixed(2)', content)
  51.         # '{:.1f}'.format(x) -> x.toFixed(1)
  52.         content = re.sub(r'\'\{:.1f\}\'.format\((.*)\)', r'\1.toFixed(1)', content)
  53.         # '{}'.format(x) -> x.toString()
  54.         content = re.sub(r'\'\{\}\'.format\((.*)\)', r'\1.toString()', content)
  55.         # (1, 2, 3) -> [1, 2, 3]
  56.         content = re.sub(r'\((.*)\)', r'[\1]', content)
  57.         ...
  58.         ...
  59.         ...
  60.         ... SEE snippet.host/bccvur
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement