summaryrefslogtreecommitdiff
path: root/factory
diff options
context:
space:
mode:
authorRaphaël Barrois <raphael.barrois@polytechnique.org>2016-02-14 23:12:02 +0000
committerRaphaël Barrois <raphael.barrois@polytechnique.org>2016-02-14 23:12:02 +0000
commit4fc9caa17bd464cf749bfba6af0a4c698da30ccb (patch)
tree6ec891d81d4f1a363ad9ca47cc62ae19946d6985 /factory
parent86dc8baea87c69e1d39a3d87e1271fbd0e85ca63 (diff)
parentf2c075c40fd331b7d26a9db72aad249b2165eac4 (diff)
downloadfactory-boy-4fc9caa17bd464cf749bfba6af0a4c698da30ccb.tar
factory-boy-4fc9caa17bd464cf749bfba6af0a4c698da30ccb.tar.gz
Merge pull request #267 from bors-ltd/master
factory: LazyFunction to just call a function in the simplest case
Diffstat (limited to 'factory')
-rw-r--r--factory/__init__.py1
-rw-r--r--factory/declarations.py17
2 files changed, 18 insertions, 0 deletions
diff --git a/factory/__init__.py b/factory/__init__.py
index c8bc396..1fa581b 100644
--- a/factory/__init__.py
+++ b/factory/__init__.py
@@ -43,6 +43,7 @@ from .base import (
from .faker import Faker
from .declarations import (
+ LazyFunction,
LazyAttribute,
Iterator,
Sequence,
diff --git a/factory/declarations.py b/factory/declarations.py
index f0dbfe5..9ab7462 100644
--- a/factory/declarations.py
+++ b/factory/declarations.py
@@ -57,6 +57,23 @@ class OrderedDeclaration(object):
raise NotImplementedError('This is an abstract method')
+class LazyFunction(OrderedDeclaration):
+ """Simplest OrderedDeclaration computed by calling the given function.
+
+ Attributes:
+ function (function): a function without arguments and
+ returning the computed value.
+ """
+
+ def __init__(self, function, *args, **kwargs):
+ super(LazyFunction, self).__init__(*args, **kwargs)
+ self.function = function
+
+ def evaluate(self, sequence, obj, create, extra=None, containers=()):
+ logger.debug("LazyFunction: Evaluating %r on %r", self.function, obj)
+ return self.function()
+
+
class LazyAttribute(OrderedDeclaration):
"""Specific OrderedDeclaration computed using a lambda.