jiphy模块的小demo
jiphy模块
能够让js与python能够互相转换。
安装方法:
方法一:pypi上下载并安装
方法二:
使用pip下载
pip install jiphy
安装成功后进行小测试
1. 在本地创建一个.js文件,并写入js代码
test.js代码如下
function test_jiphy();
{
alert('测试 Jiphy')
};
2.在按住shift后按下右键,选择"在此处打开命令行窗口"
3.输入命令"jiphy test.js test.py"
完成!!!!
文件夹中生成了test.py文件
查看test.py文件
报错:是以为内定义函数的最后要加冒号,还有缩进问题
注意:因为本文使用案例十分简单,而使用相对复杂的js进行转换的时候就有可能出现很多问题如下,是我使用code.js进行测试
code.js代码如下:
function strdecode(string) {
string = base64decode(string);
key = Gword+'ok ';
len = key.length;
code = '';
for (i = 0; i < string.length; i++) {
var k = i % len;
code += String.fromCharCode(string.charCodeAt(i) ^ key.charCodeAt(k))
}
return base64decode(code)
}
var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
function base64decode(str) {
var c1, c2, c3, c4;
var i, len, out;
len = str.length;
i = 0;
out = "";
while (i < len) {
do {
c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]
} while (i < len && c1 == -1);
if (c1 == -1) break;
do {
c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]
} while (i < len && c2 == -1);
if (c2 == -1) break;
out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));
do {
c3 = str.charCodeAt(i++) & 0xff;
if (c3 == 61) return out;
c3 = base64DecodeChars[c3]
} while (i < len && c3 == -1);
if (c3 == -1) break;
out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));
do {
c4 = str.charCodeAt(i++) & 0xff;
if (c4 == 61) return out;
c4 = base64DecodeChars[c4]
} while (i < len && c4 == -1);
if (c4 == -1) break;
out += String.fromCharCode(((c3 & 0x03) << 6) | c4)
}
return out
}
执行命令jiphy code.js code.py 后生成的code.py文件如下:
def strdecode(string):
string = base64decode(string)
key = Gword+'ok '
len = key.length
code = ''
for i = 0; i < string.length; i++:
var k = i % len
code += str.fromCharCode(string.charCodeAt(i) ^ key.charCodeAt(k))
return base64decode(code)
var base64DecodeChars = new Array(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1)
def base64decode(str):
var c1, c2, c3, c4
var i, len, out
len = str.length
i = 0
out = ""
while i < len:
do {
c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff]
} while i < len and c1 == -1)
if c1 == -1) break
do {
c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff]
} while i < len and c2 == -1)
if c2 == -1) break
out += str.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4))
do {
c3 = str.charCodeAt(i++) & 0xff
if c3 == 61) return out
c3 = base64DecodeChars[c3]
} while i < len and c3 == -1)
if c3 == -1) break
out += str.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2))
do {
c4 = str.charCodeAt(i++) & 0xff
if c4 == 61) return out
c4 = base64DecodeChars[c4]
} while i < len and c4 == -1)
if c4 == -1) break
out += str.fromCharCode(((c3 & 0x03) << 6) | c4)
}
return out
}t
}t
}ut
}t
}ut
}t
在****的编辑器中看可能没有问题,但是在pycharm中错误百出.
发现jiphy这个模块的在转换过程中处理方法还不是太完善,还需要人工在过一遍才可放心使用
补充 (以下内容为参考取入):
phy 目前的最大短板在于其仅支持 Python 的一部分功能集。类以及默认参数尚不受支持,不过装饰器与例外机制已经可以正常使用。这主要是因为 Jiphy 坚持在源代码与目标代码之间采用行对行直接转译方式,不过其开发人员也开始着眼于 ES6 中的新功能,旨在将更多高级 Python 功能纳入支持范畴。