<s> '''<STR_LIT>''' <EOL> from nose . tools import * <EOL> import unittest <EOL> import collections <EOL> import time <EOL> import numpy <EOL> import random <EOL> from msgpack import packb , unpackb <EOL> import hashlib <EOL> from pyon . core . interceptor . encode import encode_ion , decode_ion <EOL> def sha1 ( buf ) : <EOL> return hashlib . sha1 ( buf ) . hexdigest ( ) . upper ( ) <EOL> count = <NUM_LIT:0> <EOL> class PackRunBase ( object ) : <EOL> _decoder = None <EOL> _encoder = None <EOL> types = collections . OrderedDict ( <EOL> [ <EOL> ( '<STR_LIT>' , ( '<STR_LIT:bool>' , random . randint , ( <NUM_LIT:0> , <NUM_LIT:1> ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , lambda o : chr ( count ) , ( None , ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , lambda o : chr ( count ) * <NUM_LIT:16> , ( None , ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , random . randint , ( - ( <NUM_LIT:1> << <NUM_LIT:7> ) , ( <NUM_LIT:1> << <NUM_LIT:7> ) - <NUM_LIT:1> ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , random . randint , ( - ( <NUM_LIT:1> << <NUM_LIT:15> ) , ( <NUM_LIT:1> << <NUM_LIT:15> ) - <NUM_LIT:1> ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , random . randint , ( - ( <NUM_LIT:1> << <NUM_LIT> ) , ( <NUM_LIT:1> << <NUM_LIT> ) - <NUM_LIT:1> ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , random . randint , ( - ( <NUM_LIT:1> << <NUM_LIT> ) , ( <NUM_LIT:1> << <NUM_LIT> ) - <NUM_LIT:1> ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , random . randint , ( <NUM_LIT:0> , ( <NUM_LIT:1> << <NUM_LIT:8> ) - <NUM_LIT:1> ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , random . randint , ( <NUM_LIT:0> , ( <NUM_LIT:1> << <NUM_LIT:16> ) - <NUM_LIT:1> ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , random . randint , ( <NUM_LIT:0> , ( <NUM_LIT:1> << <NUM_LIT:32> ) - <NUM_LIT:1> ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , random . randint , ( <NUM_LIT:0> , ( <NUM_LIT:1> << <NUM_LIT:64> ) - <NUM_LIT:1> ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , lambda o : numpy . float16 ( "<STR_LIT:1.0>" ) + o , ( numpy . finfo ( '<STR_LIT>' ) . eps , ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , lambda o : <NUM_LIT:1> - o , ( numpy . finfo ( '<STR_LIT>' ) . epsneg , ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , numpy . random . uniform , ( numpy . finfo ( '<STR_LIT>' ) . min , numpy . finfo ( '<STR_LIT>' ) . max ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , lambda o : <NUM_LIT:1> + o , ( numpy . finfo ( '<STR_LIT>' ) . eps , ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , lambda o : <NUM_LIT:1> - o , ( numpy . finfo ( '<STR_LIT>' ) . epsneg , ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , numpy . random . uniform , ( numpy . finfo ( '<STR_LIT>' ) . min , numpy . finfo ( '<STR_LIT>' ) . max ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , lambda o : <NUM_LIT:1> + o , ( numpy . finfo ( '<STR_LIT>' ) . eps , ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , lambda o : <NUM_LIT:1> - o , ( numpy . finfo ( '<STR_LIT>' ) . epsneg , ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , numpy . random . uniform , ( numpy . finfo ( '<STR_LIT>' ) . min , numpy . finfo ( '<STR_LIT>' ) . max ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , lambda a , b : numpy . complex ( numpy . random . uniform ( a , b ) , numpy . random . uniform ( a , b ) ) , ( numpy . finfo ( '<STR_LIT>' ) . min , numpy . finfo ( '<STR_LIT>' ) . max ) ) ) , <EOL> ( '<STR_LIT>' , ( '<STR_LIT>' , lambda a , b : numpy . complex ( numpy . random . uniform ( a , b ) , numpy . random . uniform ( a , b ) ) , ( numpy . finfo ( '<STR_LIT>' ) . min , numpy . finfo ( '<STR_LIT>' ) . max ) ) ) , <EOL> ( '<STR_LIT:object>' , ( '<STR_LIT:object>' , lambda o : { count : chr ( count ) * <NUM_LIT:8> } , ( None , ) ) ) <EOL> ] <EOL> ) <EOL> shapes = ( ( <NUM_LIT:1> , ) , ( <NUM_LIT:3> , <NUM_LIT:4> ) , ( <NUM_LIT:9> , <NUM_LIT:12> , <NUM_LIT> ) , ( <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> , <NUM_LIT:10> ) , ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . _decoder = decode_ion <EOL> self . _encoder = encode_ion <EOL> def test_all ( self ) : <EOL> for shape in self . shapes : <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> print "<STR_LIT>" <EOL> for type_name , ( type , func , args ) in self . types . iteritems ( ) : <EOL> print "<STR_LIT>" % ( type_name , str ( shape ) ) <EOL> self . run_it ( self . _encoder , self . _decoder , type , func , args , shape ) <EOL> def run_it ( self , encoder , decoder , type , func , args , shape ) : <EOL> array = numpy . zeros ( shape , type ) <EOL> count = <NUM_LIT:0> <EOL> for x in numpy . nditer ( array , flags = [ '<STR_LIT>' ] , op_flags = [ '<STR_LIT>' ] ) : <EOL> count += <NUM_LIT:1> <EOL> x [ ... ] = func ( * args ) <EOL> tic = time . time ( ) <EOL> msg = packb ( array , default = encoder ) <EOL> new_array = unpackb ( msg , object_hook = decoder ) <EOL> toc = time . time ( ) - tic <EOL> print '<STR_LIT>' % ( len ( msg ) , toc ) <EOL> assert_true ( ( array == new_array ) . all ( ) ) <EOL> if type is not '<STR_LIT:object>' : <EOL> assert_equals ( sha1 ( array . tostring ( ) ) , sha1 ( new_array . tostring ( ) ) ) <EOL> class NumpyMsgPackTestCase ( unittest . TestCase , PackRunBase ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> unittest . TestCase . __init__ ( self , * args , ** kwargs ) <EOL> PackRunBase . __init__ ( self , * args , ** kwargs ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> pb = PackRunBase ( ) <EOL> pb . test_all ( ) </s>
