Skip to content
This repository was archived by the owner on Dec 11, 2025. It is now read-only.

arc4d3-io/python-script-boilerplate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Certainly! Here's a README.md template for the given class:

CustomScript

CustomScript is a Python class that serves as a template for creating custom Python scripts. It provides common functionalities such as argument parsing, logging, error handling, and execution time tracking.

Features

The CustomScript class offers the following key features:

  1. Argument Parsing: Simplifies command-line argument processing using the argparse module.
  2. Logging: Configures a logging system to record messages during script execution.
  3. Error Handling: Catches and logs exceptions, ensuring proper error handling.
  4. Checksum: Calculates the MD5 hash checksum of the script file for integrity verification.
  5. Execution Time Tracking: Measures the total execution time of the script.

These features provide a solid foundation for creating custom Python scripts, streamlining argument handling, logging, error management, and execution time monitoring. The CustomScript class can be extended by subclasses to implement custom script logic.

Usage

To use the CustomScript class, follow these steps:

  1. Import the necessary modules:

    import argparse
    import logging
    import hashlib
    import os
    import sys
    import time
  2. Instantiate the CustomScript class:

    script = CustomScript()
  3. Parse the command-line arguments:

    args = script.parse_args()
  4. Implement the run_script method in your subclass:

    class MyCustomScript(CustomScript):
        def run_script(self):
            # Your script logic goes here
            pass
  5. Handle errors by calling the error_handling method:

    script.error_handling()
  6. Finalize the script execution and display the results:

    script.finalize()
  7. Customize the class and its behavior as needed, such as modifying the description, adding additional script arguments, or overriding existing methods.

Class Methods

__init__(self, description="Custom Python script", dryrun=False, verbose=False, logger=None)

The constructor method for the CustomScript class.

  • description (optional): The description of the script (default: "Custom Python script").
  • dryrun (optional): If set to True, the script will run in dryrun mode (default: False).
  • verbose (optional): If set to True, the script will produce verbose output (default: False).
  • logger (optional): An existing logger instance that will be used for logging.

parse_args(self) -> Namespace

Parses the command-line arguments using the argparse module and returns the parsed arguments as a Namespace object.

error_handling(self)

Executes the script's run_script method within a try-except block. If an exception occurs, it logs the error and exits the script.

run_script(self)

Note: This method needs to be implemented in a subclass.

Placeholder method that should be overridden in a subclass to contain the custom logic of the script.

finalize(self)

Finalizes the script execution by logging the completion message, displaying the checksum of the script file, and showing the total execution time.

checksum(self) -> str

Calculates and returns the MD5 checksum of the script file.

Example

Here's an example of how to create a custom script using the CustomScript class:

import argparse
import logging
import hashlib
import os
import sys
import time

class MyCustomScript(CustomScript):
    def run_script(self):
        # Custom script logic goes here
        self.logger.info("Running MyCustomScript...")

if __name__ == "__main__":
    script = MyCustomScript()
    args = script.parse_args()
    script.error_handling()
    script.finalize()

License

This project is licensed under the MIT License.

Feel free to customize and adapt the CustomScript class to suit your specific script requirements. Happy coding!

About

An initial skeleton for creating Python scripts with common features like logging, command-line arguments, and error handling

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages