如何解决 IronPython Compile() 问题?
How to workaround IronPython Compile() Issue?
我正在尝试 运行 在我的 C#/IronPython 中执行以下操作:
import re
Message = re.sub(r"^EVN\|A\d+", "EVN|A08", Message, flags=MULTILINE)
这在真正的 python 命令提示符下运行良好。但是,一旦我将其放入 IronPython 中,就会出现错误:
IronPython.Runtime.PythonContext.InvokeUnaryOperator(CodeContext context, UnaryOperators oper, Object target, String errorMsg)
at IronPython.Runtime.Operations.PythonOps.Length(Object o)
at IronPython.Modules.Builtin.len(Object o)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
at Microsoft.Scripting.Hosting.ScriptEngine.Execute(String expression, ScriptScope scope)
研究让我明白(对还是错?)MULTILINE 标志在 IronPython 中触发 Compile()。然后我找到了这篇关于它在 IronPython 中缺乏支持的文章:https://ironpython.codeplex.com/workitem/22692.
删除 flags=MULTILINE
修复了错误。但是,它不再匹配 "^EVN"
。
编辑:如果我使用 flags=re.MULTILINE
我会收到此错误:
ERROR Error while processing the message. Message: sub() got an unexpected keyword argument 'flags'. Microsoft.Scripting.ArgumentTypeException: sub() got an unexpected keyword argument 'flags'
结束编辑
我的问题是:我怎样才能解决这个问题,并且仍然得到与在命令行中给定上述代码片段相同的结果,但在 IronPython?
我很少使用 Python,更不用说 IronPython,所以请原谅我不确定我的替代品。
IronPython 可能不支持 re.sub
中的 flags
关键字参数。要解决该问题,您可以先编译您的正则表达式。如果您打算多次使用您的表达式,无论如何都推荐这样做;无论如何模块级函数都会这样做。
为此,请使用 re.compile
。标志可以作为第二个参数传递:
regex = re.compile('^EVN\|A\d+', re.MULTILINE)
这给了你一个正则表达式对象,你可以直接使用它的sub
方法来执行你的替换:
Message = regex.sub('EVN|A08', Message)
我正在尝试 运行 在我的 C#/IronPython 中执行以下操作:
import re
Message = re.sub(r"^EVN\|A\d+", "EVN|A08", Message, flags=MULTILINE)
这在真正的 python 命令提示符下运行良好。但是,一旦我将其放入 IronPython 中,就会出现错误:
IronPython.Runtime.PythonContext.InvokeUnaryOperator(CodeContext context, UnaryOperators oper, Object target, String errorMsg)
at IronPython.Runtime.Operations.PythonOps.Length(Object o)
at IronPython.Modules.Builtin.len(Object o)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
at Microsoft.Scripting.Hosting.ScriptEngine.Execute(String expression, ScriptScope scope)
研究让我明白(对还是错?)MULTILINE 标志在 IronPython 中触发 Compile()。然后我找到了这篇关于它在 IronPython 中缺乏支持的文章:https://ironpython.codeplex.com/workitem/22692.
删除 flags=MULTILINE
修复了错误。但是,它不再匹配 "^EVN"
。
编辑:如果我使用 flags=re.MULTILINE
我会收到此错误:
ERROR Error while processing the message. Message: sub() got an unexpected keyword argument 'flags'. Microsoft.Scripting.ArgumentTypeException: sub() got an unexpected keyword argument 'flags'
结束编辑
我的问题是:我怎样才能解决这个问题,并且仍然得到与在命令行中给定上述代码片段相同的结果,但在 IronPython?
我很少使用 Python,更不用说 IronPython,所以请原谅我不确定我的替代品。
IronPython 可能不支持 re.sub
中的 flags
关键字参数。要解决该问题,您可以先编译您的正则表达式。如果您打算多次使用您的表达式,无论如何都推荐这样做;无论如何模块级函数都会这样做。
为此,请使用 re.compile
。标志可以作为第二个参数传递:
regex = re.compile('^EVN\|A\d+', re.MULTILINE)
这给了你一个正则表达式对象,你可以直接使用它的sub
方法来执行你的替换:
Message = regex.sub('EVN|A08', Message)