codeflash/code_to_optimize/book_catalog.py

75 lines
2.4 KiB
Python
Raw Normal View History

2024-05-16 16:55:11 +00:00
from __future__ import annotations
2024-05-16 03:07:11 +00:00
from time import time
2024-05-16 16:55:11 +00:00
from typing import Any, cast
2024-05-16 03:07:11 +00:00
2024-05-16 16:55:11 +00:00
from _typeshed import SupportsDunderGT, SupportsDunderLT
2024-05-16 03:07:11 +00:00
from sqlalchemy import Boolean, Column, ForeignKey, Integer, Text
2024-05-16 16:55:11 +00:00
from sqlalchemy.engine import Engine, create_engine
from sqlalchemy.orm import DeclarativeBase, Session, relationship, sessionmaker
from sqlalchemy.orm.relationships import _RelationshipDeclared
POSTGRES_CONNECTION_STRING: str = "postgresql://cf_developer:XJcbU37MBYeh4dDK6PTV5n@sqlalchemy-experiments.postgres.database.azure.com:5432/postgres"
2024-05-16 03:07:11 +00:00
2024-05-16 16:55:11 +00:00
class Base(DeclarativeBase):
pass
2024-05-16 03:07:11 +00:00
class Author(Base):
2024-05-16 16:55:11 +00:00
__tablename__: str = "authors"
2024-05-16 03:07:11 +00:00
2024-05-16 16:55:11 +00:00
id: Column[int] = Column(Integer, primary_key=True)
name: Column[str] = Column(Text, nullable=False)
2024-05-16 03:07:11 +00:00
class Book(Base):
2024-05-16 22:38:37 +00:00
__tablename__: str = "books"
2024-05-16 03:07:11 +00:00
2024-05-16 16:55:11 +00:00
id: Column[int] = Column(Integer, primary_key=True)
title: Column[str] = Column(Text, nullable=False)
author_id: Column[int] = Column(Integer, ForeignKey("authors.id"), nullable=False)
is_bestseller: Column[bool] = Column(Boolean, default=False)
2024-05-16 03:07:11 +00:00
2024-05-16 16:55:11 +00:00
author: _RelationshipDeclared[Author] = relationship("Author", backref="books")
2024-05-16 03:07:11 +00:00
2024-05-16 16:55:11 +00:00
def init_table() -> Session:
engine: Engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)
session: Session = sessionmaker(bind=engine)()
i: int
2024-05-16 03:07:11 +00:00
for i in range(50):
2024-05-16 16:55:11 +00:00
author: Author = Author(id=i, name=f"author{i}")
2024-05-16 03:07:11 +00:00
session.add(author)
for i in range(100000):
2024-05-16 16:55:11 +00:00
book: Book = Book(id=i, title=f"book{i}", author_id=i % 50, is_bestseller=i % 2 == 0)
2024-05-16 03:07:11 +00:00
session.add(book)
session.commit()
return session
2024-05-16 16:55:11 +00:00
def get_authors(session: Session) -> list[Author]:
books: list[Book] = session.query(Book).all()
_authors: list[Author] = []
book: Book
2024-05-16 03:07:11 +00:00
for book in books:
_authors.append(book.author)
2024-05-16 16:55:11 +00:00
return sorted(
list(set(_authors)),
key=lambda x: cast(SupportsDunderLT[Any] | SupportsDunderGT[Any], x.id),
)
2024-05-16 03:07:11 +00:00
if __name__ == "__main__":
# _session = init_table()
2024-05-16 16:55:11 +00:00
engine: Engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)
session_factory: sessionmaker[Session] = sessionmaker(bind=engine)
_session: Session = session_factory()
_t: float = time()
authors: list[Author] = get_authors(_session)
2024-05-16 03:07:11 +00:00
print("TIME TAKEN", time() - _t)
authors_name = list(map(lambda x: x.name, authors))
print("len(authors_name)", len(authors_name))
print(set(authors_name))