py2js


Namepy2js JSON
Version 1.2.1 PyPI version JSON
download
home_pagehttps://github.com/am230/py2js
SummaryWrite javascript in python with python syntax
upload_time2023-07-12 14:45:42
maintainer
docs_urlNone
authoram230
requires_python
licenseMIT Licence
keywords javascript convert translator
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Python to Javascript translator
===============================

.. image:: https://img.shields.io/github/license/mashape/apistatus.svg
   :target: http://opensource.org/licenses/MIT
.. image:: https://badge.fury.io/py/py2js.svg
    :target: https://badge.fury.io/py/py2js

Usage
-----

.. code:: python

    import py2js

    @py2js.js
    def main():
        # code here


Options: py2js.js
-----------------

.. code:: python

    func:              t.Optional[t.Callable] 
    as_function:       t.Optional[bool]       
    python_compatible: t.Optional[bool]       

Examples
--------

python

.. code:: python

    import py2js
    import typing

    this = typing.Self


    @py2js.js
    def translated_normal():
        'generator'
        def generator_func():
            yield 1
            yield 2
            yield 3
            yield from [9, 8, 7]
            return 0

        for item in generator_func():
            console.log(item)

        'for-else'
        for i in [0, 1, 2, 3]:
            if i > 2:
                break
        else:
            console.log('else')

        'f-string'
        a = 4
        console.log(f'{a} fstring')

        'class'
        class Main:
            a: int

            def constructor():
                this.a = 1

            def func():
                console.log(this.a)

        Main().func()

        'try catch else finally'
        try:
            raise SyntaxError('syntax error')
        except SyntaxError as e:
            console.log('syntax error raised')
        except:
            console.log('excepted')
        else:
            console.log('else')

        try:
            if Boolean(1):
                raise SyntaxError('syntax error')
        except:
            pass
        finally:
            console.log('finally')

        'while'
        i = 10
        while i > 0:
            i -= 1

        'comparator'
        i = 5
        if 0 < i < 9:
            console.log('true')


    @py2js.js(python_compatible=True)
    def translated_compatible():
        'class with self args'
        class Main:
            def __init__(self, value):
                self.a = value

            def func(self):
                console.log(self.a)

        Main('hello, world!').func()


    with open('translated_normal.js', 'w') as f:
        f.write(translated_normal)

    with open('translated_compatible.js', 'w') as f:
        f.write(translated_compatible)

translated_normal.js
~~~~~~~~~~~~~~~~~~~~

.. code:: javascript

    `generator`;
    let generator_func = function*() {
        yield 1;
        yield 2;
        yield 3;
        yield*[9, 8, 7];
        return 0
    };
    for (let item of generator_func()) {
        console.log(item)
    };
    `for-else`;
    __else: {
        for (let i of [0, 1, 2, 3]) {
            if (i > 2) {
                break __else
            }
        }
        console.log(`else`)
    };
    `f-string`;
    let a = 4;
    console.log(`${a} fstring`);
    `class`;
    let Main = class {
        constructor() {
            this.a = 1
        };
        func() {
            console.log(this.a)
        }
        a
    }
    Main = new Proxy(Main, {
        apply: (clazz, thisValue, args) => new clazz(...args)
    });;
    Main().func();
    `try catch else finally`;
    __else: {
        try {
            throw SyntaxError(`syntax error`)
        } catch (__err) {
            if (__err instanceof SyntaxError) {
                e = __err;
                console.log(`syntax error raised`);
                break __else
            } {
                console.log(`excepted`);
                break __else
            }
        }
        console.log(`else`)
    };
    try {
        if (Boolean(1)) {
            throw SyntaxError(`syntax error`)
        }
    } catch (__err) {
        {
            /* pass */ }
    } finally {
        console.log(`finally`)
    };
    `while`;
    let i = 10;
    while (i > 0) {
        i -= 1
    };
    `comparator`;
    i = 5;
    if (0 < i < 9) {
        console.log(`true`)
    }

translated_compatible.js
~~~~~~~~~~~~~~~~~~~~~~~~

.. code:: javascript

    `class with self args`;
    let Main = class {
        constructor(...args) {
            if ('__init__' in this) this.__init__(...args);
            return new Proxy(this, {
                apply: (target, self, args) => target.__call__(...args),
                get: (target, key) => target.__getitem__(key)
            })
        }
        __init__ = (...__args) => {
            ((self, value) => {
                self.a = value
            })(this, ...__args)
        };
        func = (...__args) => {
            ((self) => {
                console.log(self.a)
            })(this, ...__args)
        }
    }
    Main = new Proxy(Main, {
        apply: (clazz, thisValue, args) => new clazz(...args)
    });;
    Main(`hello, world!`).func()

todo
----

