-
Notifications
You must be signed in to change notification settings - Fork 15
Open
Description
Hey,
I created some new number classes and wanted to share them. I might keep on working on some more.
With these classes it's possible to add / multiply multiple dependent fields and to create a RandomFloat where the maximum is dependent on another field.
class RelativeNumberAddFields(DependentField):
"""
Returns a number relative to another number field.
Example:
>>> import testdata
>>> class Foo(testdata.DictFactory):
... a = testdata.CountingFactory(1)
b = testdata.CountingFactory(5)
c = testdata.CountingFactory(10)
... d = testdata.RelativeNumberAdd('a', 'b', 'c')
>>> [i for i in Foo().generate(3)]
[{'a': 1, 'b': 5, 'c': 10, 'd': 16},
{'a': 2, 'b': 6, 'c': 11, 'd': 19},
{'a': 3, 'b': 7, 'c': 12, 'd': 22}]
"""
def __init__(self, *args):
super(RelativeNumberAddFields, self).__init__(args)
self._args = args
def __call__(self):
super(RelativeNumberAddFields, self).__call__()
result = 0
for value in self._args:
result += self.depending_fields[value]
return result
class RelativeNumberMultiplyFields(DependentField):
"""
Returns a number relative to another number field.
Example:
>>> import testdata
>>> class Foo(testdata.DictFactory):
... a = testdata.CountingFactory(1)
b = testdata.CountingFactory(5)
c = testdata.CountingFactory(10)
... d = testdata.RelativeNumberAdd('a', 'b', 'c')
>>> [i for i in Foo().generate(3)]
[{'a': 1, 'b': 5, 'c': 10, 'd': 50},
{'a': 2, 'b': 6, 'c': 11, 'd': 132},
{'a': 3, 'b': 7, 'c': 12, 'd': 252}]
"""
def __init__(self, *args):
super(RelativeNumberMultiplyFields, self).__init__(args)
self._args = args
def __call__(self):
super(RelativeNumberMultiplyFields, self).__call__()
result = 1
for value in self._args:
result = result * self.depending_fields[value]
return result
class ConditionalMaxRandomFloat(DependentField):
"""
Returns a Float between `minimum` and `maximum`
"""
def __init__(self, minimum, maximum):
super(ConditionalMaxRandomFloat, self).__init__([maximum])
self._maximum = maximum
self._minimum = minimum
def __call__(self):
super(ConditionalMaxRandomFloat, self).__call__()
return (random.random() * (self.depending_fields[self._maximum] - self._minimum)) + self._minimum
Metadata
Metadata
Assignees
Labels
No labels