ans123's picture
Initial upload from Colab
ef1ad9e verified
from typing import Any
from datetime import datetime
def to_dict(model_instance) -> dict[str, Any]:
"""
Returns a dictionary representation of a model instance.
Includes all attributes defined on the model class.
Args:
model_instance: The model instance to convert to a dictionary.
Returns:
A dictionary containing all attributes and their values.
"""
return {
c.name: getattr(model_instance, c.name)
for c in model_instance.__table__.columns
}
def get_current_datetime():
# Get the current datetime object
now = datetime.now()
# Format the datetime object in the desired format and return
return now.strftime("%Y-%m-%d %H:%M:%S.%f")
def update_table(db, model, data_dict):
for field, value in data_dict.items():
# Exclude updating if field is 'userId'
if field == "userId":
continue
if isinstance(value, bool):
value = str(int(value))
setattr(model, field, value)
db.add(model)
db.commit()
return