如何在为类变量赋值时减少样板代码

时间:2017-06-19 07:53:33

标签: python

有没有办法简化这段代码,因为每次放一个实例名来访问类变量都很烦人。我基本上形成了Kotlin背景,我们使用with来访问类的属性。

Kotlin例如:

val notification  = Notification()

with(notification){
created_at = datetime.datetime.now()
recipient_id = recipient
//etc

}

notification.py

class Notification(CustomerBase, Base):
    __tablename__ = "notification"
    created_at = Column(DateTime, nullable=True, default=datetime.datetime.now())
    recipient_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    sender_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    data = Column(JSONB, nullable=True)
    message = Column(String, nullable=True)
    type = Column(String, nullable=True)
    activity_id = Column(Integer, nullable=True)
    is_read = Column(BOOLEAN, nullable=True, default=False)
    sender_info = relationship("User", foreign_keys=[sender_id])
    recipient_info = relationship("User", foreign_keys=[recipient_id])
    entity_id = Column(Integer, nullable=True)
    entity_name = Column(String, nullable=True)

将通知保存到会话。

                notification = Notification()
                notification.created_at = datetime.datetime.now()
                notification.recipient_id = recipient
                notification.sender_id = activity.created_by
                notification.message = add_people_msg(user, added_user, activity)
                notification.type = NotificationTypes.PEOPLE.value
                notification.customer_id = activity.customer_id
                notification.entity_id = activity.id
                notification.customer_id = activity.customer_id
                notification.activity_id = activity.id
                notification.data = activity_data

                self.session.add(notification)

1 个答案:

答案 0 :(得分:0)

class Notification(CustomerBase, Base):
__tablename__ = "notification"

# Define table columns
created_at = Column(DateTime, nullable=True, 
default=datetime.datetime.now())
recipient_id = Column(Integer, ForeignKey("users.id"), nullable=True)
sender_id = Column(Integer, ForeignKey("users.id"), nullable=True)
data = Column(JSONB, nullable=True)
message = Column(String, nullable=True)
type = Column(String, nullable=True)
activity_id = Column(Integer, nullable=True)
is_read = Column(BOOLEAN, nullable=True, default=False)
sender_info = relationship("User", foreign_keys=[sender_id])
recipient_info = relationship("User", foreign_keys=[recipient_id])
entity_id = Column(Integer, nullable=True)
entity_name = Column(String, nullable=True)

def __init__(*args, **kwargs):

    # Dynamically set attributes from keyword arguments
    # This is not very explicit, 
    # and therefore should be properly documented
    for k, v in kwargs:
        setattr(self, k, v)

# Set kwork arguments 
notification_args = {
    'created_at': datetime.datetime.now(),
    'recipient_id': recipient,
    'sender_id': activity.created_by,
    'message': add_people_msg(user, added_user, activity),
    'type': NotificationTypes.PEOPLE.value,
    'customer_id': activity.customer_id,
    'entity_id': activity.id,
    'customer_id': activity.customer_id,
    'activity_id': activity.id,
    'data': activity_data
}

# Add the instance to the session
self.session.add(Notification(**notification_args))

这是更多的代码行,但重复次数更少。

文档: