Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 39 additions & 10 deletions odml/property.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,39 @@ def odml_tuple_import(t_count, new_value):
except NameError:
unicode = str

if len(new_value) != 1 and not isinstance(new_value[0], unicode):
return new_value
if not isinstance(new_value, (list, tuple)) and \
not isinstance(new_value[0], (list, tuple)):
new_value = [new_value]

return_value = []

for n_val in new_value:
if isinstance(n_val, (list, tuple)):
if len(n_val) == t_count:
n_val_str = "("
for tuple_val in n_val:
n_val_str += str(tuple_val) + "; "
return_value += [n_val_str[:-2] + ")"]
else:
#non-unicode handling needed for python2
if len(n_val) != 1 and not isinstance(n_val[0], unicode):
n_val = n_val.encode('utf-8')
cln = n_val.strip()
br_check = cln.count("(") == cln.count(")")
sep_check = t_count == 1 or cln.count("(") == (cln.count(";") / (t_count - 1))

cln = new_value[0].strip()
l_check = cln.startswith("[") and cln.endswith("]")
br_check = cln.count("(") == cln.count(")")
com_check = cln.count("(") == (cln.count(",") + 1)
sep_check = t_count == 1 or cln.count("(") == (cln.count(";") / (t_count - 1))
if len(new_value) == 1 and cln.startswith("["):
l_check = cln.startswith("[") and cln.endswith("]")
com_check = cln.count("(") == (cln.count(",") + 1)
if l_check and br_check and com_check and sep_check:
return_value = cln[1:-1].split(",")
elif br_check and sep_check:
return_value += [cln]

if l_check and br_check and com_check and sep_check:
new_value = cln[1:-1].split(",")
if not return_value:
return_value = new_value

return new_value
return return_value


@allow_inherit_docstring
Expand Down Expand Up @@ -774,6 +794,11 @@ def extend(self, obj, strict=True):
return

new_value = self._convert_value_input(obj)

if self._dtype.endswith("-tuple"):
t_count = int(self._dtype.split("-")[0])
new_value = odml_tuple_import(t_count, new_value)

if len(new_value) > 0 and strict and \
dtypes.infer_dtype(new_value[0]) != self.dtype:

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

if self._dtype.endswith("-tuple"):
t_count = int(self._dtype.split("-")[0])
new_value = odml_tuple_import(t_count, new_value)

if len(new_value) > 0 and strict and \
dtypes.infer_dtype(new_value[0]) != self.dtype:

Expand Down
24 changes: 20 additions & 4 deletions test/test_property.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,16 @@ def test_value(self):
with self.assertRaises(ValueError):
Property(name="intprop", dtype=DType.int, value=[2, "Hello!", 4])

prop6 = Property('myprop', values=["(8; 9; 10)", "(11; 12; 13)"], dtype="3-tuple")
self.assertEqual(len(prop6.values), 2)

prop7 = Property('myprop', values=[["0", "1", "2"], [3, 4, 5]], dtype="3-tuple")
self.assertEqual(len(prop7.values), 2)

prop8 = Property('myprop', values=["(8; 9; 10)", ["0", "1", "2"], [3, 4, 5]], dtype="3-tuple")
self.assertEqual(len(prop8.values), 3)


def test_value_append(self):
# Test append w/o Property value or dtype
prop = Property(name="append")
Expand Down Expand Up @@ -231,6 +241,9 @@ def test_value_append(self):
prop9.append("(7; 8; 9)")
self.assertEqual(len(prop9), 2)
self.assertRaises(ValueError, prop9.append, "(10; 11)")
prop9.append([[2, 3, 4]])
self.assertEqual(len(prop9), 3)
self.assertRaises(ValueError, prop9.append, [[10, 11]])

def test_value_extend(self):
prop = Property(name="extend")
Expand Down Expand Up @@ -332,10 +345,13 @@ def test_value_extend(self):
self.assertRaises(ValueError, prop3.extend, 1.3)
self.assertRaises(ValueError, prop3.extend, True)

prop = Property(name="tuple-test", dtype="3-tuple", values="(1; 2; 3)")
prop.extend(["(7; 8; 9)", "(10; 11; 12)"])
self.assertEqual(len(prop), 3)
self.assertRaises(ValueError, prop.extend, "(10; 11)")
prop4 = Property(name="tuple-test", dtype="3-tuple", values="(1; 2; 3)")
prop4.extend(["(7; 8; 9)", "(10; 11; 12)"])
self.assertEqual(len(prop4), 3)
self.assertRaises(ValueError, prop4.extend, "(10; 11)")
prop4.extend([[2, 3, 4], [5, 6, 7]])
self.assertEqual(len(prop4), 5)
self.assertRaises(ValueError, prop4.extend, [[10, 11]])

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