alteryx python sdk抽象层
SnakePlane的Python项目详细描述
蛇形面
alteryx python sdk抽象层
snakeplane是一个使alteryx的python sdk工具的构建变得简单、有趣和流畅的工具包。蛇形飞机提供了一种快速开发alteryx工具的方法,同时保持质量。该抽象提供了许多构建的功能,如对所需输入连接的错误检查、记录生成和推送等。
支架
版权所有2019 Alteryx Inc.
兹授予获得本软件副本和相关文档文件(以下简称"本软件")的任何人免费使用本软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再授权的权利,以及d/或出售软件的副本,并允许向其提供软件的人员这样做,但须遵守以下条件:
上述版权声明和本许可声明应包含在软件的所有副本或实质性部分中。
本软件按"原样"提供,不作任何形式的明示或默示保证,包括但不限于适销性保证、特定用途适用性保证和非侵权性保证。在任何情况下,无论是在合同、侵权或其他诉讼中,作者或版权所有人都不对任何索赔、损害赔偿或其他责任负责,这些索赔、损害赔偿或其他责任是由软件或软件的使用或其他交易引起的、由软件引起的或与之相关的。
设置
snakeplane设计用于alteryx designer v2018.4+中的alteryx python sdk。
有关如何开发工具/利用内置系统的示例,请参见pilot目录。
问题
发现的任何问题都应在此存储库上报告为github问题。
概述
蛇形飞机使用了一个类似烧瓶的框架。用户使用一个plugin factory
类来构建他们的插件并通过工厂的接口,并且可以指定他们的选项和自定义功能的选择。
开发人员在使用snakeplane时必须定义三个函数。
initialize_plugin
:此函数定义在工具初始化时一次发生的行为。此区域通常用于验证来自图形用户界面的设置,并执行任何必需的变量初始化。处理数据
:此函数定义用于从输入(如果存在)生成输出记录的behvior。构建元数据
:alteryx设计器平台之所以如此强大的原因之一是在配置时传播元数据。因此,工具开发人员必须在一个单独的位置指定输出数据的架构,该位置可在设计器中的配置时使用,以便将此元数据传播到下游工具。 < > >模式
:注册设计器希望插件在何种模式下运行。此变量的选项为:"batch"
:在此模式下,插件将从所有输入锚集中输入记录,然后调用处理数据
函数。"stream"
:在此模式下,每次从任何输入接口接收到记录时,插件都将调用处理数据。输入数据将是一个表示一行的列表,每个列都有一个值。
输入类型
(可选):告诉插件用户希望输入的数据类型。选项是:"list"
(默认值):输入数据是列表列表(用于批处理)或单个列表(用于流),其中每一行是一条记录,每一列是记录中的一个字段。"dataframe"
:输入数据是pandas数据框
刀具id->;int
:当前刀具的刀具id。工作流配置- >;orderedict
:通过gui sdk注册的alteryx数据项的配置。 < > >get_temp_file_path()
:创建临时文件的方法,该文件只在运行工作流的生存期内存在。返回文件的路径。创建锚点元数据()
:创建新锚点元数据对象的方法。详情如下。 < > >显示信息消息(msg:str)
:将信息消息打印到alteryx designer。显示警告消息(msg:str)
:将警告消息打印到alteryx设计器。显示错误消息(msg:str)
:将错误消息打印到alteryx设计器。 < > >数据->;[列表[列表[任意]]或pandas.dataframe]
:包含锚上的记录数据。是pandas数据帧的列表,具体取决于流程数据的输入类型
设置。此数据是只读的,因为下游工具不能影响其传入数据。元数据->;anchometadata
:包含此锚的锚元数据。此元数据是只读的,因为下游工具不能影响其传入的元数据。 < > >数据->;[列表[列表[任意]]或pandas.dataframe]
:包含锚上的记录数据。可以是熊猫数据帧的列表。此工具可以在"处理数据"功能中设置输出锚定的数据。元数据->;anchometadata
:包含此锚的锚元数据。此元数据是只读的,因为下游工具不能影响其传入的元数据。 < > >列->;列表[列元数据]
:包含列元数据
对象的列表。 < > >添加列(名称:str,列类型:alteryxsdk.fieldtype,大小:optional(int),比例:optional(int),源:optional(str),说明:optional(str))->;无
:向anchormetadata添加新列的方法。唯一需要的两个输入是列名和列类型。名称可以是与其他列名不同的任何字符串,并且类型必须是受支持的alteryx sdk字段类型之一,如下面alteryx sdk字段类型下所述。index_of(name:str)->;int
:获取指定列名的列索引。如果列不存在,则返回"无"。按名称获取列(名称:str)->;列元数据
:获取给定列名的列元数据
对象。get_column_names()->;list[str]
:获取可用列n的列表ames。 < > >名称->;str
:(必需)列的名称。类型->;sdk.fieldtype
:(必需)列的类型。大小->;int
:(可选)字符串的字符数,或blob和空间类型的字节大小。忽略基本类型。比例->;int
:(可选)固定小数类型的比例因子。其他数据忽略。源->;str
:(可选)数据源。说明->;str
:(可选)列的说明。 < > >
下面是实现这三个功能的示例工具。
批处理工具示例
importpandasaspd# Core Alteryx Python SDK FunctionalityimportAlteryxPythonSDKassdk# Abstraction Layerfromsnakeplane.plugin_factoryimportPluginFactory# Create plugin factory# NOTE: The string passed here is the name of the tool being created# This must match the name of the tool in the tools directoryfactory=PluginFactory("ExampleBatchTool")# Use the factory initialize_plugin decorator to register this function with snakeplane@factory.initialize_plugindefinit(input_mgr,user_data,logger)->bool:# We can access Alteryx data items from the workflow_config attribute of the input_mgr# The workflow_config is a dictionary of items.setting=input_mgr.workflow_config["ExampleSetting"]# We can produce errors or infos using the logger inputif(int(setting)<10):logger.display_error_msg("An error has occurred, setting less than 10.")returnFalse# Or you can display info messageslogger.display_info_msg(f"Setting is {setting}")# And warningsif"ExampleSetting2"notininput_mgr.workflow_config:logger.display_warn_msg("Setting2 not available.")# Process the data:# Again, decorate with a factory method to register this function.# The parameters for this decorators specify:# mode: (Options)# "batch": all records accumulates and process_data called once# "stream": process_data is called any time a record is recieved by this tool# "source": process_data is called once to generate records. This should only# be used when there are no input anchors# input_type: (Options)# "dataframe": data retrieved from input anchors will be in the form of a pandas dataframe# "list": data retrieved from input anchors will be a list of lists@factory.process_data(mode="batch",input_type="dataframe")defprocess_data(input_mgr,output_mgr,user_data,logger):# Gets the input anchor for the data we want, in this case there is only one input# anchor.# Since some anchors (multi-input anchors) can accept multiple inputs, the return# value of accessing an anchor by name is a list of connections. In this case, there is# only one, so we just extract it inline.# NOTE: The input/output anchor names must match those specified in tools config XML file.input_anchor=input_mgr["InputAnchorName"]input_connection=input_anchor[0]# Get the batch data from that input as a dataframe# Calling data on an input connection will give you all of the data available for# that anchor. In this case, the specified input_type is a dataframe, so the return# value is a pandas dataframe.input_data=input_connection.data# This tool will append a column of all zerosdf=pd.DataFrame({'New Column':[0]*input_data.shape[0]})# Create output dataframe by appending our new column to the input dataoutput_data=input_data.join(df)# Push the output data to the anchor of our choiceoutput_anchor=output_mgr["OutputAnchorName"]output_anchor.data=output_data# The build_metadata function takes the same parameters as the process_data function@factory.build_metadatadefbuild_metadata(input_mgr,output_mgr,user_data,logger):# Get the input anchor as beforeinput_anchor=input_mgr.data("InputAnchorName")input_connection=input_anchor[0]# Extract the input metadatametadata=input_connection.metadata# Add a new column that has a floating point value in itmetadata.add_column("New Column",sdk.FieldType.float)# Get the output anchoroutput_anchor=output_mgr["OutputAnchorName"]# Set the metadata for that anchoroutput_anchor.metadata=metadata# Export the plugin.AyxPlugin=factory.generate_plugin()
插件程序
插件工厂构造函数将工具名作为单个参数。此名称在alteryx中安装时必须与工具的目录名匹配。这是因为插件工厂内部使用此名称查找插件的配置XML文件,因此需要该工具的路径。
当使用抽象层时,首先要做的是构造一个新的插件工厂:
factory=pluginfactory("exampletool")
完成自定义功能规范(如下所述)后,必须导出插件:
ayxplugin=factory.generate_plugin()
名称ayxplugin
必须完全匹配。python sdk expect找到一个名为ayxplugin的类,以便在运行时生成插件。
初始化插件
您可以通过创建具有指定签名的函数并用@factory.initialize_plugin
对其进行修饰来指定插件初始化行为(有关python decorators的有用指南,请参阅此处的)。这将向插件工厂注册初始化功能,以确保在适当的时间调用它。
工艺数据
process_data
是插件设计器放置大部分插件功能的地方。与initialize_plugin
类似,用户注册自己的process_data
函数的方法是使用@factory.process_data
装饰器。在这种情况下,不同之处在于此装饰器采用以下参数:
生成元数据
build_metadata
是指定工具输出元数据的位置。它使用输入/输出管理器和anchometadata
对象的组合来完成
功能输入
initialize_plugin
、process_data
和build_metadata
函数可以接受以下输入的任意组合:
输入管理器
输入管理器是一个对象,用户可以通过它从插件输入锚访问数据和元数据。它可以被视为用于检索输入锚的python字典,即输入锚的名称为example
,可以通过调用input_mgr["example"]
来获取example
锚。返回值是一个inputanchors
列表,如下所述。
输入管理器还有其他一些有用的属性:
输出管理器
output_mgr
与input_mgr
类似,只是它只能访问输出锚。就对锚的字典式访问而言,它与input-mgr
具有相同的接口,只是返回值只是单个outputanchor
对象,而不是inputanchor
的列表
输出管理器也有几个有用的属性/方法:
用户数据
用户数据
是一个简单名称空间
插件设计器可以使用它来保存ey希望在调用initialize_plugin
、build_metadata
和连续调用process_data
之间保持
记录器
记录器是一个对象,包含将错误、警告和信息记录到alteryx designer画布的方法。
它包含以下方法:
工作流配置
workflow_config
是一个orderedict
包含用户从html gui指定的设置。这通常包含要在处理数据中使用的设置信息
用户数据
是一个pythonsimplename空间
对象,专门用于插件开发人员存储所需信息。在流模式下操作时,此数据在初始化插件调用和
处理数据调用之间以及
处理数据调用之间保持不变。
课程描述:
inputanchor(也称为ayxplugininterface)
用于检索给定输入锚定的数据和元数据。
属性:
输出电压
用于检索和设置给定输出锚定的数据和元数据。
属性:
凤尾鱼
anchometadata
类包含给定输入/输出锚的所有元数据,并包含检查/创建这些设置的帮助方法。
要创建新的anchometadata
对象,可以使用output\u mgr
的create\u anchor\u metadata
方法
属性:
方法:
列元数据
column metadata
类包含单个数据列的所有元数据。每个对象都具有以下属性:
alteryx sdk字段类型
所有alteryx字段类型都必须使用alteryxpythonsdk
依赖项引用。这些可以作为alteryxpythonsdk.fieldtype的属性找到。此对象的属性支持以下类型:boolean、byte、int16、int32、int64、fixedDecimal、float、double、string、wstring、v string、v_wstring、date、time和blob
。注意:此时不支持空间对象。
这些字段类型的描述可以找到在这里,在这里