Skip to main content

new.py

Python program to write new Python programs

Description

The "new.py" program is intended to automate the creation of a program that uses argparse to handle command-line arguments. Run with -h|--help for the documentation:

usage: new.py [-h] [-n NAME] [-e EMAIL] [-p PURPOSE] [-t] [-f] program

Create Python argparse program

positional arguments:
  program               Program name

optional arguments:
  -h, --help            show this help message and exit
  -n NAME, --name NAME  Name for docstring (default: Ken Youens-Clark)
  -e EMAIL, --email EMAIL
                        Email for docstring (default: kyclark@gmail.com)
  -p PURPOSE, --purpose PURPOSE
                        Purpose for docstring (default: Rock the Casbah)
  -t, --write_test      Create basic test.py (default: False)
  -f, --force           Overwrite existing (default: False)

The only required argument is the "program" name:

$ new.py foo.py
Done, see new script "foo.py."

Now you will have a program that will accept the following parameters:

$ ./foo.py -h
usage: foo.py [-h] [-a str] [-i int] [-f FILE] [-o] str

Rock the Casbah

positional arguments:
  str                   A positional argument

optional arguments:
  -h, --help            show this help message and exit
  -a str, --arg str     A named string argument (default: )
  -i int, --int int     A named integer argument (default: 0)
  -f FILE, --file FILE  A readable file (default: None)
  -o, --on              A boolean flag (default: False)

Edit the "get_args()" function to whatever your program's requirements may be.

If you run this same command again, the "foo.py" file will be detected, and you will be prompted to see if you wish to overwrite it. Unless you answer "yes," the program will exit:

$ new.py foo.py
"foo.py" exists.  Overwrite? [yN] n
Will not overwrite. Bye!

If you do wish to overwrite the program, you can use the -f|--force flag:

$ new.py foo.py -f
Done, see new script "foo.py."

You can indicate the name and email address you wish to appear in the program's docstrings, or you can write a ~/.new.py configuration file with these parameters, e.g.:

$ cat ~/.new.py
name=Ken Youens-Clark
email=kyclark@gmail.com
purpose=Look out, you rock-and-rollers!

The -p|--purpose option will also become part of the program docstring/argparse documentation. You can also add a "purpose" option to your "~/.new.py" file to override the default "Rock the Casbah" value.

Use the -t|--test option to create a "{prg}_test.py" file and a "Makefile":

$ new.py -t foo.py
Done, see new script "foo.py."

Now you should have the following files:

$ find . -type f
./Makefile
./tests/foo_test.py
./foo.py

You can run "make test" to execute "python3 -m pytest -xv":

$ make test
python3 -m pytest -xv
============================= test session starts ==============================
...

tests/foo_test.py::test_exists PASSED                                    [ 33%]
tests/foo_test.py::test_usage PASSED                                     [ 66%]
tests/foo_test.py::test_ok PASSED                                        [100%]

============================== 3 passed in 0.27s ===============================

Installation

You can copy the new.py program to any directory currently in your $PATH. It's common to place programs into a directory like /usr/local/bin, but this often will require root priviliges. A common workaround is to create a writable directory in your $HOME where you can place programs. I like to use $HOME/.local as the "prefix" for local software installations. This means that $HOME/.local/bin will usually the be location where binaries will be placed; therefore I will add this to my .bash_profile (or .bashrc) file:

export PATH=$HOME/.local/bin:$PATH

Author

Ken Youens-Clark kyclark@gmail.com

Metadata

Release files for new-py 0.1.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for new-py 0.1.4
File Size Uploaded Provenance Actions
new_py-0.1.4.tar.gz 6.7 kB Without provenance Details for new_py-0.1.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for new-py 0.1.4
File Interpreter ABI Platform Actions
new_py-0.1.4-py3-none-any.whl Python 3 none any Details for new_py-0.1.4-py3-none-any.whl

Total release size: 15.6 kB

Release files / new_py-0.1.4.tar.gz

Download URL new_py-0.1.4.tar.gz
Size 6.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9977169c2257215329265d1e8b7debe3bda01225423e701287d62a02a96bddba
BLAKE2b-256 checksum
How to use checksums
fea163ff072283c4a4ca091f9ec792f17aa6ec5d8eb9b4554b2998373e5db84b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Release files / new_py-0.1.4-py3-none-any.whl

Download URL new_py-0.1.4-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2cd021fc77b96be1f802d685c86b93d91904d76f4e39fe049a6df3b6245614e0
BLAKE2b-256 checksum
How to use checksums
421c003a8d0a4a969615d825f61e3d17a6f30a6653c6919248ed5df713c1d367
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.2

Security for new-py 0.1.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page