How to use the js2py.translate_js function in Js2Py

To help you get started, we’ve selected a few Js2Py examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github PiotrDabkowski / Js2Py / tests / test_cases / testCompilationPlan.py View on Github external
import js2py
import datetime
now = datetime.datetime.now

# the line below will
# - build a 'compilation plan' by substituting strings and constants with placeholders
# - then build the ast and emit the python code from that compilation plan
# - then substitute back in the constants
# This causes some regex overhead, but for js code with similar structure
# subsequent translate_js calls are 20 times faster
js2py.translate_js('name == "Robert" && age == 46', use_compilation_plan=True)

# this lines below will re-use the compilation plan already laid out by the
# statement above
js2py.translate_js('name == "Guido" && age == 50', use_compilation_plan=True)
js2py.translate_js('name == "Spam" && age == 25', use_compilation_plan=True)

# now we'll show off the performance gain:
start = now()
for cnt in range(10000):
	js2py.translate_js('name == "Robert" && age == %i'%cnt, use_compilation_plan=False)
print(('duration without compilation plan %i'%(now() - start).seconds))

start = now()
for cnt in range(10000):
	js2py.translate_js('name == "Robert" && age == %i'%cnt, use_compilation_plan=True)
print(('duration with compilation plan %i'%(now() - start).seconds))
github PiotrDabkowski / Js2Py / tests / test_cases / testCompilationPlan.py View on Github external
js2py.translate_js('name == "Robert" && age == 46', use_compilation_plan=True)

# this lines below will re-use the compilation plan already laid out by the
# statement above
js2py.translate_js('name == "Guido" && age == 50', use_compilation_plan=True)
js2py.translate_js('name == "Spam" && age == 25', use_compilation_plan=True)

# now we'll show off the performance gain:
start = now()
for cnt in range(10000):
	js2py.translate_js('name == "Robert" && age == %i'%cnt, use_compilation_plan=False)
print(('duration without compilation plan %i'%(now() - start).seconds))

start = now()
for cnt in range(10000):
	js2py.translate_js('name == "Robert" && age == %i'%cnt, use_compilation_plan=True)
print(('duration with compilation plan %i'%(now() - start).seconds))

# you can see how a compilation plan works with the lines below:
from js2py.translators.translator import get_compilation_plan
expression = "Age==1 && Gender==2 && JobTitle=='Decision maker'"
strings, numbers, plan = get_compilation_plan(expression)
print('strings:\n%s'%strings)
print('numbers:\n%s'%numbers)
print('plan:\n%s'%plan)
print(js2py.translate_js(expression))
github ozmartian / tvlinker / tvlinker / js2py / translators / translator.py View on Github external
def main():
        with codecs.open("esprima.js", "r", "utf-8") as f:
            d = f.read()
            r = js2py.translate_js(d)

            with open('res.py','wb') as f2:
                f2.write(r)
            exec(r, {})
    if PROFILE:
github DxCx / plugin.video.9anime / resources / lib / ui / js2py / translators / translator.py View on Github external
def main():
        with codecs.open("esprima.js", "r", "utf-8") as f:
            d = f.read()
            r = js2py.translate_js(d)

            with open('res.py','wb') as f2:
                f2.write(r)
            exec(r, {})
    if PROFILE:
github PiotrDabkowski / Js2Py / js2py / translators / translator.py View on Github external
def main():
        with codecs.open("esprima.js", "r", "utf-8") as f:
            d = f.read()
            r = js2py.translate_js(d)

            with open('res.py', 'wb') as f2:
                f2.write(r)
            exec (r, {})
github a4k-openproject / script.module.openscrapers / lib / openscrapers / modules / js2py / translators / translator.py View on Github external
def main():
        with codecs.open("esprima.js", "r", "utf-8") as f:
            d = f.read()
            r = js2py.translate_js(d)

            with open('res.py','wb') as f2:
                f2.write(r)
            exec(r, {})
    if PROFILE:
github pymedusa / Medusa / lib / js2py / translators / translator.py View on Github external
def main():
        with codecs.open("esprima.js", "r", "utf-8") as f:
            d = f.read()
            r = js2py.translate_js(d)

            with open('res.py','wb') as f2:
                f2.write(r)
            exec(r, {})
    if PROFILE: