我有一个简单的函数,它是这样调用的:
arbitrary_function(**kwargs1, **kwargs2, **kwargs3)
Run Code Online (Sandbox Code Playgroud)
它似乎在我的本地安装(python 3.5.1)上编译得很好,但是当我在带有 python 3.4.5 的 docker 上编译它时会引发 SyntaxError。
我不太确定为什么会出现这种行为。不允许使用多个 kwarg 吗?我应该在传递给函数之前组合它们吗?单独传递它们更方便,例如:
plot(**x_axis_params, **y_axis_params, **plot_params)
Run Code Online (Sandbox Code Playgroud)
代替
params = dict()
for specific_param in [x_axis_params, y_axis_params, plot_params]:
params.update(specific_param)
plot(**params)
Run Code Online (Sandbox Code Playgroud) 我有两个班级,TrialIdentifier和TimeCourse
TimeCourse 有一个包含 TrialIdentifier 的实例变量,我正在尝试在两者之间设置外键关系。
在 TrialIdentifier 中
__tablename__ = 'trial_identifiers'
relationships = relationship('TimeCourse',
back_populates = 'trial_identifier', uselist = False)
Run Code Online (Sandbox Code Playgroud)
在时间课程
__tablename__ = 'time_course'
trial_identifier_id = Column(Integer, ForeignKey('trial_identifiers.id'))
trial_identifier = relationship('TrialIdentifier', back_populates = 'relationships')`
Run Code Online (Sandbox Code Playgroud)
如果我trial_identifier在TimeCourse以下错误中命名变量,则会引发:
sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|TimeCourse|time_course' has no property 'trial_identifier'
Run Code Online (Sandbox Code Playgroud)
如果我将其命名为其他任何名称,则一切正常。整个包都建立在 上,TimeCourse().trial_identifier所以如果可能的话,我想避免重构它。或者至少理解这种行为。
我有两个清单:
我想检查我的每个菌株的replicateID是否在所选菌株的列表中,在python中它将是这样的:
for strain in strainInfo:
if strain.replicateID in [selectedStrain.replicateID for selectedStrain in selectedStrainInfo]
print('This strain is selected')
Run Code Online (Sandbox Code Playgroud)
我在django中获得了正确的功能,但我想知道是否有一种方法可以简化使用列表理解:
{% for row in strainInfo %}
{% for selectedStrain in selectedStrainsInfo %}
{% if row.replicateID == selectedStrain.replicateID %}
checked
{% endif %}
{% endfor %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud) 我想知道是否有可能防止向数据库提交重复项。例如,假设有一个类如下
class Employee(Base):
id = Column(Integer, primary_key=True)
name = Column(String)
Run Code Online (Sandbox Code Playgroud)
如果我要制作一系列这些物品,
employee1 = Employee(name='bob')
employee2 = Employee(name='bob')
session.add_all([employee1, employee2])
session.commit()
Run Code Online (Sandbox Code Playgroud)
我想只添加单个行到数据库中,并employee1与employee2以指向同一个对象在内存中(如果可能)。
SQLAlchemy 中是否有实现此目的的功能?或者我是否需要以编程方式确保重复项不存在?