Skip to content

Commit

Permalink
changed typing import
Browse files Browse the repository at this point in the history
  • Loading branch information
toluaina committed Dec 22, 2023
1 parent 400e608 commit 765307d
Show file tree
Hide file tree
Showing 30 changed files with 344 additions and 338 deletions.
43 changes: 23 additions & 20 deletions bin/parallel_sync
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ import multiprocessing
import os
import re
import sys
import typing as t
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
from dataclasses import dataclass
from queue import Queue
from threading import Thread
from typing import Generator, Optional, Union

import click
import sqlalchemy as sa
Expand Down Expand Up @@ -134,13 +134,13 @@ class Task:
@timeit
def fetch_tasks(
doc: dict,
block_size: Optional[int] = None,
) -> Generator:
block_size: t.Optional[int] = None,
) -> t.Generator:
block_size = block_size or BLOCK_SIZE
pages: dict = {}
sync: Sync = Sync(doc)
page: Optional[int] = None
row: Optional[int] = None
page: t.Optional[int] = None
row: t.Optional[int] = None
name: str = re.sub(
"[^0-9a-zA-Z_]+", "", f"{sync.database.lower()}_{sync.index}"
)
Expand Down Expand Up @@ -213,7 +213,10 @@ def fetch_tasks(

@timeit
def synchronous(
tasks: Generator, doc: dict, verbose: bool = False, validate: bool = False
tasks: t.Generator,
doc: dict,
verbose: bool = False,
validate: bool = False,
) -> None:
sys.stdout.write("Synchronous\n")
sync: Sync = Sync(doc, verbose=verbose, validate=validate)
Expand All @@ -231,9 +234,9 @@ def synchronous(

@timeit
def multithreaded(
tasks: Generator,
tasks: t.Generator,
doc: dict,
nprocs: Optional[int] = None,
nprocs: t.Optional[int] = None,
verbose: bool = False,
validate: bool = False,
) -> None:
Expand Down Expand Up @@ -274,9 +277,9 @@ def multithreaded(

@timeit
def multiprocess(
tasks: Generator,
tasks: t.Generator,
doc: dict,
nprocs: Optional[int] = None,
nprocs: t.Optional[int] = None,
verbose: bool = False,
validate: bool = False,
) -> None:
Expand All @@ -292,9 +295,9 @@ def multiprocess(

@timeit
def multithreaded_async(
tasks: Generator,
tasks: t.Generator,
doc: dict,
nprocs: Optional[int] = None,
nprocs: t.Optional[int] = None,
verbose: bool = False,
validate: bool = False,
) -> None:
Expand All @@ -309,9 +312,9 @@ def multithreaded_async(

@timeit
def multiprocess_async(
tasks: Generator,
tasks: t.Generator,
doc: dict,
nprocs: Optional[int] = None,
nprocs: t.Optional[int] = None,
verbose: bool = False,
validate: bool = False,
) -> None:
Expand All @@ -328,13 +331,13 @@ def multiprocess_async(


async def run_tasks(
executor: Union[ThreadPoolExecutor, ProcessPoolExecutor],
tasks: Generator,
executor: t.Union[ThreadPoolExecutor, ProcessPoolExecutor],
tasks: t.Generator,
doc: dict,
verbose: bool = False,
validate: bool = False,
) -> None:
sync: Optional[Sync] = None
sync: t.Optional[Sync] = None
if isinstance(executor, ThreadPoolExecutor):
# threads can share a common Sync object
sync = Sync(doc, verbose=verbose, validate=validate)
Expand All @@ -354,8 +357,8 @@ async def run_tasks(

def run_task(
task: dict,
sync: Optional[Sync] = None,
doc: Optional[dict] = None,
sync: t.Optional[Sync] = None,
doc: t.Optional[dict] = None,
verbose: bool = False,
validate: bool = False,
) -> int:
Expand Down Expand Up @@ -427,7 +430,7 @@ def main(config, nprocs, mode, verbose):
config: str = get_config(config)

for document in config_loader(config):
tasks: Generator = fetch_tasks(document)
tasks: t.Generator = fetch_tasks(document)
if mode == "synchronous":
synchronous(tasks, document, verbose=verbose)
elif mode == "multithreaded":
Expand Down
12 changes: 6 additions & 6 deletions examples/airbnb/data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typing as t
from datetime import datetime, timedelta
from typing import List

import click
from schema import Booking, City, Country, Host, Place, Review, User
Expand All @@ -26,7 +26,7 @@ def main(config):
Session = sessionmaker(bind=engine, autoflush=True)
session = Session()

users: List[User] = [
users: t.List[User] = [
User(email="[email protected]"),
User(email="[email protected]"),
User(email="[email protected]"),
Expand All @@ -35,7 +35,7 @@ def main(config):
User(email="[email protected]"),
]

hosts: List[Host] = [
hosts: t.List[Host] = [
Host(email="[email protected]"),
Host(email="[email protected]"),
Host(email="[email protected]"),
Expand All @@ -45,7 +45,7 @@ def main(config):
Host(email="[email protected]"),
]

cities: List[City] = [
cities: t.List[City] = [
City(
name="Manila",
country=Country(
Expand Down Expand Up @@ -90,7 +90,7 @@ def main(config):
),
]

places: List[Place] = [
places: t.List[Place] = [
Place(
host=hosts[0],
city=cities[0],
Expand Down Expand Up @@ -123,7 +123,7 @@ def main(config):
),
]

reviews: List[Review] = [
reviews: t.List[Review] = [
Review(
booking=Booking(
user=users[0],
Expand Down
4 changes: 2 additions & 2 deletions examples/book/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typing as t
from random import choice
from typing import Set

import click
import sqlalchemy as sa
Expand All @@ -24,7 +24,7 @@

def insert_op(session: sessionmaker, model, nsize: int) -> None:
faker: Faker = Faker()
rows: Set = set()
rows: t.Set = set()
for _ in range(nsize):
kwargs = {}
for column in model.__table__.columns:
Expand Down
30 changes: 15 additions & 15 deletions examples/book/data.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import datetime
import random
from typing import Dict, List
import typing as t

import click
from faker import Faker
Expand Down Expand Up @@ -51,14 +51,14 @@ def main(config, nsize):
session = Session()

# Bootstrap
continents: Dict[str, Continent] = {
continents: t.Dict[str, Continent] = {
"Europe": Continent(name="Europe"),
"North America": Continent(name="North America"),
}
with subtransactions(session):
session.add_all(continents.values())

countries: Dict[str, Country] = {
countries: t.Dict[str, Country] = {
"United Kingdom": Country(
name="United Kingdom",
continent=continents["Europe"],
Expand All @@ -75,7 +75,7 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(countries.values())

cities: Dict[str, City] = {
cities: t.Dict[str, City] = {
"London": City(
name="London", country=countries["United Kingdom"]
),
Expand All @@ -87,7 +87,7 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(cities.values())

publishers: Dict[str, Publisher] = {
publishers: t.Dict[str, Publisher] = {
"Oxford Press": Publisher(name="Oxford Press", is_active=True),
"Penguin Books": Publisher(
name="Penguin Books", is_active=False
Expand All @@ -102,7 +102,7 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(publishers.values())

authors: Dict[str, Author] = {
authors: t.Dict[str, Author] = {
"Stephen King": Author(
name="Stephen King",
date_of_birth=datetime.datetime(1947, 9, 21),
Expand Down Expand Up @@ -132,7 +132,7 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(authors.values())

subjects: Dict[str, Subject] = {
subjects: t.Dict[str, Subject] = {
"Literature": Subject(name="Literature"),
"Poetry": Subject(name="Poetry"),
"Romance": Subject(name="Romance"),
Expand All @@ -144,7 +144,7 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(subjects.values())

languages: Dict[str, Language] = {
languages: t.Dict[str, Language] = {
"en-GB": Language(code="en-GB"),
"en-US": Language(code="en-US"),
"de-DE": Language(code="de-DE"),
Expand All @@ -157,14 +157,14 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(languages.values())

shelves: Dict[str, Shelf] = {
shelves: t.Dict[str, Shelf] = {
"Shelf A": Shelf(shelf="Shelf A"),
"Shelf B": Shelf(shelf="Shelf B"),
}
with subtransactions(session):
session.add_all(shelves.values())

books: Dict[str, Book] = {
books: t.Dict[str, Book] = {
"001": Book(
isbn="001",
title="It",
Expand Down Expand Up @@ -294,7 +294,7 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(books.values())

ratings: List[Rating] = [
ratings: t.List[Rating] = [
Rating(value=1.1, book=books["001"]),
Rating(value=2.1, book=books["002"]),
Rating(value=3.1, book=books["003"]),
Expand All @@ -307,7 +307,7 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(ratings)

book_authors: List[BookAuthor] = [
book_authors: t.List[BookAuthor] = [
BookAuthor(book=books["001"], author=authors["Stephen King"]),
BookAuthor(book=books["002"], author=authors["Stephen King"]),
BookAuthor(book=books["003"], author=authors["J. K. Rowling"]),
Expand All @@ -326,7 +326,7 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(book_authors)

book_subjects: List[BookSubject] = [
book_subjects: t.List[BookSubject] = [
BookSubject(book=books["001"], subject=subjects["Literature"]),
BookSubject(book=books["002"], subject=subjects["Literature"]),
BookSubject(book=books["003"], subject=subjects["Poetry"]),
Expand All @@ -345,7 +345,7 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(book_subjects)

book_languages: List[BookLanguage] = [
book_languages: t.List[BookLanguage] = [
BookLanguage(book=books["001"], language=languages["en-GB"]),
BookLanguage(book=books["002"], language=languages["en-GB"]),
BookLanguage(book=books["003"], language=languages["en-GB"]),
Expand All @@ -366,7 +366,7 @@ def main(config, nsize):
with subtransactions(session):
session.add_all(book_languages)

book_shelves: List[BookShelf] = [
book_shelves: t.List[BookShelf] = [
BookShelf(book=books["001"], shelf=shelves["Shelf A"]),
BookShelf(book=books["001"], shelf=shelves["Shelf B"]),
BookShelf(book=books["002"], shelf=shelves["Shelf A"]),
Expand Down
4 changes: 2 additions & 2 deletions examples/book_view/benchmark.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import typing as t
from random import choice
from typing import Set

import click
import sqlalchemy as sa
Expand All @@ -21,7 +21,7 @@

def insert_op(session: sessionmaker, model, nsize: int) -> None:
faker: Faker = Faker()
rows: Set = set()
rows: t.Set = set()
for _ in range(nsize):
kwargs = {}
for column in model.__table__.columns:
Expand Down
4 changes: 2 additions & 2 deletions examples/node/data.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
import typing as t

import click
from schema import Node
Expand All @@ -24,7 +24,7 @@ def main(config):
with pg_engine(database) as engine:
Session = sessionmaker(bind=engine, autoflush=True)
session = Session()
nodes: List[Node] = [
nodes: t.List[Node] = [
Node(id=1, name="Node A"),
Node(id=2, name="Node B"),
Node(id=3, name="Node C"),
Expand Down
1 change: 0 additions & 1 deletion examples/social/data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import click
import sqlalchemy as sa
from schema import Comment, Post, PostComment, Tag, User, UserPost, UserTag
from sqlalchemy.orm import sessionmaker

Expand Down
5 changes: 0 additions & 5 deletions examples/through/data.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import datetime
import random
from typing import Dict, List

import click
from faker import Faker
from schema import Customer, CustomerGroup, Group
from sqlalchemy.orm import sessionmaker

Expand Down
Loading

0 comments on commit 765307d

Please sign in to comment.