Skip to content

Commit

Permalink
Fix Issue setitem on Data with tuple #12
Browse files Browse the repository at this point in the history
  • Loading branch information
gb119 committed May 19, 2017
1 parent b1a7a77 commit 458eca3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
29 changes: 24 additions & 5 deletions Stoner/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ def __lookup__(self,name,multiple=False,exact=False):
KeyError: if no key matches name.
"""
ret=None
if not exact and not isinstance(name,string_types):
name=repr(name)
try: #name directly as key
super(regexpDict,self).__getitem__(name)
ret=name
Expand Down Expand Up @@ -2675,12 +2677,29 @@ def __setitem__(self, name, value):
"""Called for :py:class:`DataFile`[name ] = value to write mewtadata entries.
Args:
name (string): The string key used to access the metadata
value (any): The value to be written into the metadata. Currently bool, int, float and string values are correctly handled. Everythign else is treated as a string.
name (string, tuple): The string key used to access the metadata or a tuple index into data
value (any): The value to be written into the metadata or data/
Returns:
Nothing."""
self.metadata[name] = value
Notes:
If name is a string or already exists as key in metadata, this setitem will set metadata values, otherwise if name
is a tuple then if the first elem,ent in a string it checks to see if that is an existing metadata item that is iterable,
and if so, sets the metadta. In all other circumstances, it attempts to set an item in the main data array.
"""
if isinstance(name,string_types) or name in self.metadata:
self.metadata[name] = value
elif isinstance(name,tuple):
if isinstance(name[0],string_types) and name[0] in self.metadata and isinstance(self.metadata[name[0]],Iterable):
if len(name)==2:
key=name[0]
name=name[1]
else:
key=name[0]
name=tuple(name[1:])
self.metadata[key][name]=value
else:
self.data[name]=value
else:
self.data[name]=value

def __setstate__(self, state):
"""Internal function for pickling."""
Expand Down
14 changes: 14 additions & 0 deletions tests/Stoner/test_Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ def test_indexing(self):
self.d["X-Dat"]=[11,12,13,14,15]
self.assertEqual(self.d["X-Dat",2],13,"Failed indexing of metadata lists with tuple")
self.assertEqual(self.d["X-Dat"][2],13,"Failed indexing of metadata lists with double indices")
d=Data(np.ones((10,10)))
d[0,0]=5 #Index by tuple into data
d["Column_1",0]=6 # Index by column name, row into data
d[0,"Column_2"]=7 #Index by row, column name into data
d["Column_3"]=[1,2,3,4] # Create a metadata
d["Column_3",2]=2 # Index existing metadata via tuple
d.metadata[0,5]=10
d[0,5]=12 # Even if tuple, index metadata if already existing.
self.assertTrue(np.all(d[0]==np.array([5,6,7,1,1,1,1,1,1,1])),"setitem on Data to index into Data.data failed.")
self.assertEqual(d.metadata["Column_3"],[1,2,2,4],"Tuple indexing into metadata Failed.")
self.assertEqual(d.metadata[0,5],12,"Indexing of pre-existing metadta keys rather than Data./data failed.")




def test_len(self):
Expand Down Expand Up @@ -233,6 +246,7 @@ def test_setas_metadata(self):
if __name__=="__main__": # Run some tests manually to allow debugging
test=Datatest("test_operators")
test.setUp()
test.test_indexing()
test.test_constructor()
test.test_attributes()
test.test_operators()
Expand Down

0 comments on commit 458eca3

Please sign in to comment.