def remove_vehicle(db: Session, vehicle_id: str) -> bool: if vehicle := retrieve_vehicle(db, vehicle_id): db.delete(vehicle) db.commit() return True return Falseclass Veicle(BaseModel): veicle_id: (uuid.uuid1(str)) board: str model: str like_vehicle: str
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 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 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)
def session_multi_add_commit(Session, list_of_instances): sess = Session() sess.add_all(list_of_instances) sess.commit()
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy import Column, Integer, String from sqlalchemy import create_engine from settings import DB_CONNECTION_STRING from sqlalchemy import Session Base = declarative_base() class Person(Base): __tablename__ = "Peaople" id = Column(Integer, primery_key=True) name = Column(String(20)) engine = create_engine(DB_CONNECTION_STRING) Base.metadata.create_all(engine) session = Session(bind=engine) kolio = Person(name="Kolio") session.add(kolio) seesion.commit()
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
def __init__(self, engine): self.session = Session(bind=engine)
from sqlalchemy import Column, ForeignKey, Integer, String from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import relationship from sqlalchemy import create_engine, Session from sqlalchemy.orm import sessionmaker from pprint import pprint # Create an engine that stores data in the local directory's # sqlalchemy_example.db file. engine = create_engine('sqlite:////web/Sqlite-Data/example.db') Session = sessionmaker(bind=engine) session = Session() # this loads the sqlalchemy base class Base = declarative_base() # Setting up the classes that create the record objects and define the schema class Customer(Base): __tablename__ = 'customer' first_name = Column(String(250), nullable=False) last_name = Column(String(250), nullable=False) username = Column(String(250), nullable=False) email = Column(String(250)) address = Column(String(250)) town = Column(String(250)) class Item(Base): __tablename__ = 'items' name = Column(String(250))
from flask import (Flask, render_template, jsonify, request, redirect) # create instance of Flask app app = Flask(__name__) # setup database engine = create_engine("sqlite:///db/belly_button_biodiversity.sqlite", echo=False) Base = automap_base() Base.prepare(engine, reflect=True) Sample = Base.classes.samples OTU = Base.classes.otu Metadata = Base.classes.samples_metadata session = Session(engine) # create route that renders index.html template @app.route("/") def home(): return render_template("index.html") @app.route('/names') def names(): all_samples = session.query(Sample).statement all_samples_df = pd.read_sql_query(all_samples, session.bind) all_samples_df.set_index('otu_id', inplace=True) return jsonify(list(all_samples_df.columns))