simphony-osp icon indicating copy to clipboard operation
simphony-osp copied to clipboard

Syntactic sugar for a series of get calls

Open yoavnash opened this issue 5 years ago • 7 comments

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 avatar Aug 19 '20 07:08 yoavnash

Perhaps related to #416

yoavnash avatar Aug 19 '20 08:08 yoavnash

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

urbanmatthias avatar Aug 19 '20 09:08 urbanmatthias

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

yoavnash avatar Aug 19 '20 10:08 yoavnash

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

Any ideas for an alternative syntax?

urbanmatthias avatar Aug 19 '20 11:08 urbanmatthias

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

yoavnash avatar Aug 26 '20 11:08 yoavnash

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

yoavnash avatar Aug 31 '20 09:08 yoavnash

I think this could become handy 👍

urbanmatthias avatar Aug 31 '20 09:08 urbanmatthias