Package: distillery
distilleryis anotherfactory_girllike library for python
ORMs.Installationpip install distilleryDistilleriesEach distillery has a__model__and a set of attributes and
methods. The__model__is the ORM model class from which instance
will be produced:classUserDistillery(MyOrmDistillery):__model__=UserAttributesA distillery class attribute defines default values for specific model
field:classUserDistillery(MyOrmDistillery):__model__=Userusername="defaultusername"The distillery’s attribute values act asdefaults. If aUserobject is created using this distillery, itsusernameattribute
will default to"defaultusername".Lazy attributesUsing thelazydecorator, you can provide dynamic attributes.fromdistilleryimportlazyclassUserDistillery(MyOrmDistillery):__model__=Userusername="defaultusername"@lazydefemail_address(cls,instance,sequence):return"%s@%s"%(instance.username,instance.company.domain)All newUsercreated fromUserDistillerywill have anemail_addresscomputed from his username and his company domain.Note: all lazies received aninstanceand asequencethat are
the object instance and an auto-incremented sequence, respectively.HooksA distillery can provide an_after_createclass method to hook
into the factory machinery.classUserDistillery(MyOrmDistillery):__model__=Userusername="defaultusername"@classmethoddef_after_create(cls,instance):# Do stuff after instance creation# ...Distillery.init()Theinit()method creates and populates a new instance.user=UserDistillery.init()assertuser.username=="defaultusername"assertuser.idisNoneuser=UserDistillery.create(username="overriddenusername")assertuser.username=="overriddenusername"assertuser.idisNoneDistillery.create()Thecreate()method initializes the object usinginit()and
subsequentlysavesit.user=UserDistillery.create()assertuser.username=="defaultusername"assertuser.idisnotNoneDistillery.bulk()Creates instances in bulk.users=UserDistillery.bulk(12,username="user_%(i)%")assertusers[7].username='user_7'SetsThedistillery.Setclass acts as a fixture container.It’s required to define a__distillery__attribute which is used
to create objects.fromdistilleryimportSetclassUserSet(Set):__distillery__=UserDistilleryclassjeanphix:username='jeanphix'To create the fixtures, simply instantiate the set.users=UserSet()assertusers.jeanphix.username=='jeanphix'Importantly, as long as a reference to theinstantiated setis held
(e.g. theusersvariable in this example), the set can be called
again and the same instance is returned:assertUserSet()isUserSet()You can reference other sets, too. Note that you must reference using
the class, or use a lazy attribute (described later):fromdistilleryimportSetclassCompanySet(Set):__distillery__=CompanyDistilleryclassmy_company:name='My company'classUserSet(Set):__distillery__=UserDistilleryclassjeanphix:username='jeanphix'company=CompanySet.companyusers=UserSet()assertusers.jeanphix.company=='My company'In addition to classes, methods can be defined; each will result in an
object which is added to the set.classProfileSet(Set)class__distillery__:__model__=Profileadmin=lambdas:UserDistillery.create(username='admin').profileThis functionality extends to class-based references. Note that the
reference must be resolvable at the point of creation; circular
relationships are currently not supported.classUserSet(Set):classpeter:friend=Noneclasspaul:friend=classmethod(lambdac:UserSet.peter)If theon_demandflag is set, objects are created only when first
accessed.users=UserSet(on_demand=True)users.jeanphix# jeanphix will be created here.Finally, sets can be nested.classfixtures(Set):users=UserSetassertfixtures().users.jeanphix.username=='jeanphix'HooksEach fixture in a set can provide an_after_createlistener:classProfileSet(Set):class__distillery__:__model__=Profileclassadmin:@classmethoddef_after_create(cls,profile):profile.name='Full name'assertProfileSet().admin.name=='Full name'ORMsBoth Django and SQLAlchemy are supported.DjangoDjango models could be distilled usingDjangoDistillerythat only
requires a__model__class member:fromdistilleryimportDjangoDistilleryfromdjango.auth.modelsimportUserclassUserDistillery(DjangoDistillery):__model__=User#  ...SQLAlchemySQLAlchemy distilleries require both the__model__and__session__attributes.fromdistilleryimportSQLAlchemyDistilleryfromsqlalchemyimportcreate_enginefromsqlalchemy.ormimportsessionmakerengine=create_engine('sqlite://',echo=False)Session=sessionmaker()Session.configure(bind=engine)session=Session()Base=declarative_base()classUser(Base):#  ...classUserDistillery(SQLAlchemyDistillery):__model__=User__session__=session#  ...