Certainly! Here's a README.md template for the given class:
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.
The CustomScript class offers the following key features:
- Argument Parsing: Simplifies command-line argument processing using the
argparsemodule. - Logging: Configures a logging system to record messages during script execution.
- Error Handling: Catches and logs exceptions, ensuring proper error handling.
- Checksum: Calculates the MD5 hash checksum of the script file for integrity verification.
- 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.
To use the CustomScript class, follow these steps:
-
Import the necessary modules:
import argparse import logging import hashlib import os import sys import time
-
Instantiate the
CustomScriptclass:script = CustomScript()
-
Parse the command-line arguments:
args = script.parse_args()
-
Implement the
run_scriptmethod in your subclass:class MyCustomScript(CustomScript): def run_script(self): # Your script logic goes here pass
-
Handle errors by calling the
error_handlingmethod:script.error_handling()
-
Finalize the script execution and display the results:
script.finalize()
-
Customize the class and its behavior as needed, such as modifying the description, adding additional script arguments, or overriding existing methods.
The constructor method for the CustomScript class.
description(optional): The description of the script (default: "Custom Python script").dryrun(optional): If set toTrue, the script will run in dryrun mode (default:False).verbose(optional): If set toTrue, the script will produce verbose output (default:False).logger(optional): An existing logger instance that will be used for logging.
Parses the command-line arguments using the argparse module and returns the parsed arguments as a Namespace object.
Executes the script's run_script method within a try-except block. If an exception occurs, it logs the error and exits the script.
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.
Finalizes the script execution by logging the completion message, displaying the checksum of the script file, and showing the total execution time.
Calculates and returns the MD5 checksum of the script file.
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()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!