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:

贡献者指南