跳转到主要内容

将简短的Pascal脚本转换为Python的工具

项目描述

Transmogripy

关于

Transmogripy是一个将简短的Pascal脚本转换为Python的工具。最初它被用来转换TaLoS脚本,但可以很容易地适应许多其他环境。

要求

Transmogripy故意设计成在基本的运行环境中运行,所以唯一的要求是Python >= 3.6,它甚至可以在嵌入式系统(如QGIS的Python环境)中运行。

关于Numpy

Numpy不是必需的,甚至都没有使用。然而,Numpy可能会出现在输出脚本中。可以通过将allow_numpy参数显式设置为False来防止这种情况。

用法

主要功能是convert函数。

from transmogripy import convert

print(convert("""
var
   a: integer;
begin
   str := GetVal(Sym[j], 'Name') + ':' + IntToStr(Count[j]);
   a := 10; { do something
   also this}
   (* repeat until loop execution *)
   repeat
      writeln('repeat until loop execution: ', a);
      a := a + log10(12);
      l := floattoint(12.5);
   until a = 20;
end
"""
))

打印

"""
this script was automatically converted from pascal using transmogripy v...
"""

from talos import *
import math


def main():
   str = GetVal(Sym[j], 'Name') + ':' + str(count[j])
   a = 10  # do something
   # also this
   # repeat until loop execution
   while True:
      writeln('repeat until loop execution: ', a)
      a = a + math.log10(12)
      l = int(12.5)
      if a == 20: break

Result :=的行为

处理Pascal的Result :=代码有几种不同的方法,可以通过convertresult_behaviour参数进行设置

  • "variable":将创建一个临时变量__Return__,仅在脚本结束时返回
  • "return":将Result :=转换为return,并且不会检查早期返回
  • "warn":与"return"相同,如果检测到早期返回,将发出警告
  • "try"(默认值):与"warn"相同,但如果检测到早期警告,将重新尝试转换,使用"variable"选项

注释和行内注释

转换尝试转换注释,然而,所有行内注释(在同一行后有代码的注释)将被移除

var
    a: integer;
begin
    a := 0;
    Result := 0;
    while a <> 10 do {end of line comment}
    begin
        Result := Result {inline comment} + a;
        a := a+1; // end-of line comment
    end;
end

转换为

"""
this script was automatically converted from pascal using transmogripy v...
"""

from talos import *


def main():
    a = 0
    __Return__ = 0
    while a != 10:  # end of line comment
        __Return__ = __Return__  + a
        a = a+1  # end-of line comment
    return __Return__

此行为可以通过设置remove_inline_comments参数来更改,将其设置为False将在遇到行内注释时引发异常

导入、头和段

一些Pascal函数,如log,需要导入Python库。Transmogopy会自动添加转换为的代码中引用的库。

var
    a: integer;

begin
    a := 0;
    Result := 0;
    while a <> 10 do
    begin
        Result := Result + log10(a);
        a := a+1;
    end;
end

转换为

"""
this script was automatically converted from pascal using transmogripy v...
"""

from talos import *
import math


def main():
    a = 0
    __Return__ = 0
    while a != 10:
        __Return__ = __Return__ + math.log10(a)
        a = a+1
    return __Return__

此外,可以使用pre_words/post_words参数在转换代码之前/之后添加额外的行。(注意:pre_words的默认值为(from talos import *,),结果为from talos import *行)

代码检查

默认情况下,Transmogripy会检查输出脚本的语法,如果发现任何错误,则会发出警告。可以通过将check_syntax参数设置为False来更改此行为。

不支持

以下Pascal/Delphi功能不受支持,将引发错误

  • 预编译指令(foo({$IFDEF bar}bar{$ENDIF})
  • goto语句(goto foo
  • 所有子句和关键字(if/while等)必须小写(While
  • else if -> elif仅在elseif在同一行时才受支持
    • 通常,只支持格式正确的脚本

项目详情


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

此版本没有可用的源代码分发文件。请参阅生成分发存档的教程

构建分发

transmogripy-1.2.0-py3-none-any.whl (13.8 kB 查看散列)

上传时间 Python 3

由以下组织支持