Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor generate_qmd.py #12

Open
wesslen opened this issue Jan 2, 2024 · 0 comments
Open

refactor generate_qmd.py #12

wesslen opened this issue Jan 2, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@wesslen
Copy link
Owner

wesslen commented Jan 2, 2024

Here are the main functions of the code:

  1. convert_to_folder_name: This function takes a string as input and converts it to a folder name format by replacing spaces, slashes, question marks, colons, commas, and hyphens with underscores. It uses the translate method of strings with a translation table to perform the replacement.

  2. create_qmd_file: This function takes an example dictionary and an output folder path as input. It extracts relevant information from the example dictionary, such as the title, publish date, and image URL. It also creates a folder name based on the title using convert_to_folder_name. It then creates a QMD file path by combining the output folder path, folder name, and current date. It checks if the file already exists and returns if it does. Otherwise, it uses Jinja2 templating to render a QMD file content with the example data. It creates the output sub-folder if it doesn't exist and writes the rendered content to the file.

  3. generate_qmd: This is the main command of the script. It takes an input JSONL file path and an output folder path as input. It opens the JSONL file and iterates over each line. It loads the line as a JSON object and calls the create_qmd_file function to generate a QMD file for each example.

The code uses the typer package for command-line interface (CLI) parsing. It defines a CLI command generate_qmd that accepts the input JSONL file and output folder as arguments.

Suggestions for improving the code for programming best practices:

  1. Use type hints: Add type hints to function and variable declarations to improve code readability and maintainability.

  2. Use docstrings: Add docstrings to functions to provide a description of their purpose and usage.

  3. Handle exceptions: Add exception handling to catch and handle any potential errors that may occur during file operations, such as opening, reading, or writing files.

  4. Separate concerns: Consider breaking down the create_qmd_file function into smaller, more focused functions to improve code organization and readability.

  5. Use pathlib functions: Instead of manually constructing file paths using string concatenation, use the pathlib module's functions to manipulate file paths. For example, use Path(output_folder) / folder_name / file_name to create the file path.

  6. Use f-strings: Instead of concatenating strings using the + operator, use f-strings to improve string formatting and readability. For example, use f"File saved: {file_path}" instead of "File saved: " + file_path.

  7. Use a logger: Instead of printing messages directly to the console, consider using a logging library like logging to log messages with different levels of severity.

  8. Error handling for existing directories: Add error handling to check if the output folder already exist s and whether it is a valid directory before writing files to it.

  9. Unit tests: Add unit tests to verify the correctness of the functions and handle edge cases.

@wesslen wesslen added the enhancement New feature or request label Jan 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant