gpt4 book ai didi

Python UiPath - 如何将类与 UiPath.Python.Activities 一起使用

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:43 27 4
gpt4 key购买 nike

这是显示 Python 范围事件的图像(版本 3.6 和目标 x64):

Python 作用域

主要问题是两个调用Python方法之间的关系,第一个用于启动类对象,第二个用于访问该类的方法。这是第一次调用 python 属性的图像:

调用Python init方法

以及 getNumberPlusOne 事件调用:

调用Python getNumberPlusOne方法

正在执行的Python代码:

class ExampleClass:
def __init__(self,t,n):
self.text = t
self.number = n

def getNumberPlusOne(self):
return (self.number+1)

最后是执行第二个Invoke Python Method时的错误:

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is: System.InvalidOperationException: Error invoking Python method ----> System.Runtime.Serialization.InvalidDataContractException: Type 'UiPath.Python.PythonObject' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

知道错误在哪里以及如何与 init 方法中创建的输出对象交互吗?

最佳答案

我相信这个事件是根据简单的脚本设计的,而不是针对整个类(class)。 Here's他们社区论坛上的一篇文章,其中用户 Sergiu.Wittenberger 介绍了更多详细信息。

让我们从“加载 Python 脚本”事件开始: invoke python script

在我的例子中,局部变量“pyScript”是指向 python 对象的指针,即 ExampleClass 的实例。

现在,有“调用 Python 方法”事件 - 该事件允许我们按名称调用方法。然而,UiPath 似乎无法访问类上的方法 - 您不能只键入 pyScript.MethodName() 。 invoke python method

所以看来我们无法访问类方法(请在这里证明我错了!),但有一个解决方法,如 Sergio 所示。在您的情况下,您可以在类外部添加另一个方法来访问或操作您的对象:

class ExampleClass:
def __init__(self,t,n):
self.text = t
self.number = n

def getNumberPlusOne(self):
return (self.number+1)


foo = ExampleClass("bar", 42)

def get_number_plus_one():
return foo.getNumberPlusOne()

请注意,这也意味着该对象是在同一个文件中实例化的:foo。在这一点上,这似乎是与对象交互的唯一选择——再次,我希望有人能证明我错了。

为了完整起见,结果如下: enter image description here

关于Python UiPath - 如何将类与 UiPath.Python.Activities 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55905144/

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