Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_attr(self):
self.assertEqual(self.pl.tail, InputList([2, 4, 8]),
"attribute access does not give correct element")
self.assertEqual(self.pl[3].next, InputList([0, InputList({"depth": 23})]),
"nested attribute access does not give correct element")
def setUpClass(cls):
cls.pl = InputList([
{"foo": "bar"},
2,
42,
{"next":
[0,
{"depth": 23}
]
}
], table_name = "input")
cls.pl["tail"] = InputList([2,4,8])
file_location = os.path.dirname(os.path.abspath(__file__))
pr = Project(file_location)
cls.file_name = os.path.join(file_location, "input.h5")
cls.hdf = ProjectHDFio(project=pr, file_name=cls.file_name,
h5_path="/test", mode="a")
def test_init_dict(self):
d = {"foo": 23, "test case": "bar"}
pl = InputList(d)
self.assertEqual(tuple(pl.items()), tuple(d.items()),
"source dict items not preserved")
with self.assertRaises(ValueError,
msg = "no ValueError on invalid initializer"):
pl = InputList({2: 0, 1: 1})
def test_get_attr(self):
self.assertEqual(self.pl.tail, InputList([2, 4, 8]),
"attribute access does not give correct element")
self.assertEqual(self.pl[3].next, InputList([0, InputList({"depth": 23})]),
"nested attribute access does not give correct element")
def test_to_hdf_group(self):
self.pl.to_hdf(hdf=self.hdf, group_name = "test_group")
self.assertEqual(self.hdf["test_group/NAME"],
"InputList")
self.assertEqual(self.hdf["test_group/TYPE"],
"")
self.assertEqual(self.hdf["test_group/OBJECT"],
"InputList")
l = InputList(self.hdf["test_group/data"])
self.assertEqual(self.pl, l)
def test_extend(self):
pl = InputList()
pl.extend([1,2,3])
self.assertEqual(list(pl.values()), [1,2,3],
"extend from list does not set values")
def test_get_nested(self):
n = [
{"foo": "bar"},
2,
42,
{"next":
[0,
{"depth": 23}
]
}
]
pl = InputList(n)
self.assertEqual(type(pl[0]), InputList,
"nested dict not converted to InputList")
self.assertEqual(type(pl["3/next"]), InputList,
"nested list not converted to InputList")
self.assertEqual(type(pl["0/foo"]), str,
"nested str converted to InputList")
def test_del_item(self):
pl = InputList({0: 1, "a": 2, "foo": 3})
with self.assertRaises(ValueError,
msg = "no ValueError on invalid index type"):
del pl[{}]
del pl["a"]
self.assertTrue("a" not in pl, "delitem does not delete with str key")
del pl[0]
self.assertTrue(pl[0] != 1, "delitem does not delete with index")
def test_mark(self):
pl = InputList([1,2,3])
pl.mark(1, "foo")
self.assertEqual(pl[1], pl.foo,
"marked element does not refer to correct element")
pl.mark(2, "foo")
self.assertEqual(pl[2], pl.foo,
"marking with existing key broken")
with self.assertRaises(IndexError,
msg = "no IndexError on invalid index"):
pl.mark(10, "foo")
dd = {}
for k, v in self.items():
# force all string keys in output to work with h5io (it
# requires all string keys when storing as json), since
# _normalize calls int() on all digit string keys this is
# transparent for the rest of the module
k = str(k)
if isinstance(v, InputList):
dd[k] = v.to_builtin(stringify = stringify)
else:
dd[k] = repr(v) if stringify else v
return dd
elif stringify:
return list(v.to_builtin(stringify = stringify)
if isinstance(v, InputList) else repr(v)
for v in self.values())
else:
return list(v.to_builtin(stringify = stringify)
if isinstance(v, InputList) else v
for v in self.values())