2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

pythonをjavascriptにしたい!

Last updated at Posted at 2023-02-19

(これは無視 https://github.com/flexxui/pscript

pythonからjavascriptに変換したいーー!
ということでpythonでjavascriptを書けライブラリを作りましたー

インストール!

pip install py2js

使い方

py2js.convert(コードの文字列 もしくは 関数やクラス)

もしくは

@py2js.js
def main():
    // ここに変換したいコードを入れる

とするとmainがjavascriptのコードになります

サンプル!

これが

'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')

このように変換されます!

`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`)
}

仕組み

pythonを構文木解析してそれをなんかいろいろしてjavascriptに変換!

詳しくは見て

ソースコード!!!!

2
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?