openPMD/openPMD-api

Attribute Getting: Default Param

開放

#477 建立於 2019年2月17日

 (2 則留言) (0 個反應) (0 位負責人)C++ (57 個分叉)auto 404
frontend: C++17good first issuehelp wanted

倉庫指標

星標
 (160 顆星)
PR 合併指標
 (PR 指標待抓取)

描述

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:

貢獻者指南