Skip to main content

Function Code

Functions are implemented using the Python3 scripting language. The Python scripts can call most functions and operators within the standard library. For security and commonsense reasons, most imported modules are not accessible. To that extent, any module not imported by the function processor is not available in the Code evaluation.

Some of the limited imported modules include:

  • datetime
  • math (some mapped functions)
  • decimal
tip

If you want to write a function for which an import is needed, please contact us to review as we can extend the reach of the imported libraries

Code in a function is evaluated in a "sandboxed" space so-as to manage variables and contain any errors. Generally code should be limited to one line as there is no document context in which to manage indentation and other language considerations. Fortunately Python is good at stuffing lots into a single line, so you can model a function offline as a multi-line process, then short-hand it to fit into the single line requirement here.

Example Code

Here are a few examples of Code from the current functions list to demonstrate the nature of what you could create.

FunctionCodeComments
ADDx + yBasic math operators are readily available
MULTIPLYx * y
INTint(number)Returns the whole portion of a number
TODAYdate.today()Uses the date module to return today's date
NOWdatetime.now()Uses datetime to return a current timestamp
DATE_DIFF(date2 - date1).daysUses the datetime timedelta class to return # of days between two dates
DATE_TO_STRdate.strftime(mask)Uses the date module to convert a date to a printable string using Masks
TIME_DIFF_MINUTES((time2 - time1).days * 86400 + (time2 - time1).seconds) / 60.0Like DATE_DIFF but takes days and seconds and converts back to minutes_
LOWERvalue.lower()Built-in string function to convert to lower case
RSTRIPvalue.rstrip(chars)Strips chars from the right side of value
FINDMANYself.related_data(source, column, value, returncolumn)Example of an internal helper function which take the parameters and handles the request internally
SET_EXCEPTIONself.set_exception(exception)Another internal helper; this one doesn't really do anything complicated, but since its used a-lot, is simply a timesaving step versus having to use SET(Exception, exceptiontype) sort of thing; there are shortcut helpers like this for EMPLOYEE, PUNCH and others
OCCURRENCESself.occurrences(type, starting, ending, hours)We weren't about to attempt this in the no-code rules interface, but is a useful internal function to build upon

You can review all the Code snippets in the next section.

tip

You'll notice in the last few examples the internal helper functions start with self.somefunction which is the specific refence to the rules parsing engine. Standard Python Library code would not start with or refer to self

Lastly, we'll leave the further understanding of the Python language to many readily available resources, starting here:

Python3 Standard Library

Python3 Docs

Or let us know if you have an idea that needs some help or validation.