Skip to content

General Functions

General-purpose functions for value retrieval, formatting, and conditional logic.

from everysk.sdk.engines import Expression
expression_engine = Expression()


Value Retrieval

port_attribute

port_attribute(arr, default=0.0)

Returns the single unique element in arr. Raises an exception if more than one distinct value is found. Returns default when the array is empty.

This function is typically used to extract portfolio-level attributes — such as NLV or reference date — that appear identically on every row of a data store.

import numpy as np

expression_engine.solve('port_attribute(arr)', {'arr': np.array([1000.0, 1000.0, 1000.0])})
# 1000.0

expression_engine.solve('port_attribute(arr)', {'arr': np.array(['USD', 'USD', 'USD'])})
# 'USD'

expression_engine.solve('port_attribute(arr)', {'arr': np.array([])})
# 0.0

single_value

single_value(arr, default=None)

Returns the unique value in arr, or default if the array contains more than one distinct value or is empty.

import numpy as np

expression_engine.solve('single_value(arr)', {'arr': np.array([5, 5, 5])})
# 5

expression_engine.solve('single_value(arr)', {'arr': np.array([1, 2, 3])})
# None

expression_engine.solve('single_value(arr, "mixed")', {'arr': np.array([1, 2, 3])})
# 'mixed'

single_number

single_number(arr)

Specialization of single_value that first casts the array to float. Returns 0.0 if the array has more than one distinct value or is empty.

import numpy as np

expression_engine.solve('single_number(arr)', {'arr': np.array([42, 42, 42])})
# 42.0

expression_engine.solve('single_number(arr)', {'arr': np.array([1, 2, 3])})
# 0.0

single_text

single_text(arr)

Specialization of single_value that first casts the array to str. Returns '-' if the array has more than one distinct value or is empty.

import numpy as np

expression_engine.solve('single_text(arr)', {'arr': np.array(['BRL', 'BRL', 'BRL'])})
# 'BRL'

expression_engine.solve('single_text(arr)', {'arr': np.array(['BRL', 'USD'])})
# '-'

multiple_text

multiple_text(arr, delimiter=',')

Joins the unique elements of arr into a single string, separated by delimiter. Duplicate values are deduplicated and None values are dropped. Returns '-' if no valid values remain.

import numpy as np

expression_engine.solve('multiple_text(arr)', {'arr': np.array(['Equity', 'Bond', 'Equity'])})
# 'Equity, Bond'

expression_engine.solve('multiple_text(arr, " |")', {'arr': np.array(['a', 'b', 'c'])})
# 'a | b | c'


Formatting

to_format

to_format(value, format)

Vectorized. Formats value into a display string using a format specifier. Returns None if value is None.

The format string combines a type code with an optional decimal precision:

Format Description Example input Example output
N Number 1.2345 with N2 '1.23'
% Percentage 0.2345 with %2 '23.45%'
$ Currency 123.456 with $2 '$123.46'
$S Short currency 123456.789 with $S2 '$123 456.79'
$LN Currency with notation 123456.789 with $LN2 '$123.46K'
LN Long notation 123456.789 with LN2 '123.46K'
S Space-separated 123456.789 with S0 '123 457'
D Date
expression_engine.solve('to_format(1.2345, "N2")')
# '1.23'

expression_engine.solve('to_format(0.234567, "%2")')
# '23.46%'

expression_engine.solve('to_format(123456.789, "$LN0")')
# '$123K'

import numpy as np
expression_engine.solve('to_format(values, "N2")', {
    'values': np.array([1.2345, 0.234567, 123.456])
})
# ['1.23', '0.23', '123.46']


Array Cleaning

drop_none

drop_none(arr)

Removes all None values from arr.

import numpy as np

expression_engine.solve('drop_none(arr)', {'arr': np.array([1, None, 2, None, 3])})
# [1, 2, 3]

expression_engine.solve('drop_none(arr)', {'arr': np.array(['a', None, 'b'])})
# ['a', 'b']


Conditional Logic

conditional

conditional(cond_1, value_if_cond_1, , cond_n, value_if_cond_n, default)

Evaluates conditions left to right and returns the value paired with the first condition that is True. Returns default when no condition matches.

This is equivalent to a chain of if / elif / else statements. Arguments alternate between conditions and values, ending with a single default.

expression_engine.solve('conditional(False, "a", False, "b", "c")')
# 'c'

expression_engine.solve('conditional(True, "a", False, "b", "c")')
# 'a'

expression_engine.solve('conditional(False, "a", True, "b", "c")')
# 'b'