def record_data(label: str, device: MetaWearClient) -> None: """ This function commits the data streamed to the database :param label: :param device: :return None: """ print(f"Logging data for {label}") acc, gyr = stream_data(device=device) print("Finished!") gx = gyr[0].replace('[', '').replace(']', '') gy = gyr[1].replace('[', '').replace(']', '') gz = gyr[2].replace('[', '').replace(']', '') ax = acc[0].replace('[', '').replace(']', '') ay = acc[1].replace('[', '').replace(']', '') az = acc[2].replace('[', '').replace(']', '') body_acc_x = BodyAccX(row_data=ax, label=label) body_acc_y = BodyAccY(row_data=ay, label=label) body_acc_z = BodyAccZ(row_data=az, label=label) body_gyr_x = BodyGyroX(row_data=gx, label=label) body_gyr_y = BodyGyroY(row_data=gy, label=label) body_gyr_z = BodyGyroZ(row_data=gz, label=label) print(f"Committing {label} data to database...") s = Session() s.add_all([ body_acc_x, body_acc_y, body_acc_z, body_gyr_x, body_gyr_y, body_gyr_z ]) s.commit() print("Finished!") s.close()
from utils import step, configure_logger, Session, silence configure_logger() with step(): session = Session() # Grab any ol' Zebra zebra = session.query(Zebra).first() session.commit() with step(): print(f'Hello, {zebra.name}!') # Don't show this with silence(): session.close() # JUST FOR MY OWN VERIFICATION: What happens when you rollback? with step(): session = Session() # Grab any ol' Zebra print(session.query(Zebra).first()) # Rollback instead of commit session.rollback() print(f'Hello, {zebra.name}!') with step(): session = Session() zebra = session.query(Zebra).first() print(f'Hello, {zebra.name}!') session.expunge(zebra)