3838except ImportError :
3939 import simplejson as json
4040
41- VERSION = '1.9.0'
41+ from quantumrandom .qrandom import QuantumRandom
42+
43+ VERSION = '1.10.0'
4244URL = 'https://qrng.anu.edu.au/API/jsonI.php'
4345DATA_TYPES = ['uint16' , 'hex16' ]
4446MAX_LEN = 1024
4547INT_BITS = 16
4648
4749
48- def get_data (data_type = 'uint16' , array_length = 1 , block_size = 1 ):
50+ def get_data (data_type = 'uint16' , array_length = 1 , block_size = 1 , timeout = None ):
4951 """Fetch data from the ANU Quantum Random Numbers JSON API"""
5052 if data_type not in DATA_TYPES :
5153 raise Exception ("data_type must be one of %s" % DATA_TYPES )
@@ -58,15 +60,15 @@ def get_data(data_type='uint16', array_length=1, block_size=1):
5860 'length' : array_length ,
5961 'size' : block_size ,
6062 })
61- data = get_json (url )
63+ data = get_json (url , timeout = timeout )
6264 assert data ['success' ] is True , data
6365 assert data ['length' ] == array_length , data
6466 return data ['data' ]
6567
6668
6769if sys .version_info [0 ] == 2 :
68- def get_json (url ):
69- return json .loads (urlopen (url ).read (), object_hook = _object_hook )
70+ def get_json (url , timeout = None ):
71+ return json .loads (urlopen (url , timeout = timeout ).read (), object_hook = _object_hook )
7072
7173 def _object_hook (obj ):
7274 """We are only dealing with ASCII characters"""
@@ -84,21 +86,21 @@ def next(it, default=_sentinel):
8486 raise
8587 return default
8688else :
87- def get_json (url ):
88- return json .loads (urlopen (url ).read ().decode ('ascii' ))
89+ def get_json (url , timeout = None ):
90+ return json .loads (urlopen (url , timeout = timeout ).read ().decode ('ascii' ))
8991
9092
91- def binary (array_length = 100 , block_size = 100 ):
93+ def binary (array_length = 100 , block_size = 100 , ** kwargs ):
9294 """Return a chunk of binary data"""
93- return binascii .unhexlify (six .b (hex (array_length , block_size )))
95+ return binascii .unhexlify (six .b (hex (array_length , block_size , ** kwargs )))
9496
9597
96- def hex (array_length = 100 , block_size = 100 ):
98+ def hex (array_length = 100 , block_size = 100 , ** kwargs ):
9799 """Return a chunk of hex"""
98- return '' .join (get_data ('hex16' , array_length , block_size ))
100+ return '' .join (get_data ('hex16' , array_length , block_size , ** kwargs ))
99101
100102
101- def randint (min = 0 , max = 10 , generator = None ):
103+ def randint (min = 0 , max = 10 , generator = None , ** kwargs ):
102104 """Return an int between min and max. If given, takes from generator instead.
103105 This can be useful to reuse the same cached_generator() instance over multiple calls."""
104106 rand_range = max - min
@@ -107,7 +109,7 @@ def randint(min=0, max=10, generator=None):
107109 return min
108110
109111 if generator is None :
110- generator = cached_generator ()
112+ generator = cached_generator (** kwargs )
111113
112114 source_bits = int (math .ceil (math .log (rand_range + 1 , 2 )))
113115 source_size = int (math .ceil (source_bits / float (INT_BITS )))
@@ -126,17 +128,17 @@ def randint(min=0, max=10, generator=None):
126128 return num / modulos + min
127129
128130
129- def uint16 (array_length = 100 ):
131+ def uint16 (array_length = 100 , ** kwargs ):
130132 """Return a numpy array of uint16 numbers"""
131133 import numpy
132- return numpy .array (get_data ('uint16' , array_length ), dtype = numpy .uint16 )
134+ return numpy .array (get_data ('uint16' , array_length , ** kwargs ), dtype = numpy .uint16 )
133135
134136
135- def cached_generator (data_type = 'uint16' , cache_size = 1024 ):
137+ def cached_generator (data_type = 'uint16' , cache_size = 1024 , ** kwargs ):
136138 """Returns numbers. Caches numbers to avoid latency."""
137139 while 1 :
138- for n in get_data (data_type , cache_size , cache_size ):
140+ for n in get_data (data_type , cache_size , cache_size , ** kwargs ):
139141 yield n
140142
141143
142- __all__ = ['get_data' , 'binary' , 'hex' , 'uint16' , 'cached_generator' , 'randint' ]
144+ __all__ = ['get_data' , 'binary' , 'hex' , 'uint16' , 'cached_generator' , 'randint' , 'QuantumRandom' ]
0 commit comments