summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2014-01-22 22:47:11 +0100
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2014-01-22 22:47:43 +0100
commit39383fea8bd5bd58b063d9c9fbb44301e781fd80 (patch)
tree82a42004c0ef1ad0f1e132399d426354b3ea080c
parent8639d0f70b03acd0cd76f1c81207461e185f8615 (diff)
downloadfactory-boy-39383fea8bd5bd58b063d9c9fbb44301e781fd80.tar
factory-boy-39383fea8bd5bd58b063d9c9fbb44301e781fd80.tar.gz
fuzzy: Add FuzzyFloat (Closes #124).
As suggested by @savingschampion
-rw-r--r--docs/changelog.rst1
-rw-r--r--docs/fuzzy.rst30
-rw-r--r--factory/fuzzy.py17
3 files changed, 46 insertions, 2 deletions
diff --git a/docs/changelog.rst b/docs/changelog.rst
index 57b29c4..aba1d76 100644
--- a/docs/changelog.rst
+++ b/docs/changelog.rst
@@ -11,6 +11,7 @@ ChangeLog
- Add support for :attr:`factory.fuzzy.FuzzyInteger.step`, thanks to `ilya-pirogov <https://github.com/ilya-pirogov>`_ (:issue:`120`)
- Add :meth:`~factory.django.mute_signals` decorator to temporarily disable some signals, thanks to `ilya-pirogov <https://github.com>`_ (:issue:`122`)
+ - Add :class:`~factory.fuzzy.FuzzyFloat` (:issue:`124`)
.. _v2.3.1:
diff --git a/docs/fuzzy.rst b/docs/fuzzy.rst
index 2fe60b8..1480419 100644
--- a/docs/fuzzy.rst
+++ b/docs/fuzzy.rst
@@ -107,9 +107,9 @@ FuzzyInteger
FuzzyDecimal
------------
-.. class:: FuzzyDecimal(low[, high])
+.. class:: FuzzyDecimal(low[, high[, precision=2]])
- The :class:`FuzzyDecimal` fuzzer generates random integers within a given
+ The :class:`FuzzyDecimal` fuzzer generates random :class:`decimals <decimal.Decimal>` within a given
inclusive range.
The :attr:`low` bound may be omitted, in which case it defaults to 0:
@@ -140,6 +140,32 @@ FuzzyDecimal
int, the number of digits to generate after the dot. The default is 2 digits.
+FuzzyFloat
+----------
+
+.. class:: FuzzyFloat(low[, high])
+
+ The :class:`FuzzyFloat` fuzzer provides random :class:`float` objects within a given inclusive range.
+
+ .. code-block:: pycon
+
+ >>> FuzzyFloat(0.5, 42.7)
+ >>> fi.low, fi.high
+ 0.5, 42.7
+
+ >>> fi = FuzzyFloat(42.7)
+ >>> fi.low, fi.high
+ 0.0, 42.7
+
+
+ .. attribute:: low
+
+ decimal, the inclusive lower bound of generated floats
+
+ .. attribute:: high
+
+ decimal, the inclusive higher bound of generated floats
+
FuzzyDate
---------
diff --git a/factory/fuzzy.py b/factory/fuzzy.py
index 2ea544a..94599b7 100644
--- a/factory/fuzzy.py
+++ b/factory/fuzzy.py
@@ -141,6 +141,23 @@ class FuzzyDecimal(BaseFuzzyAttribute):
return base.quantize(decimal.Decimal(10) ** -self.precision)
+class FuzzyFloat(BaseFuzzyAttribute):
+ """Random float within a given range."""
+
+ def __init__(self, low, high=None, **kwargs):
+ if high is None:
+ high = low
+ low = 0
+
+ self.low = low
+ self.high = high
+
+ super(FuzzyFloat, self).__init__(**kwargs)
+
+ def fuzz(self):
+ return random.uniform(self.low, self.high)
+
+
class FuzzyDate(BaseFuzzyAttribute):
"""Random date within a given date range."""