Skip to content

Commit

Permalink
style: type hints
Browse files Browse the repository at this point in the history
  • Loading branch information
shengchenyang committed Apr 25, 2024
1 parent cef8048 commit 3e076a5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
2 changes: 1 addition & 1 deletion ayugespidertools/scraper/pipelines/mysql/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def process_item(self, item: Any, spider: "AyuSpider") -> Any:
self.insert_item(alter_item)
return item

def insert_item(self, alter_item: "AlterItem"):
def insert_item(self, alter_item: "AlterItem") -> None:
"""通用插入数据
Args:
Expand Down
10 changes: 6 additions & 4 deletions ayugespidertools/scraper/pipelines/mysql/twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
]

if TYPE_CHECKING:
from twisted.python.failure import Failure

from ayugespidertools.common.typevars import MysqlConf, slogT
from ayugespidertools.spiders import AyuSpider

Expand Down Expand Up @@ -40,10 +42,10 @@ def open_spider(self, spider: "AyuSpider") -> None:
query = self.dbpool.runInteraction(self.db_create)
query.addErrback(self.db_create_err)

def db_create(self, cursor):
def db_create(self, cursor: Any) -> None:
pass

def db_create_err(self, failure):
def db_create_err(self, failure: "Failure") -> None:
self.slog.error(f"创建数据表失败: {failure}")

def process_item(self, item: Any, spider: "AyuSpider") -> Any:
Expand All @@ -52,7 +54,7 @@ def process_item(self, item: Any, spider: "AyuSpider") -> Any:
query.addErrback(self.handle_error, item)
return item

def db_insert(self, cursor, item):
def db_insert(self, cursor: Any, item: Any) -> Any:
alter_item = ReuseOperation.reshape_item(item)
if not (new_item := alter_item.new_item):
return
Expand Down Expand Up @@ -84,5 +86,5 @@ def db_insert(self, cursor, item):
return self.db_insert(cursor, item)
return item

def handle_error(self, failure, item):
def handle_error(self, failure: "Failure", item: Any) -> None:
self.slog.error(f"插入数据失败:{failure}, item: {item}")
9 changes: 5 additions & 4 deletions ayugespidertools/scraper/pipelines/oracle/twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

if TYPE_CHECKING:
from oracledb.connection import Connection
from twisted.python.failure import Failure

from ayugespidertools.common.typevars import OracleConf, slogT
from ayugespidertools.spiders import AyuSpider
Expand Down Expand Up @@ -42,10 +43,10 @@ def open_spider(self, spider: "AyuSpider") -> None:
query = self.dbpool.runInteraction(self.db_create)
query.addErrback(self.db_create_err)

def db_create(self, cursor):
def db_create(self, cursor: Any) -> None:
pass

def db_create_err(self, failure):
def db_create_err(self, failure: "Failure") -> None:
self.slog.error(f"创建数据表失败: {failure}")

def process_item(self, item: Any, spider: "AyuSpider") -> Any:
Expand All @@ -54,7 +55,7 @@ def process_item(self, item: Any, spider: "AyuSpider") -> Any:
query.addErrback(self.handle_error, item)
return item

def db_insert(self, cursor, item):
def db_insert(self, cursor: Any, item: Any) -> Any:
alter_item = ReuseOperation.reshape_item(item)
if not (new_item := alter_item.new_item):
return
Expand All @@ -63,5 +64,5 @@ def db_insert(self, cursor, item):
cursor.execute(sql, tuple(new_item.values()))
return item

def handle_error(self, failure, item):
def handle_error(self, failure: "Failure", item: Any) -> None:
self.slog.error(f"插入数据失败:{failure}, item: {item}")
10 changes: 6 additions & 4 deletions ayugespidertools/scraper/pipelines/postgres/twisted.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
__all__ = ["AyuTwistedPostgresPipeline"]

if TYPE_CHECKING:
from twisted.python.failure import Failure

from ayugespidertools.common.typevars import PostgreSQLConf, slogT
from ayugespidertools.spiders import AyuSpider

Expand Down Expand Up @@ -40,10 +42,10 @@ def open_spider(self, spider: "AyuSpider") -> None:
query = self.dbpool.runInteraction(self.db_create)
query.addErrback(self.db_create_err)

def db_create(self, cursor):
def db_create(self, cursor: Any) -> None:
pass

def db_create_err(self, failure):
def db_create_err(self, failure: "Failure") -> None:
self.slog.error(f"创建数据表失败: {failure}")

def process_item(self, item: Any, spider: "AyuSpider") -> Any:
Expand All @@ -52,7 +54,7 @@ def process_item(self, item: Any, spider: "AyuSpider") -> Any:
query.addErrback(self.handle_error, item)
return item

def db_insert(self, cursor, item):
def db_insert(self, cursor: Any, item: Any) -> Any:
alter_item = ReuseOperation.reshape_item(item)
if not (new_item := alter_item.new_item):
return
Expand Down Expand Up @@ -81,5 +83,5 @@ def db_insert(self, cursor, item):
return self.db_insert(cursor, item)
return item

def handle_error(self, failure, item):
def handle_error(self, failure: "Failure", item: Any) -> None:
self.slog.error(f"插入数据失败:{failure}, item: {item}")

0 comments on commit 3e076a5

Please sign in to comment.