Python Script Cookiecutter

Motivation

I need to write a python script to perform/automate some task from time to time. But I don't like writing the same thing over and over again and I sure don't need a package. So I wrote a cookiecutter template for my scripts. Check it out at GitHub.

Features

To use simply run cookiecutter gh:ArcHound/python_script_cc.

  • load environment variables from .env file (these might be used in place of CLI options!),
  • accept stdin via pipe or use a file specified in an option (if you'd like),
  • return to stdout or to a file specified in an option,
  • post generation hook to create virtual environment
  • option for logging verbosity level,
  • option for including the requests lib and requests_cache lib,
    • few options for a default service - name, url and bearer token,
    • proxy options for requests session,
  • python gitignore from [https://github.com/github/gitignore/blob/main/Python.gitignore].

Used projects (dependencies)

Cookiecutter

Cookiecutter was the obvious choice. Surprisingly, I was unable to find a template that would suit my needs, so I decided to roll with my own.

A fantastic feature is the ability to write post-gen hooks, I used these to create a virtual environment for script dependencies. Other than that, it's super simple.

Click

I decided to roll with Click as the framework for CLI. Not using any advanced features, but if the script needs expanding, it's possible.

Gitignore

Sometimes I'd need to have these scripts somewhat more permanent, so why not include a gitignore - taken from Github.

Requests and Cache

Lot of my use cases need to do API calls. When tuning the logic, you don't want to spend time waiting to get the same data. Until I need aiohttp for speed, I use the fantastic requests library. The requests-cache caches the data, speeding up the process greatly.

Roadmap?

As to what the new features might be - I don't know. I was looking at tqdm or other timing functionality, but it's not something to have in the template when the exact flow is unclear. I'd like to have colored logs, but those are not that integral to the general workflow. I might cave in to the pressure and wrap it up in a package.

Conclusion

This little template has been tremendously useful for me, hopefully it's useful for you too! I am using it pretty much daily, so it might get some new features over time.

tags: python