I am struggling finding a way to have a class Factory (I use factory_boy
version 2.11.1 with Python 3) using an alternate constructor defined as a @classmethod
.
So let's say we have a class for building a 2D-point object with a default constructor and 2 additional ones:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
@classmethod
def fromlist(cls, coords): # alternate constructor from list
return cls(coords[0], coords[1])
@classmethod
def duplicate(cls, obj): # alternate constructor from another Point
return cls(obj.x, obj.y)
I create a basic Point factory:
import factory
class PointFactory(factory.Factory):
class Meta:
model = Point
inline_args = ('x', 'y')
x = 1.
y = 2.
By default, it seems to call the constructor __init__
of the class which seems very logical. I could not find a way to pass inline_args
as being coords
for using the alternate constructor fromlist
. Is there a way to do so?
This is my first experience working and building factories in general so I may also be looking up at the wrong keywords on the web...
Aucun commentaire:
Enregistrer un commentaire