Using Python expressions in custom scripts
This guide explains how to use Python expressions in custom scripts.
Use Python expressions in custom scripts such as addfieldx and convertx. Python expressions let you transform data in many different ways and tailor data to your needs.
When using Python expressions in custom scripts, follow these rules:
-
Use Python 3 code expressed in a single line.
-
Write conditional expressions in one line. For more information, see the Python documentation.
-
Only use curly brackets
{}
for the column names. -
Surround strings with single quotation marks
'string'
. Do not use double quotation marks"string"
. -
You can use the data types and methods supported by Adverity without importing any Python modules. For more information, see Supported data types and methods.
-
You can use the classes and methods of the standard Python modules supported by Adverity without importing any Python modules. For more information, see Supported classes and methods of standard Python modules.
-
You can import and use the Python modules supported by Adverity. For more information, see Importing and using Python modules.
-
In addition to the standard Python modules, you can import and use Adverity's own modules. For more information, see Importing and using Adverity modules.
For more information on adding, changing and comparing dates using Python expressions in custom scripts, see Using Python expressions to manage dates.
Supported data types and methods
The data types and methods supported by Adverity are listed in the table below. You can use these data types and methods without importing any Python modules.
Data type |
Supported methods |
Documentation |
---|---|---|
str, bytes |
capitalize, casefold, center, count, decode, encode, endswith, expandtabs, find, format, index, isalnum, isalpha, isascii, isdecimal, isdigit, isidentifier, islower, isnumeric, isprintable, isspace, istitle, isupper, join, ljust, lower, lstrip, partition, replace, rfind, rindex, rjust, rpartition, rsplit, rstrip, split, splitlines, startswith, strip, swapcase, title, upper, zfill |
|
dict |
[], clear, copy, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, setdefault, update, values, viewitems, viewkeys, viewvalues |
|
tuple |
[], count, index |
|
list |
[], count, index, reverse, sort |
|
set |
difference, intersection, isdisjoint, issubset, issuperset, symmetric_difference, union |
Supported classes and methods of standard Python modules
The classes and methods of the standard Python modules supported by Adverity are listed in the table below. You can use these classes and methods without importing any Python modules.
Module |
Supported classes and methods |
Documentation |
---|---|---|
datetime |
date.day, date.fromisocalendar, date.fromisoformat, date.fromordinal, date.fromtimestamp, date.isocalendar, date.isoformat, date.isoweekday, date.max, date.min, date.month, date.replace, date.resolution, date.strftime, date.timetuple, date.today, date.toordinal, date.weekday, date.year datetime.astimezone, datetime.combine, datetime.date, datetime.dst, datetime.fold, datetime.hour, datetime.microsecond, datetime.minute, datetime.now, datetime.second, datetime.strptime, datetime.time, datetime.timestamp, datetime.timetz, datetime.tzinfo, datetime.tzname, datetime.utcfromtimestamp, datetime.utcnow, datetime.utcoffset, datetime.utctimetuple, datetime.min, datetime.max time.dst, time.fold, time.hour, time.isoformat, time.max, time.microsecond, time.min, time.minute, time.replace, time.resolution, time.second, time.strftime, time.tzinfo, time.tzname, time.utcoffset timedelta.days, timedelta.max, timedelta.microseconds, timedelta.min, timedelta.resolution, timedelta.seconds, timedelta.total_seconds |
|
collections |
Counter.clear, Counter.copy, Counter.elements, Counter.fromkeys, Counter.get, Counter.items, Counter.keys, Counter.most_common, Counter.pop, Counter.popitem, Counter.setdefault, Counter.subtract, Counter.update, Counter.values |
|
isoweek |
Week.contains, Week.day, Week.days, Week.friday, Week.fromordinal, Week.fromstring, Week.isoformat, Week.last_week_of_year, Week.monday, Week.replace, Week.saturday, Week.sunday, Week.thisweek, Week.thursday, Week.toordinal, Week.tuesday, Week.wednesday, Week.week, Week.weeks_of_year, Week.withdate, Week.year, Week.year_week |
|
hashlib |
md5.hexdigest, md5.digest |
|
re |
match(',').end, match(',').endpos, match(',').expand, match(',').group, match(',').groupdict, match(',').groups, match(',').lastgroup, match(',').lastindex, match(',').pos, match(',').re, match(',').regs, match(',').span, match(',').start, match(',').string |
Importing and using Python modules
To extend the basic functionality offered by the classes, data types and methods supported by Adverity, import additional Python modules. To do so, use the following expression:
(__import__('MODULE_NAME'))
The modules supported by Adverity, and the supported classes and methods for each module, are listed in the table below.
Module |
Supported classes and methods |
Documentation |
---|---|---|
ast |
literal_eval |
|
babel |
numbers.format_decimal |
|
base64 |
b64encode |
|
calendar |
monthrange |
|
collections |
Counter |
|
datetime |
date, datetime, time, timedelta, tzinfo |
|
dateutil |
parser.parse, relativedelta.relativedelta |
|
hashlib |
md5, sha256 |
|
isoweek |
Week |
|
itertools |
product |
|
json |
loads, dumps |
|
math |
ceil, exp, floor, trunc |
|
pytz |
timezone.localize |
|
random |
choice, randint, random |
|
re |
A, ASCII, DEBUG, DOTALL, I, IGNORECASE, L, LOCALE, M, MULTILINE, S, Scanner, T, TEMPLATE, U, UNICODE, VERBOSE, X, compile, escape, findall, finditer, match, search, split, sub, subn |
|
statistics |
mean, fmean, geometric_mean, harmonic_mean, median, median_low, median_high, median_grouped, mode, multimode, quantiles, pstdev, pvariance, stdev, variance |
|
unicodedata |
combining, normalize |
|
urllib |
parse.parse_qs, parse.parse_qsl, parse.quote, parse.quote_plus, parse.unquote, parse.unquote_plus, parse.urlencode, parse.urlparse, parse.urlsplit, parse.urlunsplit |
Importing and using Adverity modules
To extend the basic functionality offered by the standard Python modules, import Adverity's own modules. To do so, use the following expression:
(__import__('MODULE_NAME'))
Currently, the only Adverity module available is adverity.dates
, which calculates the number of business days between two given dates. See below for a worked example on how to calculate the number of business dates in a date range using the Adverity module.
Calculating business days between dates using the adverity.dates module
To calculate the number of business days between two dates, use the dates.busday_count
method of the adverity.dates
module. Specify two dates as the two parameters of this method. Enter the parameters as instances of datetime.date
.
For example, to calculate the number of business days between 1 January 2022 and 1 February 2022, use the following expression:
(__import__('adverity.dates').dates.busday_count(datetime.date(2022, 1, 1), datetime.date(2022, 2, 1)))