Skip to content

Commit 17a6651

Browse files
authored
Merge pull request #15 from ndsev/release/0.7.0
Release 0.7.0
2 parents f4fdc16 + a6bec07 commit 17a6651

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,9 @@ zs-yaml comes with several built-in transformation functions that can be used in
222222
- `insert_yaml`: Inserts YAML content directly from an external file.
223223
- `repeat_node`: Repeats a specific node a specified number of times.
224224
- `extract_extern_as_yaml`: Extracts binary data and saves it as an external YAML file.
225+
- `py_eval`: Allows to write small snippets like `myArray: {_f: py_eval, _a: "list(range(1, 100))"}` to generate the value for a yaml node.
225226

226-
For more detailed information about these functions and their usage, please refer to the [built_in_transformations.py](https://github.com/ndsev/zs_yaml/blob/main/zs_yaml/built_in_transformations.py) source file.
227+
For more detailed information about these functions and their usage, please refer to the [built_in_transformations.py](https://github.com/ndsev/zs-yaml/blob/main/zs_yaml/built_in_transformations.py) source file.
227228

228229
Note: We plan to implement automatic source documentation generation in a future release, which will provide more comprehensive information about these functions and their parameters.
229230

zs_yaml/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
__commit_id__ = '7217bfe'
2-
__version__ = '0.6.1'
2+
__version__ = '0.7.0'

zs_yaml/built_in_transformations.py

+36
Original file line numberDiff line numberDiff line change
@@ -230,3 +230,39 @@ def rm_nulls(data):
230230
'file': file_name
231231
}
232232
}
233+
234+
def py_eval(transformer, expr):
235+
"""
236+
Safely evaluate a Python expression and return its result.
237+
238+
Args:
239+
transformer (YamlTransformer): The transformer instance.
240+
expr (str): The Python expression to evaluate.
241+
242+
Returns:
243+
The result of the evaluated expression.
244+
245+
Example YAML usage:
246+
my_array:
247+
_f: py_eval
248+
_a:
249+
expr: "[i * 2 for i in range(5)]"
250+
"""
251+
# Create a safe globals dict with limited builtins
252+
safe_globals = {
253+
'range': range,
254+
'len': len,
255+
'str': str,
256+
'int': int,
257+
'float': float,
258+
'list': list,
259+
'dict': dict,
260+
'set': set,
261+
'tuple': tuple,
262+
'bool': bool,
263+
}
264+
265+
try:
266+
return eval(expr, safe_globals, {})
267+
except Exception as e:
268+
raise ValueError(f"Error evaluating Python expression: {str(e)}")

0 commit comments

Comments
 (0)