Skip to content
This repository was archived by the owner on Apr 2, 2025. It is now read-only.

Commit 472fa1e

Browse files
committed
Typing fixes in core.py
Improve python compatability
1 parent dee507b commit 472fa1e

File tree

1 file changed

+28
-12
lines changed

1 file changed

+28
-12
lines changed

camelot/core.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
import math
66
import os
77
import sqlite3
8+
import sys
89
import tempfile
910
import zipfile
1011
from operator import itemgetter
12+
from typing import Any
1113
from typing import Iterable
1214
from typing import Iterator
1315

1416
import cv2
1517
import pandas as pd
1618

19+
20+
if sys.version_info >= (3, 11):
21+
from typing import TypedDict # pylint: disable=no-name-in-module
22+
from typing import Unpack
23+
else:
24+
from typing_extensions import TypedDict, Unpack
25+
1726
from .backends import ImageConversionBackend
1827
from .utils import build_file_path_in_temp_dir
1928
from .utils import compute_whitespace
@@ -783,6 +792,13 @@ def to_sqlite(self, path, **kwargs):
783792
conn.close()
784793

785794

795+
class Kw(TypedDict):
796+
path: os.PathLike[Any] | str
797+
dirname: str
798+
root: str
799+
ext: str
800+
801+
786802
class TableList:
787803
"""Defines a list of camelot.core.Table objects.
788804
@@ -804,7 +820,7 @@ def __repr__(self): # noqa D105
804820
def __len__(self): # noqa D105
805821
return len(self._tables)
806822

807-
def __getitem__(self, idx) -> Table: # noqa D105
823+
def __getitem__(self, idx): # noqa D105
808824
return self._tables[idx]
809825

810826
def __iter__(self) -> Iterator[Table]: # noqa D105
@@ -822,29 +838,29 @@ def n(self) -> int:
822838
"""The number of tables in the list."""
823839
return len(self)
824840

825-
def _write_file(self, f=None, **kwargs) -> None:
826-
dirname = kwargs.get("dirname")
827-
root = kwargs.get("root")
828-
ext = kwargs.get("ext")
841+
def _write_file(self, f=None, **kwargs: Unpack[Kw]) -> None:
842+
dirname = kwargs["dirname"]
843+
root = kwargs["root"]
844+
ext = kwargs["ext"]
829845
for table in self._tables:
830846
filename = f"{root}-page-{table.page}-table-{table.order}{ext}"
831847
filepath = os.path.join(dirname, filename)
832848
to_format = self._format_func(table, f)
833849
to_format(filepath)
834850

835-
def _compress_dir(self, **kwargs) -> None:
836-
path = kwargs.get("path")
837-
dirname = kwargs.get("dirname")
838-
root = kwargs.get("root")
839-
ext = kwargs.get("ext")
851+
def _compress_dir(self, **kwargs: Unpack[Kw]) -> None:
852+
path = kwargs["path"]
853+
dirname = kwargs["dirname"]
854+
root = kwargs["root"]
855+
ext = kwargs["ext"]
840856
zipname = os.path.join(os.path.dirname(path), root) + ".zip"
841857
with zipfile.ZipFile(zipname, "w", allowZip64=True) as z:
842858
for table in self._tables:
843859
filename = f"{root}-page-{table.page}-table-{table.order}{ext}"
844860
filepath = os.path.join(dirname, filename)
845861
z.write(filepath, os.path.basename(filepath))
846862

847-
def export(self, path, f="csv", compress=False):
863+
def export(self, path: str, f="csv", compress=False):
848864
"""Export the list of tables to specified file format.
849865
850866
Parameters
@@ -863,7 +879,7 @@ def export(self, path, f="csv", compress=False):
863879
if compress:
864880
dirname = tempfile.mkdtemp()
865881

866-
kwargs = {"path": path, "dirname": dirname, "root": root, "ext": ext}
882+
kwargs: Kw = {"path": path, "dirname": dirname, "root": root, "ext": ext}
867883

868884
if f in ["csv", "html", "json", "markdown"]:
869885
self._write_file(f=f, **kwargs)

0 commit comments

Comments
 (0)