Skip to content

gptdialogues/codegen-showcase

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

codegen-showcase

A repository showcasing how to use ChatGPT to generate Python 3 code for various purposes.

Snippets

Below is a snippet I frequently use when instructing ChatGPT to write Python 3 code:

I would like to add some additional specifications below.
- Add a help option using `argparse`.
- Add docstrings and type hints.
- Add an option to specify the output filename. Use a default name by adding a specific suffix after the original basename.
- Give some candidates for the file name of the script.
- Ask "(y)es/(n)o" before overwriting an existing file (default: no). 
- Add the following shebang and an encoding declaration.
```
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
```
Also, please generate a test code for each function of the generated code using pytest.

Adding a Help Option

One useful feature that ChatGPT can add to scripts is a help option. This option allows users to view a description of the script's functionality and usage instructions directly from the command line. To add a help option, ChatGPT typically uses the argparse module in Python, which provides a way to parse command-line options and arguments.

By including a help option, users can quickly understand what the script does and how to use it without needing to read through the source code or external documentation. This makes the script more user-friendly and accessible.

To access the help information for a script, you can typically use the -h or --help flag when running the script. For example:

python script_name.py --help

This will display the help message, including a brief description of the script and the available options.

Additional Specifications for Scripts

The following are some additional specifications that can be included in Python scripts to enhance their functionality:

  • Output Filename Specification: An option can be added to specify the output filename. If not specified, a default name can be used by adding a specific suffix after the original basename.

  • Filename Candidates: When suggesting filenames for the script, some candidates can be provided for the user to choose from.

  • Overwrite Confirmation: Before overwriting an existing file, the script can ask for confirmation from the user with a "(y)es/(n)o" prompt. By default, the script can be set not to overwrite the file.

  • Shebang and Encoding Declaration: The following shebang and encoding declaration can be added at the beginning of the script to ensure that it is executed using Python 3 and with UTF-8 encoding:

    #!/usr/bin/env python3
    # -*- coding: utf-8 -*-

By incorporating these specifications, scripts can become more user-friendly, flexible, and safe to use, providing a better experience for users who run them.

Scripts

0. Remove Empty Directories Script

  • File: Remove_empty_dirs_script_2024_0327.md
  • Description: This script recursively finds and removes empty directories in a specified path. It includes options for specifying the output filename, running in silent mode, and displaying help.
  • Usage:
    python 0_clean_empty_folders/clean_empty_folders.py --path <path-to-search> --output <output-file> --silent

1. Merge PNG/JPG to PDF

  • File: Merge_Images_to_PDF_2024_0609.md
  • Description: This script merges PNG/JPG image files into a single PDF file while preserving their original dimensions. It includes options for specifying the output filename and confirming before overwriting an existing file.
  • Usage:
    python 1_merge_images/merge_images.py image1.png image2.jpg -o output.pdf

2. Rotate Image Python Script

  • File: Rotate_Image_Python_Script_2024_0328.md
  • Description: This script rotates a PNG/JPG image by 90 degrees. It includes options for specifying the output filename, adding a help option, and confirming before overwriting an existing file.
  • Usage:
    python 2_rotate_image/rotate_image.py input_image.png --output rotated_image.png

3. ImageMagick Python Watermark Script

  • File: ImageMagick_Python_Watermark_Script_2024_0517.md
  • Description: This script adds a transparent watermark to an input image using the ImageMagick command through subprocess. It includes options for specifying the word, font size, color, opacity, and output filename. The watermark is placed in the center of the image, with the date information just below it. The script also confirms before overwriting an existing file.
  • Usage:
    python 3_add_watermark/add_watermark.py input_image.jpg --word "Confidential" --size 120 --color "rgba(0, 0, 0, 0.5)" --output output_image.jpg

How to Use

To use these scripts, clone this repository and navigate to the directory containing the script you want to use. Make sure you have the required dependencies installed, and then run the script with the appropriate arguments.

About

A repository showcasing how to use ChatGPT to generate Python 3 code for various purposes.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages