Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Commit 7773ffe

Browse files
holtskinnerpartheagcf-owl-bot[bot]
authored
refactor: Change read-only properties to @cached_property for readability and speed improvements (#210)
* refactor: Change read-only properties to `@cached_property` for readability --------- Co-authored-by: Anthonios Partheniou <partheniou@google.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 67be44a commit 7773ffe

File tree

2 files changed

+91
-204
lines changed

2 files changed

+91
-204
lines changed

google/cloud/documentai_toolbox/wrappers/document.py

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import copy
1919
import dataclasses
20+
from functools import cached_property
2021
import glob
2122
import os
2223
import re
@@ -389,29 +390,17 @@ class Document:
389390
gcs_uri: Optional[str] = dataclasses.field(default=None, repr=False)
390391
gcs_input_uri: Optional[str] = dataclasses.field(default=None, repr=False)
391392

392-
_pages: Optional[List[Page]] = dataclasses.field(
393-
init=False, repr=False, default=None
394-
)
395-
_entities: List[Entity] = dataclasses.field(init=False, repr=False, default=None)
396-
_text: str = dataclasses.field(init=False, repr=False, default=None)
397-
398-
@property
393+
@cached_property
399394
def pages(self):
400-
if self._pages is None:
401-
self._pages = _pages_from_shards(shards=self.shards)
402-
return self._pages
395+
return _pages_from_shards(shards=self.shards)
403396

404-
@property
397+
@cached_property
405398
def entities(self):
406-
if self._entities is None:
407-
self._entities = _entities_from_shards(shards=self.shards)
408-
return self._entities
399+
return _entities_from_shards(shards=self.shards)
409400

410-
@property
401+
@cached_property
411402
def text(self):
412-
if self._text is None:
413-
self._text = "".join(shard.text for shard in self.shards)
414-
return self._text
403+
return "".join(shard.text for shard in self.shards)
415404

416405
@classmethod
417406
def from_document_path(

0 commit comments

Comments
 (0)