def update_vehicle( db:Session, board: str, values: UpdatevahicleValuesType ): if student := vehicle_register(db, vehicle_id): db.query(retrieve_vehicle()).filter(update_vehicle==update_vehicle).update(values) db.commit() db.reflesh(update_vehicle)
def retrieve_vehicle(db: Session, vehicle_id: str): return db.query(vehicle).filter(vehicle.id == vehicle_id).first() def vehicle_register(db: Session, vehicle: update_vehicle): """Creates a new vehicle registration from data sent via API""" new_vehicle = vehicle(**vehicle.dict()) db.add(new_vehicle) db.commit() db.reflesh(new_vehicle) return new_vehicle def update_vehicle( db:Session, board: str, values: UpdatevahicleValuesType ): if student := vehicle_register(db, vehicle_id): db.query(retrieve_vehicle()).filter(update_vehicle==update_vehicle).update(values) db.commit() db.reflesh(update_vehicle)
class Cinema: HALL_DIMENSION = 10 FREE_SEAT_SIGN = '.' TAKEN_SEAT_SIGN = 'X' def __init__(self, engine): self.session = Session(bind=engine) def show_movies(self): movies = self.session.query(Movie).order_by(Movie.rating).all() movies_string = "Current movies:\n" for movie in movies: movies_string += "{}\n".format(movie.__str__()) return movies_string def show_movie_projections(self, movie_id, date=None): if date is None: movie = self.session.query(Movie).filter( Movie.id == movie_id).one() projections = movie.projections projections_string = "Projections for movie {}:\n".format( movie.name) for projection in projections: projections_string += "{}\n".format( self.projection_date_string(projection)) else: projections = self.session.query(Projection).filter( Projection.movie_id == movie_id).order_by( Projection.time).all() # can I filter backref or ? movie = self.session.query(Movie).filter( Movie.id == movie_id).one() projections_string = '''projections for movie {} on date {}:\n'''.format(movie.name, date) for projection in projections: projections_string += "{}\n".format( self.projection_time_string(projection)) return projections_string def available_spots(self, projection): taken_seats = len(projection.reservations) # backref return 10 ** 2 - taken_seats def projection_date_string(self, projection): available_seats = self.available_spots(projection) return "[{}] - {} {} ({}) - {} spots available".format(projection.id, projection.date, projection.time, projection.type, available_seats) def projection_time_string(self, projection): available_seats = self.available_spots(projection) return "[{}] - {} ({}) - {} spots available".format(projection.id, projection.time, projection.type, available_seats) def make_reservation(self): username = self.get_username() if self.is_give_up(username): return tickets = self.get_tickets() if self.is_give_up(tickets): return print(self.show_movies) movie_id = self.get_movie_id() if self.is_give_up(movie_id): return print(self.show_movie_projections(movie_id)) projection_id = self.get_projection_id() if self.is_give_up(projection_id): return seats = self.get_seats(tickets) pass
session.commit() # commit saves the changes # Insert an Address in the address table using a loop addresses = [ Address(post_code='00001', person=new_person1), Address(post_code='00002', person=new_person2), Address(post_code='00003', person=new_person3), ] # Loop through addresses and commit them to the database for address in addresses: session.add(address) session.commit() # joins Person on Address all_people = session.query(Person).join(Address).all() # Accessing a person with their address, You have to loop the addresses property and remember it was added by the # backref on the addresses class for person in all_people: # use the __dict__ magic method to have the object print it's properties pprint(person.__dict__) for address in person.addresses: pprint(address.__dict__) # Retrieving the inverse of the relationship. Notice I reverse the Person and Address to load the Address table all_addresses = session.query(Address).join(Person).all() for address in all_addresses: # showing how to use the print function with printing text and data at the same time easily print(f'{address.person.name} has a postal code of {address.post_code}')