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
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.
Function | Code | Comments |
---|---|---|
ADD | x + y | Basic math operators are readily available |
MULTIPLY | x * y | |
INT | int(number) | Returns the whole portion of a number |
TODAY | date.today() | Uses the date module to return today's date |
NOW | datetime.now() | Uses datetime to return a current timestamp |
DATE_DIFF | (date2 - date1).days | Uses the datetime timedelta class to return # of days between two dates |
DATE_TO_STR | date.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.0 | Like DATE_DIFF but takes days and seconds and converts back to minutes_ |
LOWER | value.lower() | Built-in string function to convert to lower case |
RSTRIP | value.rstrip(chars) | Strips chars from the right side of value |
FINDMANY | self.related_data(source, column, value, returncolumn) | Example of an internal helper function which take the parameters and handles the request internally |
SET_EXCEPTION | self.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 |
OCCURRENCES | self.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.
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:
Or let us know if you have an idea that needs some help or validation.