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 custom script s 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

image11

dict

[], clear, copy, fromkeys, get, has_key, items, iteritems, iterkeys, itervalues, keys, pop, popitem, setdefault, update, values, viewitems, viewkeys, viewvalues

image12

tuple

[], count, index

image13

list

[], count, index, reverse, sort

image14

set

difference, intersection, isdisjoint, issubset, issuperset, symmetric_difference, union

image15

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

image16

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

image17

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

image18

hashlib

md5.hexdigest, md5.digest

image19

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

image20

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

image21

babel

numbers.format_decimal

image22

base64

b64encode

image23

calendar

monthrange

image24

collections

Counter

image25

datetime

date, datetime, time, timedelta, tzinfo

image26

dateutil

parser.parse, relativedelta.relativedelta

parser relativedelta

hashlib

md5, sha256

image27

isoweek

Week

image28

itertools

product

image29

json

loads, dumps

image30

math

ceil, exp, floor, trunc

image31

pytz

timezone.localize

image32

random

choice, randint, random

image33

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

image34

statistics

mean, fmean, geometric_mean, harmonic_mean, median, median_low, median_high, median_grouped, mode, multimode, quantiles, pstdev, pvariance, stdev, variance

image35

unicodedata

combining, normalize

image36

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

image37

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)))