openPMD/openPMD-api

Attribute Getting: Default Param

Aperta

#477 aperta il 17 feb 2019

 (2 commenti) (0 reazioni) (0 assegnatari)C++ (57 fork)auto 404
frontend: C++17good first issuehelp wanted

Metriche repository

Star
 (160 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

While reading attributes, it would be useful for many cases to return an "empty default" instead of throwing an openPMD::no_such_attribute_error on missing.

Therefore, we should introduce an additional, optional parameter in e.g. Series.author() and Attribute.get() that if set, is returned in case of exceptions (instead of an exception).

This is similar to Python's dict() handling: Python:

dict = {'A': 'One', 'B': 2}

d = {'A': 'One', 'B': 2}

d.get('A')
# 'One'
d.get('B')
# 2

d.get('C')
#
d.get('C') == None
# True
# d.get('C', 'fallback')
# 'fallback'

or C++

// ...

auto s = openPMD::Series(/* ... */);

s.author();
// might throw

s.author("");
// new variant 1: return empty string if attribute is missing

// variant 2: let openPMD-class "attribute getters"
// return by default the empty variant of itself as a
// fallback:
s.author();
// "" or attribute value


// now to "Attributable"

s.getAttribute("key");
// might throw

s.getAttribute("key", "");
// new variant 1: fallback to empty string

s.getAttribute("key");
// new variant 2: return empty string if attribute is missing

// note: actually in `Attribute::get()`
// but that might be too late

Current handling with containsAttribute(key) or try-catch are a bit too verbose.

We could also consider returning an std::optional. C++11 implementations:

Or using std::monostate with our variant:

Guida contributor