Skip to content

Commit eb48876

Browse files
committed
[Property] Add 'values' accessor methods
1 parent 31ce79a commit eb48876

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

odml/property.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,64 @@ def value(self, new_value):
310310
"consistent type!")
311311
self._values = [dtypes.get(v, self.dtype) for v in new_value]
312312

313+
@property
314+
def values(self):
315+
"""
316+
Returns the value(s) stored in this property. Method always returns a list
317+
that is a copy (!) of the stored value. Changing this list will NOT change
318+
the property.
319+
For manipulation of the stored values use the append, extend, and direct
320+
access methods (using brackets).
321+
322+
For example:
323+
>>> p = odml.Property("prop", values=[1, 2, 3])
324+
>>> print(p.values)
325+
[1, 2, 3]
326+
>>> p.values.append(4)
327+
>>> print(p.values)
328+
[1, 2, 3]
329+
330+
Individual values can be accessed and manipulated like this:
331+
>>> print(p[0])
332+
[1]
333+
>>> p[0] = 4
334+
>>> print(p[0])
335+
[4]
336+
337+
The values can be iterated e.g. with a loop:
338+
>>> for v in p.values:
339+
>>> print(v)
340+
4
341+
2
342+
3
343+
"""
344+
return list(self._values)
345+
346+
@values.setter
347+
def values(self, new_value):
348+
"""
349+
Set the values of the property discarding any previous information.
350+
Method will try to convert the passed value to the dtype of
351+
the property and raise a ValueError if not possible.
352+
353+
:param new_value: a single value or list of values.
354+
"""
355+
# Make sure boolean value 'False' gets through as well...
356+
if new_value is None or \
357+
(isinstance(new_value, (list, tuple, str)) and len(new_value) == 0):
358+
self._values = []
359+
return
360+
361+
new_value = self._convert_value_input(new_value)
362+
363+
if self._dtype is None:
364+
self._dtype = dtypes.infer_dtype(new_value[0])
365+
366+
if not self._validate_values(new_value):
367+
raise ValueError("odml.Property.value: passed values are not of "
368+
"consistent type!")
369+
self._values = [dtypes.get(v, self.dtype) for v in new_value]
370+
313371
@property
314372
def value_origin(self):
315373
return self._value_origin

0 commit comments

Comments
 (0)