-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7d717d
commit 3e51114
Showing
3 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
r"""Implement a random seed setter for the python standard library | ||
``random``.""" | ||
|
||
from __future__ import annotations | ||
|
||
__all__ = ["RandomRandomSeedSetter"] | ||
|
||
import random | ||
|
||
from coola.random.base import BaseRandomSeedSetter | ||
|
||
|
||
class RandomRandomSeedSetter(BaseRandomSeedSetter): | ||
r"""Implement a random seed setter for the python standard library | ||
``random``. | ||
Example usage: | ||
```pycon | ||
>>> from coola.random import RandomRandomSeedSetter | ||
>>> setter = RandomRandomSeedSetter() | ||
>>> setter.manual_seed(42) | ||
``` | ||
""" | ||
|
||
def __repr__(self) -> str: | ||
return f"{self.__class__.__qualname__}()" | ||
|
||
def manual_seed(self, seed: int) -> None: | ||
random.seed(seed) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from __future__ import annotations | ||
|
||
import random | ||
|
||
from coola.random import RandomRandomSeedSetter | ||
|
||
############################################ | ||
# Tests for RandomRandomSeedSetter # | ||
############################################ | ||
|
||
|
||
def test_random_random_seed_setter_repr() -> None: | ||
assert repr(RandomRandomSeedSetter()).startswith("RandomRandomSeedSetter(") | ||
|
||
|
||
def test_random_random_seed_setter_str() -> None: | ||
assert str(RandomRandomSeedSetter()).startswith("RandomRandomSeedSetter(") | ||
|
||
|
||
def test_random_random_seed_setter_manual_seed() -> None: | ||
seed_setter = RandomRandomSeedSetter() | ||
seed_setter.manual_seed(42) | ||
x1 = random.uniform(0, 1) # noqa: S311 | ||
x2 = random.uniform(0, 1) # noqa: S311 | ||
seed_setter.manual_seed(42) | ||
x3 = random.uniform(0, 1) # noqa: S311 | ||
assert x1 == x3 | ||
assert x1 != x2 |