simphony / simphony-osp

A framework that aims to achieve interoperability between software such as simulation engines, databases and data repositories using a knowledge graph as the common language.
https://simphony.readthedocs.io
Other
16 stars 12 forks source link

Syntactic sugar for a series of get calls #491

Open yoavnash opened 4 years ago

yoavnash commented 4 years ago

Often an interesting value hides inside some internal CUDS object, which requires to do a few get calls in order to fetch it. For example:

isbn = book.get(oclass="metadata")[0].get(oclass="isbn")[0].value # snippet 1

This is quite cumbersome, and therefore I often prefer to define often-accessed values as attributes. That is:

isbn = book.isbn  # snippet 2

Is there a way to have a dot notation that is just syntactic sugar for a series of get calls for a more complex CUDS structure? So the user uses the code as in snippet 2 but behind the scenes a series of get calls is run, as in snippet 1. Another thing to point out here is that the user should be able to define these "shortcuts" by himself, probably in the YAML file.

What do you think?

yoavnash commented 4 years ago

Perhaps related to #416

urbanmatthias commented 4 years ago

I guess we could have a solution, where we define such shortcuts somewhere. I'm not sure whether we should, though...

What do you think @ahashibon

yoavnash commented 4 years ago

Why shouldn't we? Perhaps we could use another kind of syntax if we don't want to overload the dot notation.

urbanmatthias commented 4 years ago

Yeah, I don't know if overloading the dot notation is wise.

Any ideas for an alternative syntax?

yoavnash commented 4 years ago

How about isbn = book.get(attr=ns.isbn) ?

yoavnash commented 4 years ago

Another suggestion: support aliases defined in the code. As an illustration:

set_alias(alias='position', oclass=onto.Atom, func=lambda atom: atom.get(onto.Position)[0].vector)
...
a = onto.Atom()
...
p = a.position
urbanmatthias commented 4 years ago

I think this could become handy 👍