new.py: A Utility for Starting New Python Programs

The new.py script is a versatile utility designed to streamline the process of starting new Python programs. It provides a convenient way to create a new program file from a predefined template, ensuring that you begin with a complete, working program structure. This approach allows you to focus on the specific functionality of your program rather than setting up the basic structure from scratch.

Features of new.py

Template-Based Program Creation

The new.py script generates a new Python program using a template that includes essential components such as:

  • A shebang line for script execution.
  • Docstrings for documentation.
  • A main() function as the entry point of the program.
  • A get_args() function for command-line argument parsing.

This boilerplate code provides a solid foundation for developing Python programs, especially those that require command-line interaction.

File Safety and Customization

One of the key features of new.py is its ability to prevent accidental overwriting of existing files. When you attempt to create a new program file, new.py checks if the file already exists and prompts you for confirmation before overwriting. This ensures that your existing work is protected.

Additionally, new.py allows for customization of author details through a .new.py configuration file located in the user's home directory. This feature enables you to personalize the generated program files with your information.

Usage Examples

The new.py script can be executed from the top level of the repository or from any directory if copied to your ~/bin directory. Here are some examples of how to use new.py:

  1. Creating a New Program:

    To create a new program file, you can run the following command:

    $ bin/new.py 02_crowsnest/crowsnest.py
    Done, see new script "02_crowsnest/crowsnest.py."
    

    This command generates a new script at the specified location with the necessary boilerplate code.

  2. Handling Existing Files:

    If you attempt to create a file that already exists, new.py will prompt you for confirmation:

    $ bin/new.py 01_hello/hello.py
    "01_hello/hello.py" exists.  Overwrite? [yN] n
    Will not overwrite. Bye!
    

    In this example, the user chose not to overwrite the existing file.

  3. Creating a Program with a Different Name:

    You can also create a new program with a different name to avoid overwriting:

    $ bin/new.py 01_hello/hello2.py
    Done, see new script "01_hello/hello2.py."
    

    This command creates a new script named hello2.py.

Conclusion

The new.py script is a valuable tool for Python developers, providing a quick and efficient way to start new projects with a consistent structure. By using new.py, you can ensure that your programs are well-organized from the outset, allowing you to focus on implementing the core functionality of your applications.