<s> """<STR_LIT>""" <EOL> from __future__ import print_function <EOL> import os <EOL> import numpy as np <EOL> from theano . compat import six <EOL> from theano . compat . six . moves import cPickle <EOL> import tempfile <EOL> from numpy . testing import assert_ <EOL> from os import environ , close <EOL> from decimal import Decimal <EOL> from tempfile import mkstemp <EOL> from pylearn2 . compat import first_key , first_value <EOL> from pylearn2 . config . yaml_parse import load , load_path , initialize <EOL> from pylearn2 . utils import serial <EOL> from pylearn2 . utils . exc import reraise_as <EOL> from pylearn2 . config . yaml_parse import SCIENTIFIC_NOTATION_REGEXP <EOL> import yaml <EOL> import re <EOL> def test_load_path ( ) : <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> with os . fdopen ( fd , '<STR_LIT:wb>' ) as f : <EOL> f . write ( six . b ( "<STR_LIT>" ) ) <EOL> loaded = load_path ( fname ) <EOL> assert_ ( loaded [ '<STR_LIT:a>' ] == <NUM_LIT> ) <EOL> os . remove ( fname ) <EOL> def test_obj ( ) : <EOL> loaded = load ( "<STR_LIT>" ) <EOL> assert_ ( isinstance ( loaded [ '<STR_LIT:a>' ] , Decimal ) ) <EOL> def test_floats ( ) : <EOL> loaded = load ( "<STR_LIT>" ) <EOL> assert_ ( isinstance ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:a>' ] , float ) ) <EOL> assert_ ( isinstance ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:b>' ] , float ) ) <EOL> assert_ ( ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:a>' ] + <NUM_LIT> ) < <NUM_LIT> ) <EOL> assert_ ( ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:b>' ] - <NUM_LIT> ) < <NUM_LIT> ) <EOL> def test_notation_regexp ( ) : <EOL> """<STR_LIT>""" <EOL> pattern = re . compile ( SCIENTIFIC_NOTATION_REGEXP ) <EOL> matches = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> fails = [ '<STR_LIT:string>' , '<STR_LIT:4>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> for match in matches : <EOL> assert pattern . match ( match ) <EOL> for fail in fails : <EOL> assert not pattern . match ( fail ) <EOL> def test_scientific_notation ( ) : <EOL> """<STR_LIT>""" <EOL> loaded = load ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> assert isinstance ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:a>' ] , float ) <EOL> assert isinstance ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:b>' ] , float ) <EOL> assert isinstance ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:c>' ] , float ) <EOL> assert isinstance ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:d>' ] , float ) <EOL> assert isinstance ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:e>' ] , float ) <EOL> assert isinstance ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:f>' ] , float ) <EOL> assert isinstance ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:g>' ] , float ) <EOL> assert isinstance ( loaded [ '<STR_LIT:a>' ] [ '<STR_LIT:h>' ] , float ) <EOL> def test_import ( ) : <EOL> loaded = load ( "<STR_LIT>" ) <EOL> assert_ ( loaded [ '<STR_LIT:a>' ] == Decimal ) <EOL> def test_import_string ( ) : <EOL> loaded = load ( "<STR_LIT>" ) <EOL> assert_ ( loaded [ '<STR_LIT:a>' ] == Decimal ) <EOL> def test_import_colon ( ) : <EOL> loaded = load ( "<STR_LIT>" ) <EOL> assert_ ( loaded [ '<STR_LIT:a>' ] == Decimal ) <EOL> def test_preproc_rhs ( ) : <EOL> environ [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> loaded = load ( '<STR_LIT>' ) <EOL> print ( "<STR_LIT>" % loaded [ '<STR_LIT:a>' ] ) <EOL> assert_ ( loaded [ '<STR_LIT:a>' ] == "<STR_LIT>" ) <EOL> del environ [ '<STR_LIT>' ] <EOL> def test_preproc_pkl ( ) : <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> with os . fdopen ( fd , '<STR_LIT:wb>' ) as f : <EOL> d = ( '<STR_LIT:a>' , <NUM_LIT:1> ) <EOL> cPickle . dump ( d , f ) <EOL> environ [ '<STR_LIT>' ] = fname <EOL> loaded = load ( '<STR_LIT>' ) <EOL> assert_ ( loaded [ '<STR_LIT:a>' ] == d ) <EOL> del environ [ '<STR_LIT>' ] <EOL> def test_late_preproc_pkl ( ) : <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> with os . fdopen ( fd , '<STR_LIT:wb>' ) as f : <EOL> array = np . arange ( <NUM_LIT:10> ) <EOL> np . save ( f , array ) <EOL> environ [ '<STR_LIT>' ] = fname <EOL> loaded = load ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> assert_ ( loaded [ '<STR_LIT:a>' ] . yaml_src . find ( "<STR_LIT>" ) != - <NUM_LIT:1> ) <EOL> del environ [ '<STR_LIT>' ] <EOL> def test_unpickle ( ) : <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> with os . fdopen ( fd , '<STR_LIT:wb>' ) as f : <EOL> d = { '<STR_LIT:a>' : <NUM_LIT:1> , '<STR_LIT:b>' : <NUM_LIT:2> } <EOL> cPickle . dump ( d , f ) <EOL> loaded = load ( "<STR_LIT>" % fname ) <EOL> assert_ ( loaded [ '<STR_LIT:a>' ] == d ) <EOL> os . remove ( fname ) <EOL> def test_unpickle_key ( ) : <EOL> fd , fname = tempfile . mkstemp ( ) <EOL> with os . fdopen ( fd , '<STR_LIT:wb>' ) as f : <EOL> d = ( '<STR_LIT:a>' , <NUM_LIT:1> ) <EOL> cPickle . dump ( d , f ) <EOL> loaded = load ( "<STR_LIT>" % fname ) <EOL> assert_ ( first_key ( loaded ) == d ) <EOL> assert_ ( first_value ( loaded ) == <NUM_LIT:50> ) <EOL> os . remove ( fname ) <EOL> def test_multi_constructor_obj ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> load ( "<STR_LIT>" ) <EOL> except TypeError as e : <EOL> assert str ( e ) == "<STR_LIT>" <EOL> pass <EOL> except Exception as e : <EOL> error_msg = "<STR_LIT>" % ( e ) <EOL> reraise_as ( ValueError ( error_msg ) ) <EOL> def test_duplicate_keywords ( ) : <EOL> """<STR_LIT>""" <EOL> initialize ( ) <EOL> yamlfile = """<STR_LIT>""" <EOL> try : <EOL> load ( yamlfile ) <EOL> except yaml . constructor . ConstructorError as e : <EOL> message = str ( e ) <EOL> assert message . endswith ( "<STR_LIT>" ) <EOL> pass <EOL> except Exception as e : <EOL> error_msg = "<STR_LIT>" % ( e ) <EOL> raise TypeError ( error_msg ) <EOL> def test_duplicate_keywords_2 ( ) : <EOL> """<STR_LIT>""" <EOL> initialize ( ) <EOL> yamlfile = """<STR_LIT>""" <EOL> load ( yamlfile ) <EOL> def test_parse_null_as_none ( ) : <EOL> """<STR_LIT>""" <EOL> initialize ( ) <EOL> yamlfile = """<STR_LIT>""" <EOL> load ( yamlfile ) <EOL> class DumDum ( object ) : <EOL> pass <EOL> def test_pkl_yaml_src_field ( ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> fd , fn = mkstemp ( ) <EOL> close ( fd ) <EOL> o = DumDum ( ) <EOL> o . x = ( '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ) <EOL> serial . save ( fn , o ) <EOL> yaml = '<STR_LIT>' + fn + '<STR_LIT>' <EOL> loaded = load ( yaml ) <EOL> assert loaded . x == ( '<STR_LIT:a>' , '<STR_LIT:b>' , '<STR_LIT:c>' ) <EOL> assert loaded . yaml_src == yaml <EOL> finally : <EOL> os . remove ( fn ) <EOL> def test_instantiate_regression ( ) : <EOL> """<STR_LIT>""" <EOL> yaml = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> obj = load ( yaml ) <EOL> assert obj [ '<STR_LIT:a>' ] is obj [ '<STR_LIT:b>' ] <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_multi_constructor_obj ( ) <EOL> test_duplicate_keywords ( ) <EOL> test_duplicate_keywords_2 ( ) <EOL> test_unpickle_key ( ) </s>
