def testSaveNums(self): pObj = p.pref(file=self.testFile) pObj.save(name="num1", data=1) pObj.save(name="num1", data=1) pObj.save(name="num2", data=2.0) pObj.save(name="num2", data=2.0) self.assertTrue( type(pObj.load(name="num1", returnDict=False)) == np.ndarray) self.assertTrue( pObj.load(name="num1", returnDict=False).dtype == "int64") self.assertTrue(pObj.load(name="num1", returnDict=False) == 1) self.assertTrue( type(pObj.load(name="num2", returnDict=False)) == np.ndarray) self.assertTrue( pObj.load(name="num2", returnDict=False).dtype == "float64") self.assertTrue(pObj.load(name="num2", returnDict=False) == 2) pObj.save(group="yummy", name="num2", data=2.0) self.assertTrue( pObj.load(group="", name="yummy/num2", returnDict=False) == 2) self.assertTrue( type(pObj.load(group="", name="yummy/num2", returnDict=False)) == np.ndarray) self.assertTrue( pObj.load(group="", name="yummy/num2", returnDict=False).dtype == "float64") self.assertTrue( type(pObj.load(group="yummy", name="num2", returnDict=False)) == np.ndarray) self.assertTrue( pObj.load(group="yummy", name="num2", returnDict=False).dtype == "float64")
def test_default(self): pObj = p.pref(file=self.testFile) data = pObj.load(name="blah", default=True, returnDict=False) self.assertTrue(data == True) # this means it is has been saved data = pObj.load(name="blah", returnDict=False) self.assertTrue(data == True) x = np.array([(1, 2), (3, 4)]) dataList = [True, "yay", x] data= pObj.load( name= [ "blah", "blue", "blend" ],\ default= dataList, returnDict= False ) boolList = [ np.all(dataList[i] == data[i]) if type(dataList[i]) == type( np.array([])) else dataList[i] == data[i] for i in np.arange(len(dataList)) ] self.assertTrue(np.all(boolList)) # This will ensure they were saved data = pObj.load(name=["blah", "blue", "blend"], returnDict=False) boolList = [ np.all(dataList[i] == data[i]) if type(dataList[i]) == type( np.array([])) else dataList[i] == data[i] for i in np.arange(len(dataList)) ] self.assertTrue(np.all(boolList))
def test_delete(self): pObj = p.pref(file=self.testFile) dataList = [True, False, 1] nameList = ["blah", "blue", "blend"] _= pObj.load( group= "", name= nameList, \ default= dataList ) _= pObj.load( name= nameList, \ default= dataList ) pObj.delete(group="", name=nameList) with h5py.File(pObj.file, 'r', compression=pObj.compression) as f: for aName in nameList: self.assertTrue(aName not in f) self.assertTrue(pObj.DEF_GROUPSTR in f) fullNameList = [ p._joinGroupAndName(pObj.DEF_GROUPSTR, aName) for aName in nameList ] for aName in fullNameList: self.assertTrue(aName in f) pObj.delete(name=nameList) with h5py.File(pObj.file, 'r', compression=pObj.compression) as f: for aName in fullNameList: self.assertTrue(aName not in f)
def testBigStringList(self): pObj = p.pref(file=self.testFile) saveStr = [_randomString(16) for i in np.arange(500)] pObj.save(name="blah", data=saveStr) pObj.save(name="blah", data=saveStr) readStr = pObj.load(name="blah", returnDict=False) self.assertTrue(saveStr == readStr)
def testMatrix(self): pObj = p.pref(file=self.testFile) x = np.array([(1, 2), (3, 4)]) pObj.save(name="blah", data=x) xRead = pObj.load(name="blah", returnDict=False) self.assertTrue(np.all(xRead == x)) y= np.array( [ (1, 2, 3), \ (4, 5, 6), \ (5, 6, 7) \ ] ) pObj.save(name="blah", data=y) yRead = pObj.load(name="blah", returnDict=False) self.assertTrue(np.all(y == yRead))
def test_saveAndloadListOfMatrices(self): pObj = p.pref(file=self.testFile) x = np.array([(1, 2), (3, 4)]) y= np.array( [ (1, 2, 3), \ (4, 5, 6), \ (5, 6, 7) \ ] ) pObj.save(name=["blue", "blah"], data=[x, y]) xRead = pObj.load(name="blue", returnDict=False) yRead = pObj.load(name="blah", returnDict=False) self.assertTrue(np.all(x == xRead) and np.all(y == yRead)) listRead = pObj.load(name=["blue", "blah"], returnDict=False) self.assertTrue( np.all(x==listRead[0]) and \ np.all(y==listRead[1]) )
def test_returnDictionary(self): pObj = p.pref(file=self.testFile) dataList = [True, False, 1] nameList = ["blah", "blue", "blend"] _= pObj.load( group= "", name= nameList, \ default= dataList ) dataDict= pObj.load( group= "", name= nameList, \ default= dataList ) for aKey, anElement in zip(nameList, dataList): self.assertTrue(dataDict[aKey] == anElement) pObj.save( group= "", name= "blah", \ data= ["asdf","basdf"] ) dataDict = pObj.load(group="", name="blah") self.assertTrue(dataDict["blah"] == ["asdf", "basdf"])
def testSaveListOfString(self): pObj = p.pref(file=self.testFile) pObj.save(name="blah", data="asdfasdfasdf") data = pObj.load(name="blah", returnDict=False) self.assertTrue(data == "asdfasdfasdf") pObj.save(name="blah", data=[ "aaasdfasdfasdfasdf", "b", "c", "d", "easdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf" ]) data = pObj.load(name="blah", returnDict=False) self.assertTrue(data == [ "aaasdfasdfasdfasdf", "b", "c", "d", "easdfasdfasdfasdfasdfasdfasdfasdfasdfasdfasdf" ]) pObj.save(name="blah", data=["a", "b", "c", "d", "e"]) data = pObj.load(name="blah", returnDict=False) self.assertTrue(data == ["a", "b", "c", "d", "e"])
def __init__( self, persistentDir= None, persistentFile= None, \ prefGroup= None, guiDataObj= None, \ initDefaultDict= None, groupSubCat= None ): """ -Must define the directory, file and group. -if the guiDataObj is defined, then the copy constructor uses only these fields and NOT any of the data. - if initDefault dict is defined, it laods the values from disk/persistent memory """ if guiDataObj is not None: assert groupSubCat is not None, "When using copy constructor must define groupSubCat" # copy constructor, use the same file as the other guy persistentDir = guiDataObj.persistentDir persistentFile = guiDataObj.persistentFile prefGroup = guiDataObj.prefGroup + "/" + groupSubCat else: assert persistentFile is not None, "must define persistent file" assert prefGroup is not None, "must define prefGroup" assert prefGroup != "", "must define prefGroup as non empty string" if groupSubCat is not None: prefGroup = prefGroup + "/" + groupSubCat self.prefGroup = prefGroup self.persistentDir = "" self.persistentFile = persistentFile if persistentDir is not None: self.persistentDir = persistentDir prefFile = os.path.join(persistentDir, persistentFile) else: prefFile = os.environ['HOME'] self._prefObj = pref(file=prefFile) self._loadedDefaults = list() self._defDict = dict() if initDefaultDict is not None: self.addAndStoreDefaults(initDefaultDict)
def testSaveNaN(self): pObj = p.pref(file=self.testFile) pObj.save(name="blah", data=np.NAN) data = pObj.load(name="blah", returnDict=False) self.assertTrue(np.isnan(data))
def testSaveString(self): pObj = p.pref(file=self.testFile) pObj.save(name="str1", data="blahblahblah") myStr = pObj.load(name="str1", returnDict=False) self.assertTrue(myStr == "blahblahblah")
def test_saveBool(self): pObj = p.pref(file=self.testFile) pObj.save(name="blah", data=True) readBool = pObj.load(name="blah", returnDict=False) self.assertTrue(readBool == True)