import sanity: Python Modules That Keep Me Sane 🐍

illegalbyte

Lewis Gentle

Posted on August 13, 2021

import sanity: Python Modules That Keep Me Sane 🐍

One of the many great things about the snake language (read: Python) is that there's a huge number of modules and libraries that we can easily tap into to make building cool stuff a lot easier.

Python is unlike PHP, where a travelling function salesman will arrive every 2 minutes offering you a new built-in function (i.e. functions which are always accessible). Seriously, there are over 1000 built-in functions in PHP... whereas mere mortal Python 3 has a humble 68 built-in functions. Thus, becoming great at Python requires being able to effectively navigate the zoo of Python modules out there (and getting really fast at smashing pip install into your terminal. Unless of course you demand having absolute control over your entire codebase – in which case, maybe C++ is more your thing.

With all that said, I'm a complete noob at programming. I know next to nothing. But I thought it might help some other beginners (and myself in 2 days when I forget one of these modules) to compile a list of modules I've found to be really useful. A lot of these are either written by Al Sweigart or I learned about them in his excellent book [Automate The Boring Stuff] which you can read free online(https://automatetheboringstuff.com/). Let's begin:

pyinputplus

One of the most exciting things I first learnt to do in Python was to take input from the user. It was magical. Until I realised how easy it was for users (aka me) to obliterate decimate my script by entering the wrong data type. This is known as input validation or 'accelerated grey hair growth'. pyinputplus is a module which contains a bunch of really useful functions which ensure the user enters the data type you want, and how you want it. For example, you can make sure they enter a goddamn positive integer:
pyinputplus.inputNum('Enter a positive number: ', min=1)
Or get their favourite McDonald's breakfast item by prompting them with a list of lettered options:
choices = ['McMuffin', 'Hashbrown', 'Pancakes']
pyip.inputMenu(choices, lettered=True)

Please select one of the following:
A. McMuffin
B. Hashbrown
C. Pancakes

Now taking orders is literally as easy as A, B, or C.

pprint

Sometimes when you're testing your code you just want to print a list or a dictionary in a more human friendly way. That's where prettyprint (pprint) comes in. Simply give it a list and your dictionary will go from a mountain of text, to a nicely printed outline... of text.

colorama

While we're talking about making things pretty and human friendly, colorama is what you should use if you want your terminal output to be ✨colourful✨. Sure you could use the ASCII escape codes or whatever, but these often fail across multiple platforms, and sometimes its easier to just enter something like:
print("New largest chain " + Fore.GREEN + str(largestChain) + Style.RESET_ALL)

pyperclip

This module makes it easy to interact with and manipulate the users clipboard. Execute pyperclip.copy(x) and x will be copied to the users clipboard, and use x = paperclip.past() and you'll have the users clipboard saved to x.


Of course there are some more fundamental modules and libraries such as os, CSV, and pathlib, but I thought I'd focus more on the niche modules which do one thing well and incite sanity.

💬 If you want to share your favourite python modules in the comments I'd love to hear about them!

💖 💪 🙅 🚩
illegalbyte
Lewis Gentle

Posted on August 13, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

How to level up your backend skills
webdev How to level up your backend skills

November 16, 2024

Hello DEV Community!
cybersecurity Hello DEV Community!

September 5, 2024

Conditions (If-else-elif) Part-08
beginners Conditions (If-else-elif) Part-08

September 4, 2024