codeflash/code_to_optimize/book_catalog.py

110 lines
3.6 KiB
Python
Raw Permalink Normal View History

2024-10-13 01:58:44 +00:00
from time import time
2024-06-27 22:37:14 +00:00
from typing import List
from sqlalchemy import Boolean, Column, ForeignKey, Integer, Text, func
2024-05-16 16:55:11 +00:00
from sqlalchemy.engine import Engine, create_engine
from sqlalchemy.orm import DeclarativeBase, Session, relationship, sessionmaker
2024-05-22 01:10:46 +00:00
from sqlalchemy.orm.relationships import Relationship
2024-05-16 16:55:11 +00:00
2024-05-22 01:10:46 +00:00
POSTGRES_CONNECTION_STRING: str = ("postgresql://cf_developer:XJcbU37MBYeh4dDK6PTV5n@sqlalchemy-experiments.postgres"
".database.azure.com:5432/postgres")
2024-05-16 16:55:11 +00:00
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-22 01:10:46 +00:00
author: Relationship[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:
2024-05-22 01:10:46 +00:00
catalog_engine: Engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)
session: Session = sessionmaker(bind=catalog_engine)()
2024-05-16 16:55:11 +00:00
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
def get_authors(books: list[Book]) -> list[Author]:
2024-05-16 16:55:11 +00:00
_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)),
2024-05-22 01:10:46 +00:00
key=lambda x: x.id,
2024-05-16 16:55:11 +00:00
)
2024-05-16 03:07:11 +00:00
2024-05-31 10:57:26 +00:00
def get_authors2(num_authors) -> list[Author]:
engine: Engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)
session_factory: sessionmaker[Session] = sessionmaker(bind=engine)
session: Session = session_factory()
books: list[Book] = session.query(Book).all()
_authors: list[Author] = []
book: Book
for book in books:
_authors.append(book.author)
return sorted(
list(set(_authors)),
key=lambda x: x.id,
)[:num_authors]
2024-05-16 03:07:11 +00:00
2024-06-27 22:37:14 +00:00
def get_top_author(authors: List[Author]) -> Author:
engine: Engine = create_engine(POSTGRES_CONNECTION_STRING, echo=True)
session_factory: sessionmaker[Session] = sessionmaker(bind=engine)
session: Session = session_factory()
# Step 1: Initialize variables to keep track of the author with the maximum bestsellers
max_bestsellers = 0
top_author = None
# Step 2: Iterate over each author to count their bestsellers
for author in authors:
bestseller_count = (
session.query(func.count(Book.id))
.filter(Book.author_id == author.id, Book.is_bestseller == True)
.scalar()
)
# Step 3: Update the author with the maximum bestsellers
if bestseller_count > max_bestsellers:
max_bestsellers = bestseller_count
top_author = author
return top_author
2024-05-16 03:07:11 +00:00
if __name__ == "__main__":
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))