Table of Contents

Code Module

Code is used for performing more complex operations, compared to those that can be performed by the Equation, including communicating and retrieving data from external servers.


Endpoint

The Code endpoint provides a RESTful interface to interact with Code objects in the database. It allows authenticated users to create, update, and delete their own Code objects.

The base URL for the Code endpoint is https://datamanagerapi.solver-ai.com/api/data/code/.


HTTP Methods


Data

A Code can be set up via the Browsable API (2 - Module Management) or programmatically (more on this here).

The parameters required for creating a Code module via the Code Browsable API are:

Following is an example:

Name: Example Code
Code: C:/test/code.py
VariablesStringIn: x, y
VariablesStringOut: z
VectorizationIndices: 3-5

More on VectorizationIndices

The VectorizationIndices work similarly to the Equation and all the other modules.

In this example, VariablesStringIn represents the input variables to the code, which are 'x' and 'y'. The VariablesStringOut represents the output variables of the code, which for this case is only 'z'. The VectorizationIndices field is set to '3-5', indicating that this code should be vectorized over the indices 3, 4, and 5. This would be equivalent to defining three separate codes: 'code_3', 'code_4', and 'code_5'; where:

If vectorizationIndices is left empty, then the code will not be vectorized and the naming of the variables will remain as defined by the VariablesStringIn and VariablesStringOut.


Code File

The code file frovided by you will contain a Python script. Things you should know:

The code file contains a Python script that uses the variables specified in VariablesStringIn as global variables and sets a value for one or more output variables specified in VariablesStringOut. For example, if we have VariablesStringIn as ‘x, y’ and VariablesStringOut as ‘z’, a possible Python script could be:

Code Example

if x > y:
    z = x
else:
    z = y

This script checks if ‘x’ is greater than ‘y’, and if so, sets ‘z’ to the value of ‘x’. Otherwise, it sets ‘z’ to the value of ‘y’.

Multiple Outputs Code Example:

If there are multiple output variables in variablesStringOut, you can set values for all of them in your Python script. For example:

if x > y:
    z = x
    w = x + y
else:
    z = y
    w = x - y

This script sets values for both ‘z’ and ‘w’ based on the comparison between ‘x’ and ‘y’.

GET HTML Request Code Example:

response = get(
    'https://server-url',
    headers={"Content-Type": "application/json"}
)
if response.status_code == 200:
    response_data = response.json()
    z = response_data.get('z') + x + y
else:
    z = 0

This script computes 'z' performing an HTTP request with a get function and adding 'x' and 'y' to it.

POST HTML Request Code Example:

data = {
  'x': x,
  'y': y,
  'userId': 1,
}
response = post(
    'https://server-url',
    data=dumps(data),
    headers={"Content-Type": "application/json"}
)
if response.status_code == 200:
    response_data = response.json()
    z = response_data.get('z')
else:
    z = 0

This script computes 'z' performing an HTTP request with a post function passing 'x' and 'y' as input data.


Code Restrictions

The code is executed in a restricted environment for security reasons.

Following is a list of python keywords and functions which will not be allowed, causing the code to fail:

import np.load
open np.save
print np.savetxt
exec np.frompyfunc
eval np.vectorize
compile np.finfo
input np.iinfo
getattr
setattr
delattr
global
globals
locals

Permissions

Only authenticated users can interact with this endpoint, this can be done via the API page or pragrammatically via a token, which can be obtained from the Account page.

All Code created will be associated with the authenticated user, and and will not be accessible by other users.


Notes