When you're coding the SCRIPT for a component, you'll need to know syntax specific to the domain your working in (Javascript, R, or Python). This article covers some of the API functions for building Python components. There are others documented in the Value API article for manipulating data in javascript components.
You'll need this stuff for creating the functions that process data coming in and going out of your component. You'll also need it for debugging - creating warnings, errors, and logs.
(For a primer on what it's like working with Python in the Exaptive Studio, check out the Python article in our Working with Your Favorite Technology section.)
The Python component API is accessible through the api property of the component context passed to an input handler. It is denoted by self.api. It need not be denoted as self, however. You could use state, for instance.
// spec "input": [ { "name": "sum", "valueType": "entity<integer x, integer y>", "description": "Computes the sum of x and y." } ], "output": [ { "name": "result", "valueType": "integer", "description": "The sum value" } ]
# script def sum(self): state = self.inputstate.export() self.api.log("Hello") self.api.output("data", state["sum"]["x"] + state["sum"]["y"])
Here are the most commonly used API functions for building Python components.
inputstate self.api.inputstate Type: Value
This represents the component's current input values. It is an object of type value of the Exaptive system.
# Export to Python values. state = self.api.inputstate.export() x = state["x"] y = state["y"] # Use as Value objects. x = self.api.inputstate.get_attribute("x").export() y = self.api.inputstate.get_attribute("y").export()
imports self.api.imports Type: Dict
This is the access point to the component's dependencies such as user files and data, if any.
// Suppose these dependencies are defined in the spec. "dependencies": { "file": [ {"type": "url", "name": "myfile", "asset": "5ec33c94-2df2-11e6-b67b-9e71128cae77" }, {"type": "data", "name": "mydata", "asset": "6b82df02-2df2-11e6-b67b-9e71128cae77" }, ] }
# In the component # Data filedata = self.api.imports["mydata"] # File with open(self.api.imports["myfile"], "r") as f: filedata2 = f.read()
log self.api.log( msg, value ) Type: Function
Log messages are displayed in the dataflow console and can be viewed within the log messages tab. This function takes two arguments, msg and value. The msg argument must be a string. Consider msg to be the title of the message that is logged to the console. The value argument accepts various types including strings, json and lists.
self.api.log("This is a log") self.api.log(u"alpha=\u03b1") # With values self.api.log("This is a log", 1) self.api.log("This is a log", self.value.integer(1))
warning self.api.warning( msg, value ) Type: Function
Warning messages are highlighted yellow in the dataflow console and can be viewed within the warning messages tab. This function takes two arguments, msg and value. The msg argument must be a string. Consider msg to be the title of the message that is logged to the console. The value argument accepts various types including strings, json and lists.
error self.api.error( msg, value ) Type: Function
Error messages are highlighted red in the dataflow console and can be viewed within the error messages tab.
value self.api.value Type: Value API
This is a factory for instantiating a value type object.
# Generic import. The type is inferred automatically, unless specified. x = self.api.value.import_(1) x = self.api.value.import_({ "a": 1 }, type="entity<integer a>") # specify type # Explicitly typed imports x = self.api.value.boolean(True) x = self.api.value.string("Hello") x = self.api.value.integer(1) x = self.api.value.float(1) x = self.api.value.list([1]) x = self.api.value.multiset([1]) x = self.api.value.set([1]) x = self.api.value.entity({ "a": 1 }) x = self.api.value.binary(b"\x00\x01") x = self.api.value.null()
output self.api.output( name, value ) Type: Function
This sends a value to a component output port with the given name. This function is used to return data from a component. It takes two arguments, name and value. The name must match the corresponding output port. The value must match the value that is required by the output port.