Skip to content

Commit

Permalink
STY: Use more tuples and list/dict comprehensions (#976)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinThoma authored Jun 12, 2022
1 parent df8f121 commit 55f7c7b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 23 deletions.
16 changes: 8 additions & 8 deletions PyPDF2/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,13 @@ def _merge_page(
def _expand_mediabox(
self, page2: "PageObject", ctm: Optional[CompressedTransformationMatrix]
) -> None:
corners1 = [
corners1 = (
self.mediabox.left.as_numeric(),
self.mediabox.bottom.as_numeric(),
self.mediabox.right.as_numeric(),
self.mediabox.top.as_numeric(),
]
corners2 = [
)
corners2 = (
page2.mediabox.left.as_numeric(),
page2.mediabox.bottom.as_numeric(),
page2.mediabox.left.as_numeric(),
Expand All @@ -563,17 +563,17 @@ def _expand_mediabox(
page2.mediabox.top.as_numeric(),
page2.mediabox.right.as_numeric(),
page2.mediabox.bottom.as_numeric(),
]
)
if ctm is not None:
ctm = tuple(float(x) for x in ctm) # type: ignore[assignment]
new_x = [
new_x = tuple(
ctm[0] * corners2[i] + ctm[2] * corners2[i + 1] + ctm[4]
for i in range(0, 8, 2)
]
new_y = [
)
new_y = tuple(
ctm[1] * corners2[i] + ctm[3] * corners2[i + 1] + ctm[5]
for i in range(0, 8, 2)
]
)
else:
new_x = corners2[0:8:2]
new_y = corners2[1:8:2]
Expand Down
9 changes: 4 additions & 5 deletions PyPDF2/_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ def _check_kids(
self.get_fields(kid.get_object(), retval, fileobj)

def _write_field(self, fileobj: Any, field: Any, field_attributes: Any) -> None:
order = ["/TM", "/T", "/FT", PA.PARENT, "/TU", "/Ff", "/V", "/DV"]
order = ("/TM", "/T", "/FT", PA.PARENT, "/TU", "/Ff", "/V", "/DV")
for attr in order:
attr_name = field_attributes[attr]
try:
Expand Down Expand Up @@ -701,10 +701,9 @@ def _get_page_number_by_indirect(
) -> int:
"""Generate _page_id2num"""
if self._page_id2num is None:
id2num = {}
for i, x in enumerate(self.pages):
id2num[x.indirect_ref.idnum] = i # type: ignore
self._page_id2num = id2num
self._page_id2num = {
x.indirect_ref.idnum: i for i, x in enumerate(self.pages) # type: ignore
}

if indirect_ref is None or isinstance(indirect_ref, NullObject):
return -1
Expand Down
12 changes: 6 additions & 6 deletions PyPDF2/_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ def remove_images(self, ignore_byte_string_object: bool = False) -> None:
"""
pg_dict = cast(DictionaryObject, self.get_object(self._pages))
pages = cast(ArrayObject, pg_dict[PA.KIDS])
jump_operators = [
jump_operators = (
b_("cm"),
b_("w"),
b_("J"),
Expand All @@ -1235,7 +1235,7 @@ def remove_images(self, ignore_byte_string_object: bool = False) -> None:
b_("B"),
b_("Do"),
b_("sh"),
]
)
for j in range(len(pages)):
page = pages[j]
page_ref = cast(DictionaryObject, self.get_object(page))
Expand Down Expand Up @@ -1539,15 +1539,15 @@ def addLink( # pragma: no cover
deprecate_with_replacement("addLink", "add_link")
return self.add_link(pagenum, pagedest, rect, border, fit, *args)

_valid_layouts = [
_valid_layouts = (
"/NoLayout",
"/SinglePage",
"/OneColumn",
"/TwoColumnLeft",
"/TwoColumnRight",
"/TwoPageLeft",
"/TwoPageRight",
]
)

def _get_page_layout(self) -> Optional[LayoutType]:
try:
Expand Down Expand Up @@ -1656,14 +1656,14 @@ def pageLayout(self, layout: LayoutType) -> None: # pragma: no cover
deprecate_with_replacement("pageLayout", "page_layout")
self.page_layout = layout

_valid_modes = [
_valid_modes = (
"/UseNone",
"/UseOutlines",
"/UseThumbs",
"/FullScreen",
"/UseOC",
"/UseAttachments",
]
)

def _get_page_mode(self) -> Optional[PagemodeType]:
try:
Expand Down
4 changes: 2 additions & 2 deletions PyPDF2/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class CatalogDictionary:
NEEDS_RENDERING = "/NeedsRendering" # boolean, optional


PDF_KEYS = [
PDF_KEYS = (
PagesAttributes,
PageAttributes,
Ressources,
Expand All @@ -302,4 +302,4 @@ class CatalogDictionary:
Core,
TrailerKeys,
CatalogAttributes,
]
)
4 changes: 2 additions & 2 deletions PyPDF2/papersizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PaperSize:
C4 = Dimensions(649, 918)


_din_a = [
_din_a = (
PaperSize.A0,
PaperSize.A1,
PaperSize.A2,
Expand All @@ -45,4 +45,4 @@ class PaperSize:
PaperSize.A6,
PaperSize.A7,
PaperSize.A8,
]
)

0 comments on commit 55f7c7b

Please sign in to comment.