-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Provides several built-in providers for common use cases. at 2024-06-…
…24T16:02:25+0800
- Loading branch information
1 parent
526f58a
commit deaba24
Showing
8 changed files
with
116 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,5 @@ | ||
from .cyclic_parameter import CyclicParameterProvider | ||
from .fixed_value import FixedValueProvider | ||
from .random_choice_from_list import RandomChoiceFromListProvider | ||
from .random_float_in_range import RandomFloatInRangeProvider | ||
from .incremental_value import IncrementalValueProvider |
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,20 @@ | ||
import itertools | ||
|
||
from pistol_magazine import DataMocker, provider | ||
|
||
|
||
@provider | ||
class CyclicParameterProvider(DataMocker): | ||
def __init__(self, parameter_list: list = None): | ||
""" | ||
This class provides parameters in a cyclic manner from the given list. | ||
If no list is provided, it uses a default list of parameters. | ||
:param parameter_list: A list of parameters to cycle through. If None, a default list is used. | ||
""" | ||
super().__init__() | ||
if parameter_list is None: | ||
parameter_list = ["default_param1", "default_param2", "default_param3"] | ||
self.parameters = itertools.cycle(parameter_list) | ||
|
||
def get_next_param(self): | ||
return next(self.parameters) |
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,19 @@ | ||
from pistol_magazine import DataMocker | ||
|
||
|
||
class FixedValueProvider(DataMocker): | ||
def __init__(self, fixed_value=None): | ||
""" | ||
This class always returns a fixed value. | ||
If no value is provided, it uses a default fixed value. | ||
Args: | ||
fixed_value: The fixed value to return. If None, a default value is used. | ||
""" | ||
super().__init__() | ||
if fixed_value is None: | ||
fixed_value = "default_fixed_value" | ||
self.value = fixed_value | ||
|
||
def get_fixed_value(self): | ||
return self.value |
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,20 @@ | ||
from pistol_magazine import DataMocker | ||
|
||
|
||
class IncrementalValueProvider(DataMocker): | ||
def __init__(self, start=0, step=1): | ||
""" | ||
This class provides incrementing values starting from a given value. | ||
Args: | ||
start (int): The starting value. Defaults to 0. | ||
step (int): The step value for each increment. Defaults to 1. | ||
""" | ||
super().__init__() | ||
self.current = start | ||
self.step = step | ||
|
||
def get_next_value(self): | ||
value = self.current | ||
self.current += self.step | ||
return value |
21 changes: 21 additions & 0 deletions
21
pistol_magazine/built_in_provider/random_choice_from_list.py
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,21 @@ | ||
from random import choice | ||
|
||
from pistol_magazine import DataMocker | ||
|
||
|
||
class RandomChoiceFromListProvider(DataMocker): | ||
def __init__(self, value_list: list = None): | ||
""" | ||
This class provides random values from the given list. | ||
If no list is provided, it uses a default list of values. | ||
Args: | ||
value_list (list): A list of values to choose from randomly. If None, a default list is used. | ||
""" | ||
super().__init__() | ||
if value_list is None: | ||
value_list = ["default_random1", "default_random2", "default_random3"] | ||
self.values = value_list | ||
|
||
def get_random_value(self): | ||
return choice(self.values) |
29 changes: 29 additions & 0 deletions
29
pistol_magazine/built_in_provider/random_float_in_range.py
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,29 @@ | ||
from random import uniform | ||
|
||
from pistol_magazine import DataMocker | ||
|
||
|
||
class RandomFloatInRangeProvider(DataMocker): | ||
def __init__(self, start=0.0, end=1.0, precision=2): | ||
""" | ||
This class provides random float values within a specified range and precision. | ||
Args: | ||
start (float): The starting range. Defaults to 0.0. | ||
end (float): The ending range. Defaults to 1.0. | ||
precision (int): The number of decimal places to round to. Defaults to 2. | ||
""" | ||
super().__init__() | ||
self.start = start | ||
self.end = end | ||
self.precision = precision | ||
|
||
def get_random_float(self): | ||
""" | ||
Returns a random float value within the specified range rounded to the specified precision. | ||
Returns: | ||
A random float value between start and end, rounded to the specified number of decimal places. | ||
""" | ||
value = uniform(self.start, self.end) | ||
return round(value, self.precision) |
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