[英]Python behavior of classes that have nested classes
I have a number of factory objects that look like the following:我有许多工厂对象,如下所示:
class OperatorFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta:
model = Operator
sqlalchemy_session = Session()
sqlalchemy_session_persistence = 'commit'
operator = 'equals'
They all share that same Meta
inner class and have distinct models, but they also share those sqlalchemy_session
and sqlalchemy_session_persistence
lines.它们都共享相同的Meta
内部类并具有不同的模型,但它们也共享那些sqlalchemy_session
和sqlalchemy_session_persistence
行。
My question is, is it possible to write this object in a more DRY (don't repeat yourself) fashion where I don't have to constantly include the lines sqlalchemy_session = Session()
and sqlalchemy_session_persistence = 'commit'
?我的问题是,是否有可能以更 DRY(不要重复自己)的方式编写这个对象,我不必经常包含sqlalchemy_session = Session()
和sqlalchemy_session_persistence = 'commit'
?
So far I have at least a dozen factories and it's getting repetitive.到目前为止,我至少有十几个工厂,而且越来越重复。
I'm not 100% certain what sessions you want to be shared, the only documentation I could find talks about sharing sessions between all factories but you are generating a separate session for each factory class but not for every instance .我不能 100% 确定您想要共享哪些会话,我能找到的唯一文档讨论了所有工厂之间的共享会话,但您正在为每个工厂类生成一个单独的会话,而不是为每个实例生成一个单独的会话。 (it is a class variable so it is shared between all instances) (它是一个类变量,所以它在所有实例之间共享)
Assuming this was an oversight and you actually want a single session for all Meta
fields, you would create a base class for the Meta class:假设这是一个疏忽,并且您实际上想要所有Meta
字段的单个会话,您将为 Meta 类创建一个基类:
class BaseMeta:
# now this sessions is shared between ALL factories
sqlalchemy_session = Session()
sqlalchemy_session_persistence = 'commit'
class OperatorFactory(factory.alchemy.SQLAlchemyModelFactory):
class Meta(BaseMeta):
model = Operator
operator = 'equals'
alternatively if you want each class to have their own session then you may want a factory function for your meta class of your factory class.或者,如果您希望每个类都有自己的会话,那么您可能需要工厂类的元类的工厂函数。
def generate_meta_options(model_):
class Meta:
model = model_
sqlalchemy_session = Session()
sqlalchemy_session_persistence = 'commit'
return Meta
class OperatorFactory(factory.alchemy.SQLAlchemyModelFactory):
Meta = generate_meta_options(Operator)
operator = 'equals'
If the intention is instead to have a separate session for every instance I'm not sure what should be done, I can't figure the relevant documentation on how the factory objects are instantiated.如果目的是为每个实例设置一个单独的会话,我不确定应该做什么,我无法确定有关如何实例化工厂对象的相关文档。
Instead of having Meta
as an inner-class you can make it a base class and just have factories inherit off it.与其将Meta
作为内部类,您还可以将其设为基类,然后让工厂继承它。 As you've said they have distinct models you can simply provide a parameter for that to __init__
.正如您所说,他们有不同的模型,您可以简单地为__init__
提供一个参数。 Take the following for example:以以下为例:
class Meta:
def __init__(self, Operator):
model = Operator
sqlalchemy_session = Session()
sqlalchemy_session_persistence = 'commit'
class Factory1(Meta, factory.alchemy.SQLAlchemyModelFactory):
def __init__(self):
op = 'equals'
super.__init__(op)
class Factory2(Meta, factory.alchemy.SQLAlchemyModelFactory):
def __init__(self):
op = 'lessthan'
super.__init__(op)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.