Skip to content

Commit

Permalink
split up functions
Browse files Browse the repository at this point in the history
  • Loading branch information
milsman2 committed Feb 14, 2024
1 parent 8cc8168 commit a4fc9af
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 30 deletions.
Binary file modified scraper/src/disc_golfers.db
Binary file not shown.
47 changes: 17 additions & 30 deletions scraper/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,11 @@
from icecream import ic
from scraper.src.models import DiscGolfer, Base
import asyncio
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
from sqlalchemy.future import select
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker, AsyncSession


async def run():
async def insert_objects(async_session: async_sessionmaker[AsyncSession]) -> None:
ic()
engine = create_async_engine(
"sqlite+aiosqlite:///scraper/src/disc_golfers.db",
echo=True,
)

async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)

async_session = async_sessionmaker(engine, expire_on_commit=False)

async with async_session() as session:
async with session.begin():
session.add_all(
Expand All @@ -47,23 +34,23 @@ async def run():
),
]
)
stmt = select(DiscGolfer)
# AsyncSession.execute() is used for 2.0 style ORM execution
# (same as the synchronous API).
result = await session.scalars(stmt)
# result is a buffered Result object.
for disc_golfer in result:
ic(disc_golfer)
# for streaming ORM results, AsyncSession.stream() may be used.
result = await session.stream(stmt)
# result is a streaming AsyncResult object.
async for disc_golfer in result.scalars():
ic(disc_golfer)
result = await session.scalars(select(DiscGolfer).order_by(DiscGolfer.id))
ic(result)
ic(result.first())
await session.commit()


async def run():
ic()
engine = create_async_engine(
"sqlite+aiosqlite:///scraper/src/disc_golfers.db",
echo=True,
)

async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all)

async_session = async_sessionmaker(engine, expire_on_commit=False)
await insert_objects(async_session)
await engine.dispose()


if __name__ == "__main__":
asyncio.run(run())

0 comments on commit a4fc9af

Please sign in to comment.