Exemple #1
0
def test_pickle():
    import tempfile
    from numpy.testing import assert_equal
    tmpdir = tempfile.mkdtemp(prefix='pickle')
    a = range(10)
    save_pickle(a, tmpdir + '/res.pkl')
    b = load_pickle(tmpdir + '/res.pkl')
    assert_equal(a, b)

    #cleanup, tested on Windows
    try:
        import os
        os.remove(tmpdir + '/res.pkl')
        os.rmdir(tmpdir)
    except (OSError, IOError):
        pass
    assert not os.path.exists(tmpdir)

    #test with file handle
    from gwstatsmodels.compatnp.py3k import BytesIO
    fh = BytesIO()
    save_pickle(a, fh)
    fh.seek(0, 0)
    c = load_pickle(fh)
    fh.close()
    assert_equal(a, b)
Exemple #2
0
def test_pickle():
    import tempfile
    from numpy.testing import assert_equal
    tmpdir = tempfile.mkdtemp(prefix='pickle')
    a = range(10)
    save_pickle(a, tmpdir+'/res.pkl')
    b = load_pickle(tmpdir+'/res.pkl')
    assert_equal(a, b)

    #cleanup, tested on Windows
    try:
        import os
        os.remove(tmpdir+'/res.pkl')
        os.rmdir(tmpdir)
    except (OSError, IOError):
        pass
    assert not os.path.exists(tmpdir)

    #test with file handle
    from gwstatsmodels.compatnp.py3k import BytesIO
    fh = BytesIO()
    save_pickle(a, fh)
    fh.seek(0,0)
    c = load_pickle(fh)
    fh.close()
    assert_equal(a,b)
Exemple #3
0
def check_pickle(obj):
    from gwstatsmodels.compatnp.py3k import BytesIO
    fh = BytesIO()
    pickle.dump(obj, fh)
    plen = fh.tell()
    fh.seek(0, 0)
    res = pickle.load(fh)
    fh.close()
    return res, plen
def check_pickle(obj):
    from gwstatsmodels.compatnp.py3k import BytesIO
    fh = BytesIO()
    pickle.dump(obj, fh)
    plen = fh.tell()
    fh.seek(0,0)
    res = pickle.load(fh)
    fh.close()
    return res, plen
Exemple #5
0
    def test_pickle_wrapper(self):

        from gwstatsmodels.iolib.smpickle import save_pickle, load_pickle

        from gwstatsmodels.compatnp.py3k import BytesIO

        fh = BytesIO()  #use cPickle with binary content

        #test unwrapped results load save pickle
        self.results._results.save(fh)
        fh.seek(0, 0)
        res_unpickled = self.results._results.__class__.load(fh)
        assert_(type(res_unpickled) is type(self.results._results))

        #test wrapped results load save
        fh.seek(0, 0)
        #save_pickle(self.results, fh)
        self.results.save(fh)
        fh.seek(0, 0)
        #res_unpickled = load_pickle(fh)
        res_unpickled = self.results.__class__.load(fh)
        fh.close()
        #print type(res_unpickled)
        assert_(type(res_unpickled) is type(self.results))

        before = sorted(self.results.__dict__.keys())
        after = sorted(res_unpickled.__dict__.keys())
        assert_(before == after, msg='not equal %r and %r' % (before, after))

        before = sorted(self.results._results.__dict__.keys())
        after = sorted(res_unpickled._results.__dict__.keys())
        assert_(before == after, msg='not equal %r and %r' % (before, after))

        before = sorted(self.results.model.__dict__.keys())
        after = sorted(res_unpickled.model.__dict__.keys())
        assert_(before == after, msg='not equal %r and %r' % (before, after))

        before = sorted(self.results._cache.keys())
        after = sorted(res_unpickled._cache.keys())
        assert_(before == after, msg='not equal %r and %r' % (before, after))
    def test_pickle_wrapper(self):

        from gwstatsmodels.iolib.smpickle import save_pickle, load_pickle

        from gwstatsmodels.compatnp.py3k import BytesIO

        fh = BytesIO()  #use cPickle with binary content

        #test unwrapped results load save pickle
        self.results._results.save(fh)
        fh.seek(0,0)
        res_unpickled = self.results._results.__class__.load(fh)
        assert_(type(res_unpickled) is type(self.results._results))

        #test wrapped results load save
        fh.seek(0,0)
        #save_pickle(self.results, fh)
        self.results.save(fh)
        fh.seek(0,0)
        #res_unpickled = load_pickle(fh)
        res_unpickled = self.results.__class__.load(fh)
        fh.close()
        #print type(res_unpickled)
        assert_(type(res_unpickled) is type(self.results))

        before = sorted(self.results.__dict__.keys())
        after = sorted(res_unpickled.__dict__.keys())
        assert_(before == after, msg='not equal %r and %r' % (before, after))

        before = sorted(self.results._results.__dict__.keys())
        after = sorted(res_unpickled._results.__dict__.keys())
        assert_(before == after, msg='not equal %r and %r' % (before, after))

        before = sorted(self.results.model.__dict__.keys())
        after = sorted(res_unpickled.model.__dict__.keys())
        assert_(before == after, msg='not equal %r and %r' % (before, after))

        before = sorted(self.results._cache.keys())
        after = sorted(res_unpickled._cache.keys())
        assert_(before == after, msg='not equal %r and %r' % (before, after))