gpt4 book ai didi

python - 在自定义语法中修改变量的最有效方法?

转载 作者:太空宇宙 更新时间:2023-11-03 16:15:00 26 4
gpt4 key购买 nike

我正在使用一个适当的驾驶模拟器,它会在Scilab的定制版本中生成“场景”文件。 。我获得了一个 11,000 行长的“主”文件,我需要替换某些值来生成场景的 n 个版本。

单个父 TASK 语法的最小示例如下所示:

TYPEOF TASK (57)
{
LABEL="Dot 3a"/*replace with name for name in list */
TASK_KIND="0"

TYPEOF VARIABLE (53)
{
LABEL="Time1"
TYPE="FLOAT"
VALUE="14.000000" /* replace with random.integer() */
INTERACTIVE="VOID"

TYPEOF VARIABLE (54)
{
LABEL="X_pos1"
TYPE="FLOAT"
VALUE="23.600000"
INTERACTIVE="VOID"


TYPEOF TASK (58)
{
LABEL="Task: ISI"
TASK_KIND="0"

TYPEOF RULE (115)
{
LABEL="Rule: Go to subtask after Time1 seconds"

TYPEOF CONDITION (SUPERIOR)
{
IS_EXPANDED="1"
MODIFIER="BECOMES_TRUE"

TYPEOF PARAMETER (OPERAND_1)
{
KIND="FUNCTION"

TYPEOF FUNCTION (GET_TASK_CLOCK)
{
}
OWNER_FILE=""
}

TYPEOF PARAMETER (OPERAND_2)
{
KIND="VARIABLE"
VALUE="53"
OWNER_FILE=""
}
}

TYPEOF ACTION (GOTO_TASK)
{
IS_EXPANDED="1"

TYPEOF PARAMETER (TASK_NUMBER)
{
KIND="ENUM"
VALUE="GOTO_NEXT_TASK"
OWNER_FILE=""
}
}
}
}

我需要用标准输入替换此脚本中的某些值。例如,有一个名称列表,它将替换父级别 TASK 下的 LABEL 的值;并且必须将第一个父级 VARIABLEVALUE 替换为 6 到 16 之间的随机数。

我的第一个解决方案是基于 Python REGEX 的,如下所示(但对于我寻求更改的每个值):

for row in scenarioInput:
parenttaskmatch = re.search("^\t\tTYPEOF TASK",row)
if parenttaskmatch:
replacementrow = re.sub(r"([0-9]{1,3})",repl,row)

有人建议我可以使用 Parsimonious 之类的东西编写自定义语法,然后使用 Mustache 重新生成输出。

from parsimonious.grammar import Grammar
grammar = Grammar(r"""
any = task / data
task = "TYPEOF " key " (" number ")" newline open_curly any+ close_curly
data = key "=" quote text quote newline
open_curly = "{" newline
close_curly = "}" newline
key = ~"[A-Z 0-9_]*"
text = ~"[A-Z0-9 ]*"i
number = ~"[0-9]*"
newline = "\n"
space = " "
quote = "\""
""")

text = open('example_driving_rule.sci').read()
grammar.parse(text)
# Note doesn't work

正如您所看到的,这也不是解决问题的有效方法。你们认为更好的解决方案是什么?

最佳答案

也许您可以将文件转换为 Scilab 脚本,该脚本会生成包含新值的文件。

转换非常简单首先在 Scilab 中(完成一次)

 T=mgetl("Task_file");mputl(sci2exp(T),"Task_file.sce")

对于每个实验,使用文本编辑器修改生成的脚本,以将默认值替换为预期的值(可以通过从文件中读取这些值,或者......)

参见下面的示例 Time1 值是由 grand 生成的,X_pos1 是从 Scilab 控制台读取的

 T=["TYPEOF TASK (57)"
"{"
" LABEL=""Dot 3a""/*replace with name for name in list */"
" TASK_KIND=""0"""
""
" TYPEOF VARIABLE (53)"
" {"
" LABEL=""Time1"""
" TYPE=""FLOAT"""
" VALUE="""+string(grand(1,1,"uin",6,16)+"""" /* replace with random.integer() */"
" INTERACTIVE=""VOID"""
""
" TYPEOF VARIABLE (54)"
" {"
" LABEL=""X_pos1"""
" TYPE=""FLOAT"""
" VALUE=""""+string(input("X_pos1")+""""
" INTERACTIVE=""VOID"""
""
""
" TYPEOF TASK (58)"
" {"
" LABEL=""Task: ISI"""
" TASK_KIND=""0"""
""
" TYPEOF RULE (115)"
" {"
" LABEL=""Rule: Go to subtask after Time1 seconds"""
""
" TYPEOF CONDITION (SUPERIOR)"
" {"
" IS_EXPANDED=""1"""
" MODIFIER=""BECOMES_TRUE"""
""
" TYPEOF PARAMETER (OPERAND_1)"
" {"
" KIND=""FUNCTION"""
""
" TYPEOF FUNCTION (GET_TASK_CLOCK)"
" {"
" }"
" OWNER_FILE="""""
" }"
""
" TYPEOF PARAMETER (OPERAND_2)"
" {"
" KIND=""VARIABLE"""
" VALUE=""53"""
" OWNER_FILE="""""
" }"
" }"
""
" TYPEOF ACTION (GOTO_TASK)"
" {"
" IS_EXPANDED=""1"""
""
" TYPEOF PARAMETER (TASK_NUMBER)"
" {"
" KIND=""ENUM"""
" VALUE=""GOTO_NEXT_TASK"""
" OWNER_FILE="""""
" }"
" }"
" }"
" }"];
muptl(T,"Task")

关于python - 在自定义语法中修改变量的最有效方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39008505/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com