aboutsummaryrefslogtreecommitdiff
path: root/factory
diff options
context:
space:
mode:
authorOmer <omer@stokeet.com>2013-09-30 12:37:15 +0300
committerRaphaƫl Barrois <raphael.barrois@polytechnique.org>2013-10-29 00:25:32 +0100
commit132eca8fa36bc7360a3b2270da0c8833378bc718 (patch)
tree366eff75d3c2d12c67d96aeb56da61d8d3bc746a /factory
parentdc482f67c83be2b2607e2e9c4b259c03a59d9532 (diff)
downloadfactory-boy-132eca8fa36bc7360a3b2270da0c8833378bc718.tar
factory-boy-132eca8fa36bc7360a3b2270da0c8833378bc718.tar.gz
Added a Fuzzy Decimal attribute.
Diffstat (limited to 'factory')
-rw-r--r--factory/fuzzy.py29
1 files changed, 24 insertions, 5 deletions
diff --git a/factory/fuzzy.py b/factory/fuzzy.py
index f3e6a31..7fa0908 100644
--- a/factory/fuzzy.py
+++ b/factory/fuzzy.py
@@ -25,7 +25,7 @@
from __future__ import unicode_literals
-
+from decimal import Decimal
import random
import string
import datetime
@@ -121,8 +121,27 @@ class FuzzyInteger(BaseFuzzyAttribute):
return random.randint(self.low, self.high)
+class FuzzyDecimal(BaseFuzzyAttribute):
+ """Random decimal within a given range."""
+
+ def __init__(self, low, high=None, precision=2, **kwargs):
+ if high is None:
+ high = low
+ low = 0.0
+
+ self.low = low
+ self.high = high
+ self.precision = precision
+
+ super(FuzzyDecimal, self).__init__(**kwargs)
+
+ def fuzz(self):
+ return Decimal(random.uniform(self.low, self.high)).quantize(Decimal(10) ** -self.precision)
+
+
class FuzzyDate(BaseFuzzyAttribute):
"""Random date within a given date range."""
+
def __init__(self, start_date, end_date=None, **kwargs):
super(FuzzyDate, self).__init__(**kwargs)
if end_date is None:
@@ -150,12 +169,12 @@ class BaseFuzzyDateTime(BaseFuzzyAttribute):
if start_dt > end_dt:
raise ValueError(
"""%s boundaries should have start <= end, got %r > %r""" % (
- self.__class__.__name__, start_dt, end_dt))
+ self.__class__.__name__, start_dt, end_dt))
def __init__(self, start_dt, end_dt=None,
- force_year=None, force_month=None, force_day=None,
- force_hour=None, force_minute=None, force_second=None,
- force_microsecond=None, **kwargs):
+ force_year=None, force_month=None, force_day=None,
+ force_hour=None, force_minute=None, force_second=None,
+ force_microsecond=None, **kwargs):
super(BaseFuzzyDateTime, self).__init__(**kwargs)
if end_dt is None: