Skip to main content

Write javascript in python with python syntax

Project description

https://img.shields.io/github/license/mashape/apistatus.svg https://badge.fury.io/py/py2js.svg

Usage

import py2js

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

Options: py2js.js

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

Examples

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

`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

`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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py2js-1.2.1.tar.gz (11.0 kB view details)

Uploaded Source

File details

Details for the file py2js-1.2.1.tar.gz.

File metadata

  • Download URL: py2js-1.2.1.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for py2js-1.2.1.tar.gz
Algorithm Hash digest
SHA256 0d99dd5a39c80e8e375d6af09fb3a76884f9403a692302e0ed0b946c29ee5df2
MD5 13794b2b94b1d2ad1d3d7a1a2a8ab95d
BLAKE2b-256 2443a649f92edb3954884f8f728ad5189635ab3c3c3431cef6f9966049d15dde

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page