<s> from __future__ import absolute_import , print_function , division <EOL> import os <EOL> import shutil <EOL> import unittest <EOL> from tempfile import mkdtemp <EOL> import numpy <EOL> from numpy . testing import assert_allclose <EOL> from nose . plugins . skip import SkipTest <EOL> import theano <EOL> import theano . sandbox . cuda as cuda_ndarray <EOL> from theano . sandbox . cuda . type import CudaNdarrayType <EOL> from theano . sandbox . cuda . var import CudaNdarraySharedVariable <EOL> from theano . sandbox . rng_mrg import MRG_RandomStreams <EOL> from theano . misc . pkl_utils import dump , load , StripPickler <EOL> class T_dump_load ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . origdir = os . getcwd ( ) <EOL> self . tmpdir = mkdtemp ( ) <EOL> os . chdir ( self . tmpdir ) <EOL> def tearDown ( self ) : <EOL> os . chdir ( self . origdir ) <EOL> if self . tmpdir is not None : <EOL> shutil . rmtree ( self . tmpdir ) <EOL> def test_dump_load ( self ) : <EOL> if not cuda_ndarray . cuda_enabled : <EOL> raise SkipTest ( '<STR_LIT>' ) <EOL> x = CudaNdarraySharedVariable ( '<STR_LIT:x>' , CudaNdarrayType ( ( <NUM_LIT:1> , <NUM_LIT:1> ) , name = '<STR_LIT:x>' ) , <EOL> [ [ <NUM_LIT:1> ] ] , False ) <EOL> with open ( '<STR_LIT:test>' , '<STR_LIT:wb>' ) as f : <EOL> dump ( x , f ) <EOL> with open ( '<STR_LIT:test>' , '<STR_LIT:rb>' ) as f : <EOL> x = load ( f ) <EOL> assert x . name == '<STR_LIT:x>' <EOL> assert_allclose ( x . get_value ( ) , [ [ <NUM_LIT:1> ] ] ) <EOL> def test_dump_load_mrg ( self ) : <EOL> rng = MRG_RandomStreams ( use_cuda = cuda_ndarray . cuda_enabled ) <EOL> with open ( '<STR_LIT:test>' , '<STR_LIT:wb>' ) as f : <EOL> dump ( rng , f ) <EOL> with open ( '<STR_LIT:test>' , '<STR_LIT:rb>' ) as f : <EOL> rng = load ( f ) <EOL> assert type ( rng ) == MRG_RandomStreams <EOL> def test_dump_zip_names ( self ) : <EOL> foo_1 = theano . shared ( <NUM_LIT:0> , name = '<STR_LIT:foo>' ) <EOL> foo_2 = theano . shared ( <NUM_LIT:1> , name = '<STR_LIT:foo>' ) <EOL> foo_3 = theano . shared ( <NUM_LIT:2> , name = '<STR_LIT:foo>' ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:wb>' ) as f : <EOL> dump ( ( foo_1 , foo_2 , foo_3 , numpy . array ( <NUM_LIT:3> ) ) , f ) <EOL> keys = list ( numpy . load ( '<STR_LIT>' ) . keys ( ) ) <EOL> assert keys == [ '<STR_LIT:foo>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> foo_3 = numpy . load ( '<STR_LIT>' ) [ '<STR_LIT>' ] <EOL> assert foo_3 == numpy . array ( <NUM_LIT:2> ) <EOL> with open ( '<STR_LIT>' , '<STR_LIT:rb>' ) as f : <EOL> foo_1 , foo_2 , foo_3 , array = load ( f ) <EOL> assert array == numpy . array ( <NUM_LIT:3> ) <EOL> class TestStripPickler ( unittest . TestCase ) : <EOL> def setUp ( self ) : <EOL> self . origdir = os . getcwd ( ) <EOL> self . tmpdir = mkdtemp ( ) <EOL> os . chdir ( self . tmpdir ) <EOL> def tearDown ( self ) : <EOL> os . chdir ( self . origdir ) <EOL> if self . tmpdir is not None : <EOL> shutil . rmtree ( self . tmpdir ) <EOL> def test0 ( self ) : <EOL> with open ( '<STR_LIT>' , '<STR_LIT:wb>' ) as f : <EOL> m = theano . tensor . matrix ( ) <EOL> dest_pkl = '<STR_LIT>' <EOL> f = open ( dest_pkl , '<STR_LIT:wb>' ) <EOL> strip_pickler = StripPickler ( f , protocol = - <NUM_LIT:1> ) <EOL> strip_pickler . dump ( m ) </s>
