G-Node / G-Node/nixpy

getitem of sections should treat props and sections identically.

Open
#240 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
22
Forks
29
PR merge metrics
No merged PRs in 30d

Description

There are some inconsistencies, I believe, in sections/properties.

When accessing an item from a section by key, e.g. `section['name']`, the returned value is treated differently depending whether the key is a property or a section as can be sen [here](https://github.com/G-Node/nixpy/blob/master/nixio/section.py#L139).

If it's a section, the section is returned. If it's a prop, if it's a single `Value` prop, the `values[0].value` is returned, otherwise a the `Value`'s, `values` are returned. It think in the zen of python that explicit is better than implicit, we shouldn't do that.

At best, I think we should always return the `Value`'s, `values`, even if there's only one. We shouldn't guess that a single valued prop is different than multiple valued prop. Unless the underlying code will always treat a single value different than multiple values (i.e. ValueList and Value, although this seems like a bad idea).

Or in my opinion, we should never return the `Value`'s, `values`, but rather the `Value` itself. Just like we return the `Section` if it's a section. This would even allow us to do `if isinstance` on the returned value.

When assigning to a section or property(?) we can do `section['name'] = 'stuff'` which creates a single valued `Value`, so I can see the desire to also return the first item of the `Value` if there's only one. But I think users will be able to distinguish between allowing that assignment for ease of use, and always getting a `Value` when doing getitem, especially if it's documented. On the flip side, guessing what the user wants will lead to confusion, even if documented.

I believe that method should look as follows:

```py
def __getitem__(self, key):
if key not in self.props and key in self.sections:
return self.sections[key]
return self.props[key]
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.