Skip to content

Commit 0a37270

Browse files
authored
isinstance() instead of eval (#75)
* `isinstance()` instead of `eval` * removed dbg code
1 parent 93c3f6b commit 0a37270

File tree

5 files changed

+47
-35
lines changed

5 files changed

+47
-35
lines changed

src/data/examples/examples.yaml

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
Test_0:
22
id: CHANGE:001
33
type: NodeRename
4-
old_value: '''nuclear envelope'''
5-
new_value: '''foo bar'''
4+
old_value: nuclear envelope
5+
new_value: foo bar
66
about_node: GO:0005635
77
about_node_representation: curie
88
command_with_curie: rename GO:0005635 from 'nuclear envelope' to 'foo bar'
@@ -11,8 +11,8 @@ Test_0:
1111
Test_1:
1212
id: CHANGE:001
1313
type: NodeRename
14-
old_value: '''nucleus'''
15-
new_value: '''bar'''
14+
old_value: nucleus
15+
new_value: bar
1616
about_node: GO:0005634
1717
about_node_representation: curie
1818
command_with_curie: rename GO:0005634 from 'nucleus' to 'bar'
@@ -61,10 +61,10 @@ Test_6:
6161
Test_7:
6262
id: CHANGE:001
6363
type: SynonymReplacement
64-
old_value: '''cell nucleus'''
65-
new_value: '''cell NUCLEUS'''
64+
old_value: cell nucleus
65+
new_value: cell NUCLEUS
6666
about_node: GO:0005634
67-
about_node_representation: curie
67+
about_node_representation: label
6868
command_with_curie: change synonym 'cell nucleus' for GO:0005634 to 'cell NUCLEUS'
6969
command_with_uri: change synonym 'cell nucleus' for http://purl.obolibrary.org/obo/GO_0005634
7070
to 'cell NUCLEUS'
@@ -97,7 +97,7 @@ Test_11:
9797
about_node: GO:9999999
9898
about_node_representation: curie
9999
node_id: GO:9999999
100-
name: '''foo'''
100+
name: foo
101101
command_with_curie: create node GO:9999999 'foo'
102102
command_with_uri: TODO
103103
Test_12:
@@ -199,15 +199,15 @@ Test_18:
199199
Test_19:
200200
id: CHANGE:001
201201
type: NewTextDefinition
202-
new_value: '''this is dummy description'''
202+
new_value: this is dummy description
203203
about_node: GO:0005635
204204
about_node_representation: curie
205205
command_with_curie: add definition 'this is dummy description' to GO:0005635
206206
command_with_uri: add definition 'this is dummy description' to http://purl.obolibrary.org/obo/GO_0005635
207207
Test_20:
208208
id: CHANGE:001
209209
type: NodeTextDefinitionChange
210-
new_value: '''this is dummy description'''
210+
new_value: this is dummy description
211211
about_node: GO:0005635
212212
about_node_representation: curie
213213
command_with_curie: change definition of GO:0005635 to 'this is dummy description'

src/docs/examples.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type: SynonymReplacement
5959
old_value: cell nucleus
6060
new_value: cell NUCLEUS
6161
about_node: GO:0005634
62-
about_node_representation: curie
62+
about_node_representation: label
6363
6464
```
6565
## Example: Addition of a node to a subset.

src/kgcl_schema/grammar/parser.py

+21-9
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,12 @@ def parse_remove_synonym(tree, id):
229229
def parse_change_synonym(tree, id):
230230
"""Change the synonym of a class."""
231231
entity_token = extract(tree, "entity")
232-
old_value = extract(tree, "synonym")
233-
new_value = extract(tree, "new_synonym")
234232
entity, representation = get_entity_representation(entity_token)
233+
old_token = extract(tree, "synonym")
234+
old_value, representation = get_entity_representation(old_token)
235+
new_token = extract(tree, "new_synonym")
236+
new_value, representation = get_entity_representation(new_token)
237+
235238
return SynonymReplacement(
236239
id=id,
237240
about_node=entity,
@@ -254,7 +257,8 @@ def parse_create_class(tree, id):
254257
def parse_add_definition(tree, id):
255258
"""Add definition to class."""
256259
entity_token = extract(tree, "entity")
257-
new_value = extract(tree, "new_definition")
260+
new_token = extract(tree, "new_definition")
261+
new_value, representation = get_entity_representation(new_token)
258262
entity, representation = get_entity_representation(entity_token)
259263
return NewTextDefinition(
260264
id=id,
@@ -267,8 +271,14 @@ def parse_add_definition(tree, id):
267271
def parse_change_definition(tree, id):
268272
"""Change the definition of a class."""
269273
entity_token = extract(tree, "entity")
270-
old_value = extract(tree, "old_definition")
271-
new_value = extract(tree, "new_definition")
274+
old_token = extract(tree, "old_definition")
275+
if old_token:
276+
old_value, representation = get_entity_representation(old_token)
277+
else:
278+
old_value = None
279+
representation = None
280+
new_token = extract(tree, "new_definition")
281+
new_value, representation = get_entity_representation(new_token)
272282
entity, representation = get_entity_representation(entity_token)
273283
return NodeTextDefinitionChange(
274284
id=id,
@@ -298,6 +308,7 @@ def parse_create(tree, id):
298308
"""Create a node."""
299309
term_id_token = extract(tree, "id")
300310
label_token = extract(tree, "label")
311+
label_value, representation = get_entity_representation(label_token)
301312
language_token = extract(tree, "language")
302313

303314
entity, representation = get_entity_representation(term_id_token)
@@ -307,7 +318,7 @@ def parse_create(tree, id):
307318
about_node=entity,
308319
about_node_representation=representation,
309320
node_id=entity, # was term_id_token
310-
name=label_token,
321+
name=label_value,
311322
language=language_token,
312323
)
313324

@@ -588,7 +599,9 @@ def parse_obsolete(tree, id):
588599
def parse_rename(tree, id):
589600
"""Rename a node."""
590601
old_token = extract(tree, "old_label")
602+
old_value, representation = get_entity_representation(old_token)
591603
new_token = extract(tree, "new_label")
604+
new_value, representation = get_entity_representation(new_token)
592605
old_language = extract(tree, "old_language")
593606
new_language = extract(tree, "new_language")
594607

@@ -600,13 +613,12 @@ def parse_rename(tree, id):
600613
else:
601614
entity = None
602615
representation = None
603-
604616
return NodeRename(
605617
id=id,
606618
about_node=entity,
607619
about_node_representation=representation,
608-
old_value=old_token,
609-
new_value=new_token,
620+
old_value=old_value,
621+
new_value=new_value,
610622
old_language=old_language,
611623
new_language=new_language,
612624
)

src/kgcl_schema/grammar/render_operations.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ def render_entity(entity, rdf_type):
4545
entity = entity.replace("'", "")
4646
return entity
4747
elif rdf_type == "label":
48-
if type(eval(entity)) == Token:
49-
entity = eval(entity).value
48+
if isinstance(entity, Token):
49+
entity = entity.value
5050
# elif "'" in entity:
5151
# # TODO: replacing quotes with backticks
5252
# # is only a temporary workaround

tests/cases.py

+13-13
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@
6666
f"rename {NUCLEAR_ENVELOPE_URI} from 'nuclear envelope' to 'foo bar'",
6767
NodeRename(
6868
id=UID,
69-
old_value="'nuclear envelope'",
70-
new_value="'foo bar'",
69+
old_value="nuclear envelope",
70+
new_value="foo bar",
7171
about_node="GO:0005635",
7272
about_node_representation="curie",
7373
),
@@ -78,8 +78,8 @@
7878
f"rename {NUCLEUS_URI} from 'nucleus' to 'bar'",
7979
NodeRename(
8080
id=UID,
81-
old_value="'nucleus'",
82-
new_value="'bar'",
81+
old_value="nucleus",
82+
new_value="bar",
8383
about_node=NUCLEUS,
8484
about_node_representation="curie",
8585
),
@@ -90,8 +90,8 @@
9090
# f"""rename {NUCLEUS} from 'nucleus' to 'bobby " tables'""",
9191
# f"""rename {NUCLEUS_URI} from 'nucleus' to 'bobby " tables'""",
9292
# NodeRename(id=UID,
93-
# old_value="'nucleus'",
94-
# new_value="'bar'",
93+
# old_value="nucleus",
94+
# new_value="bar",
9595
# about_node=NUCLEUS,
9696
# about_node_representation='curie'),
9797
# None
@@ -155,9 +155,9 @@
155155
SynonymReplacement(
156156
id=UID,
157157
about_node=NUCLEUS,
158-
about_node_representation="curie",
159-
old_value="'cell nucleus'",
160-
new_value="'cell NUCLEUS'",
158+
about_node_representation="label",
159+
old_value="cell nucleus",
160+
new_value="cell NUCLEUS",
161161
),
162162
None,
163163
),
@@ -198,7 +198,7 @@
198198
id=UID,
199199
node_id=NEW_TERM, ## TODO: remove this
200200
about_node=NEW_TERM,
201-
name="'foo'",
201+
name="foo",
202202
about_node_representation="curie",
203203
),
204204
None,
@@ -212,7 +212,7 @@
212212
# NodeCreation(id=UID,
213213
# node_id=NEW_TERM, ## TODO: remove this
214214
# about_node=NEW_TERM,
215-
# name="'foo'",
215+
# name="foo",
216216
# about_node_representation='curie'),
217217
# None
218218
# ),
@@ -338,7 +338,7 @@
338338
f"add definition 'this is dummy description' to {NUCLEAR_ENVELOPE_URI}",
339339
NewTextDefinition(
340340
id=UID,
341-
new_value="'this is dummy description'",
341+
new_value="this is dummy description",
342342
about_node="GO:0005635",
343343
about_node_representation="curie",
344344
),
@@ -349,7 +349,7 @@
349349
f"change definition of {NUCLEAR_ENVELOPE_URI} to 'this is dummy description'",
350350
NodeTextDefinitionChange(
351351
id=UID,
352-
new_value="'this is dummy description'",
352+
new_value="this is dummy description",
353353
about_node="GO:0005635",
354354
about_node_representation="curie",
355355
),

0 commit comments

Comments
 (0)