Skip to content

lucas-FP/pengen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

'Review a concise docstring for the class named PDFSlicerConfig.\nYou must output only the text, with no triple quotes, no markdown fences, no indentation and no class definition line. You should use the surrounding code to better understand and correctly document the class.\n\nStyle preferences:\n\n- Always write in Google style.\n- Always add "Defaults to x", at the end for values with defaults.\n\n\n\nInput for class Rectangle (Python source snippet):\n\npython\nclass Rectangle:\n """\n Represents a rectangle in 2-D space\n\n Attributes:\n width (float): The rectangle\'s width.\n height (float): The rectangle\'s height.\n \n Methods:\n area (float): Return the area of the rectangle.\n """\n\n def __init__(self, width: float, height: float):\n self.width = width\n self.height = height\n\n def area(self) -> float:\n return self.width * self.height\n\n\nExpected Output:\n\n{\n "should_update": false,\n "docstring_review": "Docstring ok",\n "new_docstring": null\n}\n\nInput for class Person (Python source snippet):\n\npython\nclass Person:\n """Simple repretation of a person."""\n\ndef __init__(self, name: str, age: int):\n self.name = name\n self.age = age\n\n def greet(self) -> str:\n return f"Hello, my name is {self.name}."\n\n\n\nExpected Output:\n\n{\n "should_update": true,\n "docstring_review": "Docstring typo, missing attributes and arguments",\n "new_docstring": "Represents a person with a name and age.\n\nClass Attributes:\n species (str): Scientific name for all humans.\n\nInstance Attributes:\n name (str): The person's name.\n age (int): The person's age in years.\n\nMethods:\n greet() -> str: Return a friendly greeting that includes the person's name."\n}\n\n\n\n\n\n\nThis is the full code:\n\npython\nfrom dataclasses import dataclass\nimport PyPDF2\nimport os\nfrom pydantic import BaseModel\nimport yaml\n\n\nclass PDFSlicerConfigEntry(BaseModel):\n """\n Configuration for a single PDF file and page range.\n \n Attributes:\n input_file (str): The path to the input PDF file.\n splits (tuple[int, int] | None): A tuple representing the start and end page numbers for slicing. Defaults to None.\n """\n\n input_file: str\n splits: tuple[int, int] | None\n\n\nclass PDFSlicerConfig(BaseModel):\n """\n Represents the configuration for slicing PDFs.\n \n Attributes:\n output_file (str): The path to the output PDF file.\n splits (list[PDFSlicerConfigEntry]): A list of configurations for each PDF file and page range. Defaults to an empty list.\n """\n output_file: str\n splits: list[PDFSlicerConfigEntry]\n\n\ndef load_config(config_path):\n """Load configuration from a YAML file."""\n if not os.path.exists(config_path):\n print(f"Config not found at {config_path}")\n return None\n\n with open(config_path, "r") as f:\n data = yaml.safe_load(f)\n if isinstance(data, dict):\n data = [data]\n\n assert isinstance(data, list), "Config must be a list or a dict"\n configs = []\n for raw_config in data:\n config = PDFSlicerConfig(**raw_config)\n configs.append(config)\n return configs\n\n\ndef create_sliced_pdf(config):\n """Create a merged PDF from the specified files and page ranges."""\n\n writer = PyPDF2.PdfWriter()\n\n for entry in config.splits:\n if not os.path.exists(entry.input_file):\n print(f"Warning: File not found: {entry.input_file}")\n continue\n\n reader = PyPDF2.PdfReader(entry.input_file)\n num_pages = len(reader.pages)\n\n page_range = entry.splits\n\n if page_range is None:\n selected_pages = range(num_pages)\n else:\n start, end = page_range\n if start < 0 or end >= num_pages:\n print(\n f"Warning: Page range {start}-{end} out of bounds for {entry.input_file}"\n )\n continue\n selected_pages = range(start, end + 1)\n\n for i in selected_pages:\n writer.add_page(reader.pages[i])\n\n with open(config.output_file, "wb") as output_file:\n writer.write(output_file)\n\n\ndef main():\n config_path = "config.yaml"\n configs = load_config(config_path)\n for config in configs:\n if config is None:\n continue\n print(f"Creating sliced PDF: {config.output_file}")\n create_sliced_pdf(config_path)\n print(f"Sliced PDF created at: {config.output_file}")\n\n\nif __name__ == "__main__":\n main()\n\n\nThis is the relevant snippet:\n\npython\nfrom dataclasses import dataclass\nimport PyPDF2\nimport os\nfrom pydantic import BaseModel\nimport yaml\n\n\nclass PDFSlicerConfigEntry(BaseModel):\n """\n Configuration for a single PDF file and page range.\n \n Attributes:\n input_file (str): The path to the input PDF file.\n splits (tuple[int, int] | None): A tuple representing the start and end page numbers for slicing. Defaults to None.\n """\n\n input_file: str\n splits: tuple[int, int] | None\n\n\nclass PDFSlicerConfig(BaseModel):\n """\n Represents the configuration for slicing PDFs.\n \n Attributes:\n output_file (str): The path to the output PDF file.\n splits (list[PDFSlicerConfigEntry]): A list of configurations for each PDF file and page range. Defaults to an empty list.\n """\n output_file: str\n splits: list[PDFSlicerConfigEntry]\n\n\ndef load_config(config_path):\n """Load configuration from a YAML file."""\n if not os.path.exists(config_path):\n print(f"Config not found at {config_path}")\n return None\n\n with open(config_path, "r") as f:\n data = yaml.safe_load(f)\n if isinstance(data, dict):\n data = [data]\n\n assert isinstance(data, list), "Config must be a list or a dict"\n configs = []\n for raw_config in data:\n config = PDFSlicerConfig(**raw_config)\n configs.append(config)\n return configs\n\n\ndef create_sliced_pdf(config):\n """Create a merged PDF from the specified files and page ranges."""\n\n writer = PyPDF2.PdfWriter()\n\n for entry in config.splits:\n if not os.path.exists(entry.input_file):\n print(f"Warning: File not found: {entry.input_file}")\n continue\n\n reader = PyPDF2.PdfReader(entry.input_file)\n num_pages = len(reader.pages)\n\n page_range = entry.splits\n\n if page_range is None:\n selected_pages = range(num_pages)\n else:\n start, end = page_range\n if start < 0 or end >= num_pages:\n print(\n f"Warning: Page range {start}-{end} out of bounds for {entry.input_file}"\n )\n continue\n selected_pages = range(start, end + 1)\n\n for i in selected_pages:\n writer.add_page(reader.pages[i])\n\n with open(config.output_file, "wb") as output_file:\n writer.write(output_file)\n\n\ndef main():\n config_path = "config.yaml"\n configs = load_config(config_path)\n for config in configs:\n if config is None:\n continue\n print(f"Creating sliced PDF: {config.output_file}")\n create_sliced_pdf(config_path)\n print(f"Sliced PDF created at: {config.output_file}")\n\n\nif __name__ == "__main__":\n main()\n\n\n\n\nOutput for class PDFSlicerConfig:\n'

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages