def test_scripts(): # Prepare pycode = 'foo = 42; print(foo)' pyname = os.path.join(tempfile.gettempdir(), 'pscript_test.py') with open(pyname, 'wb') as f: f.write(pycode.encode()) jsname = pyname[:-3] + '.js' # Convert - plain file (no module) script2js(pyname) # Check result jscode = open(jsname, 'rb').read().decode() assert 'foo = 42;' in jscode assert 'define(' not in jscode # Convert - module light script2js(pyname, 'mymodule', module_type='simple') # Check result jscode = open(jsname, 'rb').read().decode() assert 'foo = 42;' in jscode assert 'define(' not in jscode # Convert - module UMD script2js(pyname, 'mymodule', module_type='umd') # Check result jscode = open(jsname, 'rb').read().decode() assert 'foo = 42;' in jscode assert 'define(' in jscode assert 'module.exports' in jscode assert 'root.mymodule' in jscode # Convert - no module, explicit file script2js(pyname, None, jsname) # Check result jscode = open(jsname, 'rb').read().decode() assert 'foo = 42;' in jscode assert 'define(' not in jscode
PScript creates AMD modules that can be used in the browser, in nodejs, and in combination with browserify and related tools. """ # This import is ignored by PScript, it allows using these variable # names without triggering pyflakes (a static Python source analysis tool). from pscript import undefined, window # noqa class Foo: a_constant = 1, 2, 3 def ham(self, x): self.x = x def eggs(self, y): self.y = self.x * y hasattr(y, str) class Bar(Foo): def bla(self, z): print(z) if __name__ == '__main__': from pscript import script2js script2js(__file__, 'mymodule')
request = window.indexedDB.open(self._dbname, self._dbversion) request.onerror = on_error request.onupgradeneeded = self._on_update_required request.onsuccess = on_db_ready return await window.Promise(executor) def _error_msg(self, e): msg = "IndexDB error" if e.target.errorCode: msg += " (" + e.target.errorCode + ")" if e.target.error: msg += ": " + e.target.error return msg def _on_update_required(self, e): # This is where we structure the database. # Gets called before db_open_request.onsuccess. db = e.target.result for i in range(len(db.objectStoreNames)): db.deleteObjectStore(db.objectStoreNames[i]) db.createObjectStore(self._dbstorename, {"keyPath": "key"}) if __name__ == "__main__": import pscript pscript.script2js( __file__, target=__file__[:-3] + ".js", namespace="utils", module_type="simple" )
date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7); // January 4 is always in week 1. var week1 = new Date(date.getFullYear(), 0, 4); // Adjust to Thursday in week 1 and count number of weeks from date to week1. var res = 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000 - 3 + (week1.getDay() + 6) % 7) / 7); """) return res # noqa def get_timezone_info(t): d = Date(t * 1000) d_winter = Date(d.getFullYear(), 0, 1) d_summer = Date(d.getFullYear(), 6, 1) # offset = -d.getTimezoneOffset() / 60 offset_winter = -d_winter.getTimezoneOffset() / 60 offset_summer = -d_summer.getTimezoneOffset() / 60 return offset, offset_winter, offset_summer if __name__ == "__main__": import pscript pscript.script2js( __file__, target=__file__[:-3] + ".js", namespace="datetime", module_type="simple", )