| 2818 | |
| 2819 | |
| 2820 | class Document: |
| 2821 | |
| 2822 | def __contains__(self, loc) -> bool: |
| 2823 | if type(loc) is int: |
| 2824 | if loc < self.page_count: |
| 2825 | return True |
| 2826 | return False |
| 2827 | if type(loc) not in (tuple, list) or len(loc) != 2: |
| 2828 | return False |
| 2829 | chapter, pno = loc |
| 2830 | if (0 |
| 2831 | or not isinstance(chapter, int) |
| 2832 | or chapter < 0 |
| 2833 | or chapter >= self.chapter_count |
| 2834 | ): |
| 2835 | return False |
| 2836 | if (0 |
| 2837 | or not isinstance(pno, int) |
| 2838 | or pno < 0 |
| 2839 | or pno >= self.chapter_page_count(chapter) |
| 2840 | ): |
| 2841 | return False |
| 2842 | return True |
| 2843 | |
| 2844 | def __delitem__(self, i)->None: |
| 2845 | if not self.is_pdf: |
| 2846 | raise ValueError("is no PDF") |
| 2847 | if type(i) is int: |
| 2848 | return self.delete_page(i) |
| 2849 | if type(i) in (list, tuple, range): |
| 2850 | return self.delete_pages(i) |
| 2851 | if type(i) is not slice: |
| 2852 | raise ValueError("bad argument type") |
| 2853 | pc = self.page_count |
| 2854 | start = i.start if i.start else 0 |
| 2855 | stop = i.stop if i.stop else pc |
| 2856 | step = i.step if i.step else 1 |
| 2857 | while start < 0: |
| 2858 | start += pc |
| 2859 | if start >= pc: |
| 2860 | raise ValueError("bad page number(s)") |
| 2861 | while stop < 0: |
| 2862 | stop += pc |
| 2863 | if stop > pc: |
| 2864 | raise ValueError("bad page number(s)") |
| 2865 | return self.delete_pages(range(start, stop, step)) |
| 2866 | |
| 2867 | def __enter__(self): |
| 2868 | return self |
| 2869 | |
| 2870 | def __exit__(self, *args): |
| 2871 | self.close() |
| 2872 | |
| 2873 | @typing.overload |
| 2874 | def __getitem__(self, i: int = 0) -> Page: |
| 2875 | ... |
| 2876 | |
| 2877 | if sys.version_info >= (3, 9): |
no outgoing calls
no test coverage detected
searching dependent graphs…