top of page
Writer's pictureThe Tech Platform

Python Modules you should know

In this article, we’ll explore some built-in modules that help to solve common problems quickly and provide flexibility without the bloat.




What are Python Modules?

A Python module is a file containing Python definitions and statements. A module can define functions, classes, and variables. A module can also include runnable code. Grouping related code into a module makes the code easier to understand and use. It also makes the code logically organized.


Python Modules you should know

Here we have standard Python Modules you should know


1. textwrap

The textwrap module has a suite of utilities for manipulating the overall length of text data.

With this module, you can wrap and shorten your way to prettier strings. Let’s look at how we could shorten a longer sentence:

import textwrap

sentence = "hello, this is a really long sentence of words"

wrapped = textwrap.shorten(sentence, width=25, placeholder=" ...")

print(wrapped)

The result will shorten our sentence and insert a placeholder indicating we truncated some text:

>>> hello, this is a ...

This module could be useful for displaying previews of longer descriptions, or just making sure incoming data adheres to your visual guidelines.


2. pprint

The pprint module lets you display complex data structures in a human-readable format. This can be especially helpful if you’re dropping into the Python interpreter to try something out.


Let’s print a dictionary using the following example:

import pprint

data = {'one':1,'two':2,'three':3,'four':4}

pprint.pprint(data, indent=4, width=10, sort_dicts=False)

Here we have a simple dictionary of data. We print the dictionary, specifying how we want the output to be indented and the overall width. The reason we don’t sort the dict is that it is already sorted when it is assigned.

>>> {    'one': 1,
         'two': 2,
         'three': 3,
         'four': 4}

We can see this is clearly much more readable than the one-line definition. This becomes more and more useful as the data grows larger. Being able to read a massive dictionary of deeply nested values, or search a list for specific values is made infinitely easier with this library.


3. tempfile

Building your own temp file solution is like reinventing the wheel just because you’re bored. Don’t do it. Use the tempfile module instead. This built-in module will handle all of the heavy liftings of creating temp files and reading from or writing to them.

import tempfile

tf = tempfile.TemporaryFile()

tf.write(b'i am some temporary data')

tf.seek(0)

tf.read()

Using the example above you can automatically generate a temp file, write to it and then read it back in less than a few lines. You don’t have to provide directory paths, deal with permissions or mess with any low-level filesystem properties if you don’t want to. If you do want to specify a named file you can use the NamedTemporaryFile object instead. This will provide an interface for a file you can actually interact with at the operating system level.


4. platform

The platform module provides essential information about the underlying operating system and hardware. You can also obtain a wealth of info about the running Python interpreter. This module is handy for checking things like the hardware architecture. If we wanted to determine the arch of the underlying system we’re on we could run:

import platform

print(platform.machine())

This should produce the architecture of the system. For example, on an M1 MacBook Pro this should be: arm64. We could also find out our current running Python interpreter version by running:

import platform

print(platform.python_version())

This should produce a string of the full version number, like 3.9.13. The platform module also contains operating system specific methods as well. You can determine OS-specific information about Linux, macOS and Windows machines.


5. webbrowser

This module will let you manipulate web browsers on the underlying machine. You can do things like open URLs and get basic information about a system’s browser. The webbrowser module might not be the most practical for things like integration testing, but if you just want to display web pages to an end-user this is a quick and easy way to do it.


If we wanted to open google.com on the underlying machine all we need to do is execute:

import webbrowser

webbrowser.open('https://google.com')

This should open a new browser or a new tab (if the browser is open already) and navigate to google.com. Pretty neat! Some cool uses for this might be displaying documentation about your app to a new user, or opening a web page that generates a QR code. There’s a ton of possibilities once you’re in control of the browser.


6. ipaddress

The ipaddress module has been a favorite of mine for a while. This is invaluable if you’re doing any kind of networking with Python. Manipulating IP address strings manually is mind numbing and just plain inefficient. This library gives you a slick, simple interface for generating and analyzing IP addresses and network spaces.


This module turn a simple IP address string into a metadata-rich object. Let’s say we had a basic IP address like: 10.1.2.3. Using this module we could get a lot of info from just this one address:

import ipaddress

ip_addr = ipaddress.ip_address('10.1.2.3')

print(ip_addr.is_private)
print(ip_addr.version)
print(ip_addr.is_loopback)

We can determine basic things like if the address is public or private. We can figure out what IP version it is (v4 or v6). We can also test for many other conditions to figure out what kind of address we’re dealing with. This is great for anything that accepts user input or bulk collection so you can filter out incorrect address types.


One very handy feature is the ability to analyze subnets. Let’s say we had a network provided in CIDR notation like this: 10.1.2.0/24. We could get basic info about the network like this:

import ipaddress

ip_addr = ipaddress.ip_network('10.1.2.0/24')

print(ip_addr.netmask)
print(ip_addr.num_addresses)
print(ip_addr.broadcast_address)

These methods would give us the subnet mask (in decimal notation), the number of total addresses, and the broadcast address of the network. Combined with host addresses, you could determine a myriad of information about IP address space.




Resource: Better Programming (Medium)


The Tech Platform

0 comments

Commentaires


bottom of page