match statement

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/am230/py2js",
    "name": "py2js",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "javascript,convert,translator",
    "author": "am230",
    "author_email": "am.230@outlook.jp",
    "download_url": "https://files.pythonhosted.org/packages/24/43/a649f92edb3954884f8f728ad5189635ab3c3c3431cef6f9966049d15dde/py2js-1.2.1.tar.gz",
    "platform": "any",
    "description": "Python to Javascript translator\n===============================\n\n.. image:: https://img.shields.io/github/license/mashape/apistatus.svg\n   :target: http://opensource.org/licenses/MIT\n.. image:: https://badge.fury.io/py/py2js.svg\n    :target: https://badge.fury.io/py/py2js\n\nUsage\n-----\n\n.. code:: python\n\n    import py2js\n\n    @py2js.js\n    def main():\n        # code here\n\n\nOptions: py2js.js\n-----------------\n\n.. code:: python\n\n    func:              t.Optional[t.Callable] \n    as_function:       t.Optional[bool]       \n    python_compatible: t.Optional[bool]       \n\nExamples\n--------\n\npython\n\n.. code:: python\n\n    import py2js\n    import typing\n\n    this = typing.Self\n\n\n    @py2js.js\n    def translated_normal():\n        'generator'\n        def generator_func():\n            yield 1\n            yield 2\n            yield 3\n            yield from [9, 8, 7]\n            return 0\n\n        for item in generator_func():\n            console.log(item)\n\n        'for-else'\n        for i in [0, 1, 2, 3]:\n            if i > 2:\n                break\n        else:\n            console.log('else')\n\n        'f-string'\n        a = 4\n        console.log(f'{a} fstring')\n\n        'class'\n        class Main:\n            a: int\n\n            def constructor():\n                this.a = 1\n\n            def func():\n                console.log(this.a)\n\n        Main().func()\n\n        'try catch else finally'\n        try:\n            raise SyntaxError('syntax error')\n        except SyntaxError as e:\n            console.log('syntax error raised')\n        except:\n            console.log('excepted')\n        else:\n            console.log('else')\n\n        try:\n            if Boolean(1):\n                raise SyntaxError('syntax error')\n        except:\n            pass\n        finally:\n            console.log('finally')\n\n        'while'\n        i = 10\n        while i > 0:\n            i -= 1\n\n        'comparator'\n        i = 5\n        if 0 < i < 9:\n            console.log('true')\n\n\n    @py2js.js(python_compatible=True)\n    def translated_compatible():\n        'class with self args'\n        class Main:\n            def __init__(self, value):\n                self.a = value\n\n            def func(self):\n                console.log(self.a)\n\n        Main('hello, world!').func()\n\n\n    with open('translated_normal.js', 'w') as f:\n        f.write(translated_normal)\n\n    with open('translated_compatible.js', 'w') as f:\n        f.write(translated_compatible)\n\ntranslated_normal.js\n~~~~~~~~~~~~~~~~~~~~\n\n.. code:: javascript\n\n    `generator`;\n    let generator_func = function*() {\n        yield 1;\n        yield 2;\n        yield 3;\n        yield*[9, 8, 7];\n        return 0\n    };\n    for (let item of generator_func()) {\n        console.log(item)\n    };\n    `for-else`;\n    __else: {\n        for (let i of [0, 1, 2, 3]) {\n            if (i > 2) {\n                break __else\n            }\n        }\n        console.log(`else`)\n    };\n    `f-string`;\n    let a = 4;\n    console.log(`${a} fstring`);\n    `class`;\n    let Main = class {\n        constructor() {\n            this.a = 1\n        };\n        func() {\n            console.log(this.a)\n        }\n        a\n    }\n    Main = new Proxy(Main, {\n        apply: (clazz, thisValue, args) => new clazz(...args)\n    });;\n    Main().func();\n    `try catch else finally`;\n    __else: {\n        try {\n            throw SyntaxError(`syntax error`)\n        } catch (__err) {\n            if (__err instanceof SyntaxError) {\n                e = __err;\n                console.log(`syntax error raised`);\n                break __else\n            } {\n                console.log(`excepted`);\n                break __else\n            }\n        }\n        console.log(`else`)\n    };\n    try {\n        if (Boolean(1)) {\n            throw SyntaxError(`syntax error`)\n        }\n    } catch (__err) {\n        {\n            /* pass */ }\n    } finally {\n        console.log(`finally`)\n    };\n    `while`;\n    let i = 10;\n    while (i > 0) {\n        i -= 1\n    };\n    `comparator`;\n    i = 5;\n    if (0 < i < 9) {\n        console.log(`true`)\n    }\n\ntranslated_compatible.js\n~~~~~~~~~~~~~~~~~~~~~~~~\n\n.. code:: javascript\n\n    `class with self args`;\n    let Main = class {\n        constructor(...args) {\n            if ('__init__' in this) this.__init__(...args);\n            return new Proxy(this, {\n                apply: (target, self, args) => target.__call__(...args),\n                get: (target, key) => target.__getitem__(key)\n            })\n        }\n        __init__ = (...__args) => {\n            ((self, value) => {\n                self.a = value\n            })(this, ...__args)\n        };\n        func = (...__args) => {\n            ((self) => {\n                console.log(self.a)\n            })(this, ...__args)\n        }\n    }\n    Main = new Proxy(Main, {\n        apply: (clazz, thisValue, args) => new clazz(...args)\n    });;\n    Main(`hello, world!`).func()\n\ntodo\n----\n\nmatch statement\n",
    "bugtrack_url": null,
    "license": "MIT Licence",
    "summary": "Write javascript in python with python syntax",
    "version": "1.2.1",
    "project_urls": {
        "Homepage": "https://github.com/am230/py2js"
    },
    "split_keywords": [
        "javascript",
        "convert",
        "translator"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2443a649f92edb3954884f8f728ad5189635ab3c3c3431cef6f9966049d15dde",
                "md5": "13794b2b94b1d2ad1d3d7a1a2a8ab95d",
                "sha256": "0d99dd5a39c80e8e375d6af09fb3a76884f9403a692302e0ed0b946c29ee5df2"
            },
            "downloads": -1,
            "filename": "py2js-1.2.1.tar.gz",
            "has_sig": false,
            "md5_digest": "13794b2b94b1d2ad1d3d7a1a2a8ab95d",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10995,
            "upload_time": "2023-07-12T14:45:42",
            "upload_time_iso_8601": "2023-07-12T14:45:42.367544Z",
            "url": "https://files.pythonhosted.org/packages/24/43/a649f92edb3954884f8f728ad5189635ab3c3c3431cef6f9966049d15dde/py2js-1.2.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-07-12 14:45:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "am230",
    "github_project": "py2js",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "py2js"
}
        
Elapsed time: 0.09306s