예제 #1
0
    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")
예제 #2
0
    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))
예제 #3
0
    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)
예제 #4
0
 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)
예제 #5
0
    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))
예제 #6
0
    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]) )
예제 #7
0
    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"])
예제 #8
0
 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"])
예제 #9
0
    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)
예제 #10
0
 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))
예제 #11
0
 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")
예제 #12
0
 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)