summaryrefslogtreecommitdiff
path: root/numpy/random/_pickle.py
diff options
context:
space:
mode:
authorKevin Sheppard <kevin.k.sheppard@gmail.com>2022-07-19 15:22:01 +0100
committerKevin Sheppard <kevin.k.sheppard@gmail.com>2022-07-19 15:23:53 +0100
commit95e3e7f445407e4f355b23d6a9991d8774f0eb0c (patch)
treeb6926d5ba6c2afcaba95876f17ce747f8f6eb31a /numpy/random/_pickle.py
parent5ba36b700284a8392af5be542f65ecd262fd2568 (diff)
downloadnumpy-95e3e7f445407e4f355b23d6a9991d8774f0eb0c.tar.gz
BUG/ENH: ALlow bit generators to supply their own ctor
Allow bit generators to supply their own constructors to enable Generator objects using arbitrary bit generators to be supported closes #22012
Diffstat (limited to 'numpy/random/_pickle.py')
-rw-r--r--numpy/random/_pickle.py49
1 files changed, 23 insertions, 26 deletions
diff --git a/numpy/random/_pickle.py b/numpy/random/_pickle.py
index 5e89071e8..073993726 100644
--- a/numpy/random/_pickle.py
+++ b/numpy/random/_pickle.py
@@ -14,19 +14,19 @@ BitGenerators = {'MT19937': MT19937,
}
-def __generator_ctor(bit_generator_name='MT19937'):
+def __bit_generator_ctor(bit_generator_name='MT19937'):
"""
- Pickling helper function that returns a Generator object
+ Pickling helper function that returns a bit generator object
Parameters
----------
bit_generator_name : str
- String containing the core BitGenerator
+ String containing the name of the BitGenerator
Returns
-------
- rg : Generator
- Generator using the named core BitGenerator
+ bit_generator : BitGenerator
+ BitGenerator instance
"""
if bit_generator_name in BitGenerators:
bit_generator = BitGenerators[bit_generator_name]
@@ -34,50 +34,47 @@ def __generator_ctor(bit_generator_name='MT19937'):
raise ValueError(str(bit_generator_name) + ' is not a known '
'BitGenerator module.')
- return Generator(bit_generator())
+ return bit_generator()
-def __bit_generator_ctor(bit_generator_name='MT19937'):
+def __generator_ctor(bit_generator_name="MT19937",
+ bit_generator_ctor=__bit_generator_ctor):
"""
- Pickling helper function that returns a bit generator object
+ Pickling helper function that returns a Generator object
Parameters
----------
bit_generator_name : str
- String containing the name of the BitGenerator
+ String containing the core BitGenerator's name
+ bit_generator_ctor : callable, optional
+ Callable function that takes bit_generator_name as its only argument
+ and returns an instantized bit generator.
Returns
-------
- bit_generator : BitGenerator
- BitGenerator instance
+ rg : Generator
+ Generator using the named core BitGenerator
"""
- if bit_generator_name in BitGenerators:
- bit_generator = BitGenerators[bit_generator_name]
- else:
- raise ValueError(str(bit_generator_name) + ' is not a known '
- 'BitGenerator module.')
-
- return bit_generator()
+ return Generator(bit_generator_ctor(bit_generator_name))
-def __randomstate_ctor(bit_generator_name='MT19937'):
+def __randomstate_ctor(bit_generator_name="MT19937",
+ bit_generator_ctor=__bit_generator_ctor):
"""
Pickling helper function that returns a legacy RandomState-like object
Parameters
----------
bit_generator_name : str
- String containing the core BitGenerator
+ String containing the core BitGenerator's name
+ bit_generator_ctor : callable, optional
+ Callable function that takes bit_generator_name as its only argument
+ and returns an instantized bit generator.
Returns
-------
rs : RandomState
Legacy RandomState using the named core BitGenerator
"""
- if bit_generator_name in BitGenerators:
- bit_generator = BitGenerators[bit_generator_name]
- else:
- raise ValueError(str(bit_generator_name) + ' is not a known '
- 'BitGenerator module.')
- return RandomState(bit_generator())
+ return RandomState(bit_generator_ctor(bit_generator_name))