Python Expression
Learn how to use Python expressions in Julep task definitions
Python Expressions in Tasks
Julep tasks support Python expressions for dynamic value computation and data manipulation. This guide explains how to use them effectively.
The Special _
Variable
The underscore _
is a special variable that serves three different purposes depending on where it’s used:
- First Step Input: In the first step of a task,
_
contains the execution input:
- Previous Step Output: In any subsequent step,
_
contains the output from the previous step:
- Iterator Value: In
foreach
andmap
steps,_
represents the current item being iterated.
Where Python Expressions Are Used
Python expressions can be used in various task steps. For a complete list of step types and their syntax, refer to the Step Types table in the README.
Common places include:
evaluate
steps- Tool
arguments
if
conditionsforeach
andmap
iterations
Available Functions and Libraries
The following Python functions and libraries are available for use in expressions:
Basic Python Builtins
abs
,all
,any
,bool
,dict
,enumerate
float
,int
,len
,list
,map
,max
,min
round
,set
,str
,sum
,tuple
,zip
,reduce
Safe Versions of Functions
-
range
:def safe_range(*args)
Safely creates a range object with size limits (max 1,000,000 elements).
-
load_json
:def safe_json_loads(s: str) -> Any
(Deprecated in favor ofjson.loads
)Safely parses a JSON string with size limits.
-
load_yaml
:def safe_yaml_load(s: str) -> Any
(Deprecated in favor ofyaml.safe_load
)Safely parses a YAML string with size limits.
-
dump_json
:def dump_json(obj: Any, *, **kwargs) -> str
(Deprecated in favor ofjson.dumps
)Safely serializes an object to a JSON string.
-
dump_yaml
:def dump_yaml(obj: Any, **kwargs) -> str
(Deprecated in favor ofyaml.dumps
)Safely serializes an object to a YAML string.
-
extract_json
:def safe_extract_json(string: str) -> Any
Safely extracts and parses JSON from text.
Regex and NLP Functions
-
search_regex
:def search_regex(pattern: str, string: str) -> Optional[re2.Match]
Searches for a regex pattern in a string.
-
match_regex
:def match_regex(pattern: str, string: str) -> bool
Checks if a regex pattern matches a string.
-
chunk_doc
:def chunk_doc(string: str) -> list[str]
Chunks a string into sentences.
-
nlp
pipelines.
Example using these functions in an evaluate step:
Standard Library Modules
re
: Regular expressions (using re2)json
: JSON operationsyaml
: YAML operationsstring
: String constants and operationsdatetime
: Date and time operationsmath
: Mathematical functionsstatistics
: Statistical operationsbase64
: Base64 encoding/decodingurllib.parse
: URL parsing operationsrandom
: Random number generationtime
: Time operations
For the complete list of available functions and their safe implementations, refer to the utils.py file in the source code.
Example Usage
Here’s a practical example combining different aspects of Python expressions:
Security Note
All Python expressions are executed in a sandboxed environment with:
- Limited available functions
- Maximum string length restrictions
- Collection size limits
- Execution time limits
This ensures safe execution while providing necessary functionality for task workflows.