0

Is there any way to simplify this code, because each time putting an instance name to access the class variable is annoying. I am basically forming Kotlin background, we use with to access the properties of the class.

Kotlin eg:

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)

Saving notification to session.

                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
  • 1
    Maybe this might help. Commented Jun 19, 2017 at 8:53

1 Answer 1

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))

That's more lines of code but less repetitions.

docs:

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.