Conversation
| """ | ||
| meta-circular interpreter for evaluating python expressions | ||
|
|
||
| - See `<https://stackoverflow.com/questions/2371436/evaluating-a-mathematical-expression-in-a-string>`_ | ||
| """ |
There was a problem hiding this comment.
copied this file over from linkml_runtime as I needed to hack the existing eval_expr function a bit.
| if expr == "None": | ||
| # TODO: do this as part of parsing | ||
| return None |
There was a problem hiding this comment.
changed this from if "None" in expr to make it less permissive
There was a problem hiding this comment.
...although I think it needs to go completely. Not sure if there are cases in the main codebase where this functionality is required.
There was a problem hiding this comment.
That is the only change to this file.
| from dataclasses import dataclass | ||
| from typing import Any, Dict, Optional, Type, Union | ||
|
|
||
| from asteval import Interpreter |
There was a problem hiding this comment.
Had a look at various different packages for "safely" evaluating strings as python. This one seemed fairly reasonable in allowing a range of useful operations. Obviously evaluating user input is always going to be dodgy, and it's particularly bad in python. Anyway, I wanted something that would allow me to demonstrate the kinds of transform that I will need to do whilst making it slightly harder for potential hackers who are keenly monitoring this repo to take over everyone's computers.
| aeval = Interpreter(usersyms={"src": ctxt_obj, "target": None}) | ||
| aeval(slot_derivation.expr) | ||
| v = aeval.symtable["target"] |
There was a problem hiding this comment.
This sets up the context object as the variable src and gets the slot value from the variable target. Example transform:
d_test = [x.important_event_date for x in src.has_important_life_events if str(x.event_name) == "PASSED_DRIVING_TEST"]
target = d_test[0] if d_test else NoneIf you use **ctxt_dict, it is easy to accidentally have a variable with the same name as python function/keyword, hence restricting everything to be under src.
tests/input/examples/personinfo_basic/data/Agent-expected-001.yaml
Outdated
Show resolved
Hide resolved
…ples folders Some general documentation and tidying
Codecov Report
❗ Your organization is not using the GitHub App Integration. As a result you may experience degraded service beginning May 15th. Please install the Github App Integration for your organization. Read more. @@ Coverage Diff @@
## main #8 +/- ##
==========================================
- Coverage 86.51% 84.75% -1.77%
==========================================
Files 20 21 +1
Lines 749 846 +97
==========================================
+ Hits 648 717 +69
- Misses 101 129 +28
|
| {%- if '\n' in sd.expr -%} | ||
| gen_{{ sd.name }}(source_object) | ||
| {%- elif '{' in sd.expr and '}' in sd.expr -%} | ||
| {{ sd.expr|replace('{', '')|replace('}', '') }} | ||
| {%- else -%} |
There was a problem hiding this comment.
reproduce multi-line expressions and var names with { and } around them
| if expr == "None": | ||
| # TODO: do this as part of parsing | ||
| return None |
There was a problem hiding this comment.
...although I think it needs to go completely. Not sure if there are cases in the main codebase where this functionality is required.
| source_type = type(source_obj) | ||
| source_type_name = source_type.__name__ |
There was a problem hiding this comment.
renamed for clarity
| secondary_email: | ||
| expr: "NULL" | ||
|
|
||
| # FIXME: this should return the type "None", rather than null |
There was a problem hiding this comment.
This is dodgy -- not sure what the intention is, whether "None" should be treated as a string by the transformer or whether it should be treated as null (the current MO). IMHO it should be a string as "NULL" already sets the field to null in the transformation.
A few examples of some simple transforms that I would like to do, plus a possible implementation using the
astevalpackage.Forgot to add the
astevalpackage to the toml file so all the tests are failing ATM. Everything also needs to be updated to take into account the new changes from the compiler addition PR.