Skip to content

Commit

Permalink
MAINT: Inline PAGE_RANGE_HELP string (#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterOdin authored May 11, 2022
1 parent 29b1006 commit 2478309
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
29 changes: 11 additions & 18 deletions PyPDF2/pagerange.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@
# groups: 12 34 5 6 7 8


PAGE_RANGE_HELP = """Remember, page indices start with zero.
Page range expression examples:
: all pages. -1 last page.
22 just the 23rd page. :-1 all but the last page.
0:3 the first three pages. -2 second-to-last page.
:3 the first three pages. -2: last two pages.
5: from the sixth page onward. -3:-1 third & second to last.
The third, "stride" or "step" number is also recognized.
::2 0 2 4 ... to the end. 3:0:-1 3 2 1 but not 0.
1:10:2 1 3 5 7 9 2::-1 2 1 0.
::-1 all pages in reverse order.
"""


class PageRange:
"""
A slice-like representation of a range of page indices,
Expand All @@ -55,7 +41,17 @@ def __init__(self, arg: Union[slice, "PageRange", str]) -> None:
or a string like
"int", "[int]:[int]" or "[int]:[int]:[int]",
where the brackets indicate optional ints.
{page_range_help}
Remember, page indices start with zero.
Page range expression examples:
: all pages. -1 last page.
22 just the 23rd page. :-1 all but the last page.
0:3 the first three pages. -2 second-to-last page.
:3 the first three pages. -2: last two pages.
5: from the sixth page onward. -3:-1 third & second to last.
The third, "stride" or "step" number is also recognized.
::2 0 2 4 ... to the end. 3:0:-1 3 2 1 but not 0.
1:10:2 1 3 5 7 9 2::-1 2 1 0.
::-1 all pages in reverse order.
Note the difference between this notation and arguments to slice():
slice(3) means the first three pages;
PageRange("3") means the range of only the fourth page.
Expand All @@ -80,9 +76,6 @@ def __init__(self, arg: Union[slice, "PageRange", str]) -> None:
else:
self._slice = slice(*[int(g) if g else None for g in m.group(4, 6, 8)])

if __init__.__doc__: # see https://github.com/py-pdf/PyPDF2/issues/737
__init__.__doc__ = __init__.__doc__.format(page_range_help=PAGE_RANGE_HELP)

@staticmethod
def valid(input: Any) -> bool:
"""True if input is a valid initializer for a PageRange."""
Expand Down
8 changes: 0 additions & 8 deletions tests/test_pagerange.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,3 @@ def test_parse_filename_page_ranges_err():
assert (
exc.value.args[0] == "The first argument must be a filename, not a page range."
)


def test_page_range_help():
from PyPDF2.pagerange import PAGE_RANGE_HELP

assert len(PAGE_RANGE_HELP) > 20
assert "0:3" in PAGE_RANGE_HELP
assert PAGE_RANGE_HELP.endswith("\n")

0 comments on commit 2478309

Please sign in to comment.