Skip to content

Commit

Permalink
isinstance() instead of eval (#75)
Browse files Browse the repository at this point in the history
* `isinstance()` instead of `eval`

* removed dbg code
  • Loading branch information
hrshdhgd authored Mar 29, 2024
1 parent 93c3f6b commit 0a37270
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 35 deletions.
20 changes: 10 additions & 10 deletions src/data/examples/examples.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Test_0:
id: CHANGE:001
type: NodeRename
old_value: '''nuclear envelope'''
new_value: '''foo bar'''
old_value: nuclear envelope
new_value: foo bar
about_node: GO:0005635
about_node_representation: curie
command_with_curie: rename GO:0005635 from 'nuclear envelope' to 'foo bar'
Expand All @@ -11,8 +11,8 @@ Test_0:
Test_1:
id: CHANGE:001
type: NodeRename
old_value: '''nucleus'''
new_value: '''bar'''
old_value: nucleus
new_value: bar
about_node: GO:0005634
about_node_representation: curie
command_with_curie: rename GO:0005634 from 'nucleus' to 'bar'
Expand Down Expand Up @@ -61,10 +61,10 @@ Test_6:
Test_7:
id: CHANGE:001
type: SynonymReplacement
old_value: '''cell nucleus'''
new_value: '''cell NUCLEUS'''
old_value: cell nucleus
new_value: cell NUCLEUS
about_node: GO:0005634
about_node_representation: curie
about_node_representation: label
command_with_curie: change synonym 'cell nucleus' for GO:0005634 to 'cell NUCLEUS'
command_with_uri: change synonym 'cell nucleus' for http://purl.obolibrary.org/obo/GO_0005634
to 'cell NUCLEUS'
Expand Down Expand Up @@ -97,7 +97,7 @@ Test_11:
about_node: GO:9999999
about_node_representation: curie
node_id: GO:9999999
name: '''foo'''
name: foo
command_with_curie: create node GO:9999999 'foo'
command_with_uri: TODO
Test_12:
Expand Down Expand Up @@ -199,15 +199,15 @@ Test_18:
Test_19:
id: CHANGE:001
type: NewTextDefinition
new_value: '''this is dummy description'''
new_value: this is dummy description
about_node: GO:0005635
about_node_representation: curie
command_with_curie: add definition 'this is dummy description' to GO:0005635
command_with_uri: add definition 'this is dummy description' to http://purl.obolibrary.org/obo/GO_0005635
Test_20:
id: CHANGE:001
type: NodeTextDefinitionChange
new_value: '''this is dummy description'''
new_value: this is dummy description
about_node: GO:0005635
about_node_representation: curie
command_with_curie: change definition of GO:0005635 to 'this is dummy description'
Expand Down
2 changes: 1 addition & 1 deletion src/docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type: SynonymReplacement
old_value: cell nucleus
new_value: cell NUCLEUS
about_node: GO:0005634
about_node_representation: curie
about_node_representation: label
```
## Example: Addition of a node to a subset.
Expand Down
30 changes: 21 additions & 9 deletions src/kgcl_schema/grammar/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,12 @@ def parse_remove_synonym(tree, id):
def parse_change_synonym(tree, id):
"""Change the synonym of a class."""
entity_token = extract(tree, "entity")
old_value = extract(tree, "synonym")
new_value = extract(tree, "new_synonym")
entity, representation = get_entity_representation(entity_token)
old_token = extract(tree, "synonym")
old_value, representation = get_entity_representation(old_token)
new_token = extract(tree, "new_synonym")
new_value, representation = get_entity_representation(new_token)

return SynonymReplacement(
id=id,
about_node=entity,
Expand All @@ -254,7 +257,8 @@ def parse_create_class(tree, id):
def parse_add_definition(tree, id):
"""Add definition to class."""
entity_token = extract(tree, "entity")
new_value = extract(tree, "new_definition")
new_token = extract(tree, "new_definition")
new_value, representation = get_entity_representation(new_token)
entity, representation = get_entity_representation(entity_token)
return NewTextDefinition(
id=id,
Expand All @@ -267,8 +271,14 @@ def parse_add_definition(tree, id):
def parse_change_definition(tree, id):
"""Change the definition of a class."""
entity_token = extract(tree, "entity")
old_value = extract(tree, "old_definition")
new_value = extract(tree, "new_definition")
old_token = extract(tree, "old_definition")
if old_token:
old_value, representation = get_entity_representation(old_token)
else:
old_value = None
representation = None
new_token = extract(tree, "new_definition")
new_value, representation = get_entity_representation(new_token)
entity, representation = get_entity_representation(entity_token)
return NodeTextDefinitionChange(
id=id,
Expand Down Expand Up @@ -298,6 +308,7 @@ def parse_create(tree, id):
"""Create a node."""
term_id_token = extract(tree, "id")
label_token = extract(tree, "label")
label_value, representation = get_entity_representation(label_token)
language_token = extract(tree, "language")

entity, representation = get_entity_representation(term_id_token)
Expand All @@ -307,7 +318,7 @@ def parse_create(tree, id):
about_node=entity,
about_node_representation=representation,
node_id=entity, # was term_id_token
name=label_token,
name=label_value,
language=language_token,
)

Expand Down Expand Up @@ -588,7 +599,9 @@ def parse_obsolete(tree, id):
def parse_rename(tree, id):
"""Rename a node."""
old_token = extract(tree, "old_label")
old_value, representation = get_entity_representation(old_token)
new_token = extract(tree, "new_label")
new_value, representation = get_entity_representation(new_token)
old_language = extract(tree, "old_language")
new_language = extract(tree, "new_language")

Expand All @@ -600,13 +613,12 @@ def parse_rename(tree, id):
else:
entity = None
representation = None

return NodeRename(
id=id,
about_node=entity,
about_node_representation=representation,
old_value=old_token,
new_value=new_token,
old_value=old_value,
new_value=new_value,
old_language=old_language,
new_language=new_language,
)
Expand Down
4 changes: 2 additions & 2 deletions src/kgcl_schema/grammar/render_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ def render_entity(entity, rdf_type):
entity = entity.replace("'", "")
return entity
elif rdf_type == "label":
if type(eval(entity)) == Token:
entity = eval(entity).value
if isinstance(entity, Token):
entity = entity.value
# elif "'" in entity:
# # TODO: replacing quotes with backticks
# # is only a temporary workaround
Expand Down
26 changes: 13 additions & 13 deletions tests/cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
f"rename {NUCLEAR_ENVELOPE_URI} from 'nuclear envelope' to 'foo bar'",
NodeRename(
id=UID,
old_value="'nuclear envelope'",
new_value="'foo bar'",
old_value="nuclear envelope",
new_value="foo bar",
about_node="GO:0005635",
about_node_representation="curie",
),
Expand All @@ -78,8 +78,8 @@
f"rename {NUCLEUS_URI} from 'nucleus' to 'bar'",
NodeRename(
id=UID,
old_value="'nucleus'",
new_value="'bar'",
old_value="nucleus",
new_value="bar",
about_node=NUCLEUS,
about_node_representation="curie",
),
Expand All @@ -90,8 +90,8 @@
# f"""rename {NUCLEUS} from 'nucleus' to 'bobby " tables'""",
# f"""rename {NUCLEUS_URI} from 'nucleus' to 'bobby " tables'""",
# NodeRename(id=UID,
# old_value="'nucleus'",
# new_value="'bar'",
# old_value="nucleus",
# new_value="bar",
# about_node=NUCLEUS,
# about_node_representation='curie'),
# None
Expand Down Expand Up @@ -155,9 +155,9 @@
SynonymReplacement(
id=UID,
about_node=NUCLEUS,
about_node_representation="curie",
old_value="'cell nucleus'",
new_value="'cell NUCLEUS'",
about_node_representation="label",
old_value="cell nucleus",
new_value="cell NUCLEUS",
),
None,
),
Expand Down Expand Up @@ -198,7 +198,7 @@
id=UID,
node_id=NEW_TERM, ## TODO: remove this
about_node=NEW_TERM,
name="'foo'",
name="foo",
about_node_representation="curie",
),
None,
Expand All @@ -212,7 +212,7 @@
# NodeCreation(id=UID,
# node_id=NEW_TERM, ## TODO: remove this
# about_node=NEW_TERM,
# name="'foo'",
# name="foo",
# about_node_representation='curie'),
# None
# ),
Expand Down Expand Up @@ -338,7 +338,7 @@
f"add definition 'this is dummy description' to {NUCLEAR_ENVELOPE_URI}",
NewTextDefinition(
id=UID,
new_value="'this is dummy description'",
new_value="this is dummy description",
about_node="GO:0005635",
about_node_representation="curie",
),
Expand All @@ -349,7 +349,7 @@
f"change definition of {NUCLEAR_ENVELOPE_URI} to 'this is dummy description'",
NodeTextDefinitionChange(
id=UID,
new_value="'this is dummy description'",
new_value="this is dummy description",
about_node="GO:0005635",
about_node_representation="curie",
),
Expand Down

0 comments on commit 0a37270

Please sign in to comment.