Example #1
0
    def __init__(self, value=0, base=10):
        global _precision
        _prec = get_precision(value)
        if _prec > _precision:
            _precision = _prec
            window.eval('Decimal.precision=%s' % _precision)

        self.value = javascript.JSConstructor(window.Decimal)(value, base)
Example #2
0
  def __init__(self, value=0, base=10):
      global _precision
      _prec=get_precision(value)
      if _prec > _precision:
         _precision=_prec
         window.eval('Decimal.precision=%s' % _precision)

      self.value=javascript.JSConstructor(window.Decimal)(value, base)
Example #3
0
def run(in_globals=False):
    global output
    global result
    doc["console"].value = ''
    src = editor.getValue()
    if storage is not None:
        storage["py_src"] = src

    t0 = time.perf_counter()
    ns = {}
    try:
        if (in_globals):
            exec(src)
        else:
            exec(src, ns)
        state = 1
    except Exception as exc:
        traceback.print_exc(file=sys.stderr)
        state = 0
    output = doc["console"].value

    brython_time = (time.perf_counter() - t0) * 1000.0

    print(TIME_FORMAT_STRING % ('Brython', brython_time))
    result = {'Brython': '%6.2f' % brython_time}

    # run with CPython
    req = ajax.ajax()
    req.bind('complete', on_complete)
    req.set_timeout(4, err_msg)
    req.open('POST', '/time_cpython', False)
    req.set_header('content-type', 'text/plain')
    req.send(src)

    # run Javascript version, if it exists
    js_src = None
    if in_globals:
        print('\nCan\'t run JS_CODE if in_globals is True')
    else:
        js_src = ns.get('JS_CODE')

    if js_src:
        t0 = time.perf_counter()
        window.eval(js_src)
        js_time = (time.perf_counter() - t0) * 1000.0
        print(TIME_FORMAT_STRING % ('JS', js_time))
        result['JS'] = '%6.2f' % js_time
    else:
        print(
            '\nStore Javascript code in a global named JS_CODE and I will run it for you'
        )

    return state, result
Example #4
0
def run(in_globals=False):
    global output
    global result
    doc["console"].value = ''
    src = editor.getValue()
    if storage is not None:
       storage["py_src"] = src

    t0 = time.perf_counter()
    ns = {}
    try:
        if(in_globals):
            exec(src)
        else:
            exec(src, ns)
        state = 1
    except Exception as exc:
        traceback.print_exc(file=sys.stderr)
        state = 0
    output = doc["console"].value

    brython_time = (time.perf_counter() - t0) * 1000.0


    print(TIME_FORMAT_STRING % ('Brython', brython_time))
    result = {'Brython': '%6.2f' % brython_time}

    # run with CPython
    req = ajax.ajax()
    req.bind('complete', on_complete)
    req.set_timeout(4, err_msg)
    req.open('POST', '/time_cpython', False)
    req.set_header('content-type','text/plain')
    req.send(src)

    # run Javascript version, if it exists
    js_src = None
    if in_globals:
        print('\nCan\'t run JS_CODE if in_globals is True')
    else:
        js_src = ns.get('JS_CODE')

    if js_src:
        t0 = time.perf_counter()
        window.eval(js_src)
        js_time = (time.perf_counter() - t0) * 1000.0
        print(TIME_FORMAT_STRING % ('JS', js_time))
        result['JS'] = '%6.2f' % js_time
    else:
        print('\nStore Javascript code in a global named JS_CODE and I will run it for you')

    return state, result
Example #5
0
    f(window.initClass())
    raise Exception("should have raised TypeError")
except TypeError:
    pass

x = window.initJSWithEq()
assert 'x' == x
assert x == 'x'

# test parsing json with a null value
import javascript
obj = javascript.JSON.parse('{"foo": null}')
assert obj == {"foo": None}

# issue 1352
x = window.eval()
assert x == javascript.UNDEFINED
assert type(x) == javascript.UndefinedType

# issue #1376
setattr(window, 'foo', [])

window.foo.append('one')
window.foo.append('two')
assert window.foo == ['one', 'two']

setattr(window, 'bar', [])
bar = window.bar
bar.append('one')
bar = window.bar
bar.append('two')
Example #6
0
 def load_module(self, name):
     if name is None:
        name=self._fullname
     window.eval('__BRYTHON__.imported["%s"]={}' % name)
     return JSObject(__BRYTHON__.run_py)(self._module,
                                         self._modpath, TempMod(name))
Example #7
0
 def on_complete(self, request):
     if request.status==200 or request.status==0:
         window.eval(request.text)
Example #8
0
    f(window.initClass())
    raise Exception("should have raised TypeError")
except TypeError:
    pass

x = window.initJSWithEq()
assert 'x' == x
assert x == 'x'

# test parsing json with a null value
import javascript
obj = javascript.JSON.parse('{"foo": null}')
assert obj == {"foo": None}

# issue 1352
x = window.eval()
assert x == javascript.UNDEFINED
assert type(x) == javascript.UndefinedType

# issue #1376
setattr(window, 'foo', [])

window.foo.append('one')
window.foo.append('two')
assert window.foo == ['one', 'two']

setattr(window, 'bar', [])
bar = window.bar
bar.append('one')
bar = window.bar
bar.append('two')