A fast, helpful, and open-source document parser
drasticaly reduce python import times
Logan Markewich203c75b2e6e8
diff --git a/packages/python/liteparse/__init__.py b/packages/python/liteparse/__init__.pyindex 00bafdb..5db7b10 100644--- a/packages/python/liteparse/__init__.py+++ b/packages/python/liteparse/__init__.py@@ -1,40 +1,65 @@-from importlib.metadata import PackageNotFoundError, version+"""LiteParse - fast local document parsing."""++# Re-exports are resolved lazily (PEP 562). `lit` runs cli.py, which imports+# liteparse._liteparse and so executes this module; eagerly pulling in .parser+# and .types dragged asyncio and importlib.metadata into every CLI invocation+# for ~45ms of startup that a parse never uses.+_LAZY = {+ "LiteParse": ".parser",+ "search_items": ".parser",+ "AnnotationRect": ".types",+ "DocumentAnnotation": ".types",+ "LayoutBlock": ".types",+ "LayoutCell": ".types",+ "FormField": ".types",+ "StructureTree": ".types",+ "StructureTreeElement": ".types",+ "ExtractedImage": ".types",+ "ImageRect": ".types",+ "LayoutComplexityStats": ".types",+ "LiteParseConfig": ".types",+ "PageComplexityStats": ".types",+ "PageError": ".types",+ "ParseResult": ".types",+ "ParseBatch": ".types",+ "DocumentMetadata": ".types",+ "XfaPacket": ".types",+ "ParsedPage": ".types",+ "TextItem": ".types",+ "WordBox": ".types",+ "ScreenshotRect": ".types",+ "ScreenshotResult": ".types",+ "ParseError": ".types",+ "ParseTimeoutError": ".types",+ "VectorGraphics": ".types",+ "VectorLine": ".types",+ "VectorShape": ".types",+}+++def __getattr__(name):+ if name == "__version__":+ from importlib.metadata import PackageNotFoundError, version++ try:+ value = version("liteparse")+ except PackageNotFoundError: # source tree without installed dist metadata+ value = "0.0.0+unknown"+ else:+ module = _LAZY.get(name)+ if module is None:+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")+ from importlib import import_module++ value = getattr(import_module(module, __name__), name)+ globals()[name] = value # cache: __getattr__ only fires on the first miss+ return value+++def __dir__():+ return sorted(set(globals()) | set(_LAZY) | {"__version__"})-from .parser import LiteParse, search_items-from .types import (- AnnotationRect,- DocumentAnnotation,- LayoutBlock,- LayoutCell,- FormField,- StructureTree,- StructureTreeElement,- ExtractedImage,- ImageRect,- LayoutComplexityStats,- LiteParseConfig,- PageComplexityStats,- PageError,- ParseResult,- ParseBatch,- DocumentMetadata,- XfaPacket,- ParsedPage,- TextItem,- WordBox,- ScreenshotRect,- ScreenshotResult,- ParseError,- ParseTimeoutError,- VectorGraphics,- VectorLine,- VectorShape,-)-try:- __version__ = version("liteparse")-except PackageNotFoundError: # source tree without installed dist metadata- __version__ = "0.0.0+unknown"__all__ = ["LiteParse","AnnotationRect",diff --git a/packages/python/liteparse/parser.py b/packages/python/liteparse/parser.pyindex c3e081d..709022d 100644--- a/packages/python/liteparse/parser.py+++ b/packages/python/liteparse/parser.py@@ -1,9 +1,19 @@"""LiteParse Python wrapper - native Rust bindings via PyO3."""-import asyncio-from concurrent.futures import Executorfrom pathlib import Path-from typing import Any, Dict, Iterator, List, Optional, Tuple, Union+from typing import (+ Any,+ Dict,+ Iterator,+ List,+ Optional,+ Tuple,+ Union,+ TYPE_CHECKING,+)++if TYPE_CHECKING:+ from concurrent.futures import Executorfrom liteparse._liteparse import LiteParse as _NativeLiteParsefrom liteparse._liteparse import search_items as _native_search_items@@ -745,7 +755,7 @@ class LiteParse:self,file_data: Union[str, Path, bytes],*,- executor: Optional[Executor] = None,+ executor: Optional["Executor"] = None,) -> ParseResult:"""Parse a document without blocking the running event loop.@@ -762,6 +772,9 @@ class LiteParse:The same exceptions as :meth:`parse`, re-raised on the awaitingside."""+ # Imported here, not at module scope: `asyncio` costs ~10ms+ import asyncio+loop = asyncio.get_running_loop()return await loop.run_in_executor(executor, self.parse, file_data)diff --git a/packages/python/tests/test_pool_e2e.py b/packages/python/tests/test_pool_e2e.pyindex 82d955a..bcbcb19 100644--- a/packages/python/tests/test_pool_e2e.py+++ b/packages/python/tests/test_pool_e2e.py@@ -96,7 +96,11 @@ def test_timeout_kills_worker_and_pool_recovers(sample_pdf):def test_timeout_names_the_file(tmp_path):slow_path = tmp_path / "rogue.pdf"slow_path.write_bytes(_grid_pdf(64000))- with LiteParse(**CFG, pool_size=1, parse_timeout=0.5) as pooled:+ # The doc parses in ~0.35s, so a 0.5s budget only ever tripped because+ # worker spawn + `import liteparse` were charged to it. Pick a timeout the+ # parse itself blows through, so the assertion does not depend on how slow+ # process startup happens to be on the machine running the suite.+ with LiteParse(**CFG, pool_size=1, parse_timeout=0.1) as pooled:with pytest.raises(ParseTimeoutError) as excinfo:pooled.parse(slow_path)assert excinfo.value.source == str(slow_path.absolute())