Skip to content

Commit 66c97fc

Browse files
authored
Merge pull request #393 from fschrader1992/validation
List-Style Tuple Integration
2 parents 441432b + 9eac34d commit 66c97fc

File tree

2 files changed

+59
-14
lines changed

2 files changed

+59
-14
lines changed

odml/property.py

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,39 @@ def odml_tuple_import(t_count, new_value):
3434
except NameError:
3535
unicode = str
3636

37-
if len(new_value) != 1 and not isinstance(new_value[0], unicode):
38-
return new_value
37+
if not isinstance(new_value, (list, tuple)) and \
38+
not isinstance(new_value[0], (list, tuple)):
39+
new_value = [new_value]
40+
41+
return_value = []
42+
43+
for n_val in new_value:
44+
if isinstance(n_val, (list, tuple)):
45+
if len(n_val) == t_count:
46+
n_val_str = "("
47+
for tuple_val in n_val:
48+
n_val_str += str(tuple_val) + "; "
49+
return_value += [n_val_str[:-2] + ")"]
50+
else:
51+
#non-unicode handling needed for python2
52+
if len(n_val) != 1 and not isinstance(n_val[0], unicode):
53+
n_val = n_val.encode('utf-8')
54+
cln = n_val.strip()
55+
br_check = cln.count("(") == cln.count(")")
56+
sep_check = t_count == 1 or cln.count("(") == (cln.count(";") / (t_count - 1))
3957

40-
cln = new_value[0].strip()
41-
l_check = cln.startswith("[") and cln.endswith("]")
42-
br_check = cln.count("(") == cln.count(")")
43-
com_check = cln.count("(") == (cln.count(",") + 1)
44-
sep_check = t_count == 1 or cln.count("(") == (cln.count(";") / (t_count - 1))
58+
if len(new_value) == 1 and cln.startswith("["):
59+
l_check = cln.startswith("[") and cln.endswith("]")
60+
com_check = cln.count("(") == (cln.count(",") + 1)
61+
if l_check and br_check and com_check and sep_check:
62+
return_value = cln[1:-1].split(",")
63+
elif br_check and sep_check:
64+
return_value += [cln]
4565

46-
if l_check and br_check and com_check and sep_check:
47-
new_value = cln[1:-1].split(",")
66+
if not return_value:
67+
return_value = new_value
4868

49-
return new_value
69+
return return_value
5070

5171

5272
@allow_inherit_docstring
@@ -774,6 +794,11 @@ def extend(self, obj, strict=True):
774794
return
775795

776796
new_value = self._convert_value_input(obj)
797+
798+
if self._dtype.endswith("-tuple"):
799+
t_count = int(self._dtype.split("-")[0])
800+
new_value = odml_tuple_import(t_count, new_value)
801+
777802
if len(new_value) > 0 and strict and \
778803
dtypes.infer_dtype(new_value[0]) != self.dtype:
779804

@@ -811,6 +836,10 @@ def append(self, obj, strict=True):
811836
if len(new_value) > 1:
812837
raise ValueError("odml.property.append: Use extend to add a list of values!")
813838

839+
if self._dtype.endswith("-tuple"):
840+
t_count = int(self._dtype.split("-")[0])
841+
new_value = odml_tuple_import(t_count, new_value)
842+
814843
if len(new_value) > 0 and strict and \
815844
dtypes.infer_dtype(new_value[0]) != self.dtype:
816845

test/test_property.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ def test_value(self):
124124
with self.assertRaises(ValueError):
125125
Property(name="intprop", dtype=DType.int, value=[2, "Hello!", 4])
126126

127+
prop6 = Property('myprop', values=["(8; 9; 10)", "(11; 12; 13)"], dtype="3-tuple")
128+
self.assertEqual(len(prop6.values), 2)
129+
130+
prop7 = Property('myprop', values=[["0", "1", "2"], [3, 4, 5]], dtype="3-tuple")
131+
self.assertEqual(len(prop7.values), 2)
132+
133+
prop8 = Property('myprop', values=["(8; 9; 10)", ["0", "1", "2"], [3, 4, 5]], dtype="3-tuple")
134+
self.assertEqual(len(prop8.values), 3)
135+
136+
127137
def test_value_append(self):
128138
# Test append w/o Property value or dtype
129139
prop = Property(name="append")
@@ -231,6 +241,9 @@ def test_value_append(self):
231241
prop9.append("(7; 8; 9)")
232242
self.assertEqual(len(prop9), 2)
233243
self.assertRaises(ValueError, prop9.append, "(10; 11)")
244+
prop9.append([[2, 3, 4]])
245+
self.assertEqual(len(prop9), 3)
246+
self.assertRaises(ValueError, prop9.append, [[10, 11]])
234247

235248
def test_value_extend(self):
236249
prop = Property(name="extend")
@@ -332,10 +345,13 @@ def test_value_extend(self):
332345
self.assertRaises(ValueError, prop3.extend, 1.3)
333346
self.assertRaises(ValueError, prop3.extend, True)
334347

335-
prop = Property(name="tuple-test", dtype="3-tuple", values="(1; 2; 3)")
336-
prop.extend(["(7; 8; 9)", "(10; 11; 12)"])
337-
self.assertEqual(len(prop), 3)
338-
self.assertRaises(ValueError, prop.extend, "(10; 11)")
348+
prop4 = Property(name="tuple-test", dtype="3-tuple", values="(1; 2; 3)")
349+
prop4.extend(["(7; 8; 9)", "(10; 11; 12)"])
350+
self.assertEqual(len(prop4), 3)
351+
self.assertRaises(ValueError, prop4.extend, "(10; 11)")
352+
prop4.extend([[2, 3, 4], [5, 6, 7]])
353+
self.assertEqual(len(prop4), 5)
354+
self.assertRaises(ValueError, prop4.extend, [[10, 11]])
339355

340356
def test_get_set_value(self):
341357
values = [1, 2, 3, 4, 5]

0 commit comments

Comments
 (0)