My Favourite Python Modules

My Favourite Python Modules

Intro

So today I'm gonna tell you guys my favourite python modules which you will use in almost all of your projects. These are all practical modules that a lot of developers know how to use and that you will need to learn sooner or later.

The modules

1. Re

Re, short for "regular expressions" or "regex" is a pattern matching library that helps in many cases, especially when parsing things. A few of its major functions are: search(), findall() and sub() For example:

import re

# a regex to check if the given text contains a phone number 
numbers = re.findall("^(\+\d{1,2}\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}$", "123 132 1234")
if len(numbers) < 0:
  print("Numbers Found")

2. IO

IO is a good library for creating file-like objects which aren't stored on your hard drive but your RAM. Let's say I'm making a discord bot which pulls an image from an API without saving it and directly sends it. The io.BytesIO class would be good for that. If I didn't explain it how you want it, then be my guest and read the documentation. Read their explanation.

Binary I/O (also called buffered I/O) expects bytes-like objects and produces bytes objects. No encoding, decoding, or newline translation is performed. This category of streams can be used for all kinds of non-text data, and also when manual control over the handling of text data is desired.

Here's are 2 very basic examples of making a file-like object that stores data in a file-like object:

import io

# Storing data in bytes
f = io.BytesIO(b"Hello Cruel World")
print(f.read())

# Storing data in a string
f = io.StringIO("Hello Cruel World")
print(f.read())

Output:

b'Hello Cruel World'
Hello Cruel World

3. Requests and BeautifulSoup

I combined both of these because they work together to yield the best results. Requests is a library used for accessing APIs that can work with BeautifulSoup to parse and scrape the source code of a page in HTML.

Here's an example of scraping the meriam webster site to get definitions of words:

import requests
from bs4 import BeautifulSoup

resp = requests.get("https://www.merriam-webster.com/dictionary/atomic") # Accessing the word "atomic" from the dictionary
soup = BeautifulSoup(resp.content, "html.parser")
meaning = soup.select("span.dtText")[0].text[2:]
print(meaning)

4. OS

OS is a module that helps you interact with your operating system and file system. It can perform many functions.

Here are a few of those':

>>> import os
>>> os.environ["APPDATA"] 
'C:\\Users\\XXXXX\\AppData\\Roaming'
>>> os.getcwd()
'D:\\XXXXX\\Projects\\Testing'
>>> os.mkdir("new_dir") 
>>> os.listdir('D:\\XXXXX\\Projects\\Testing') 
['main.py', 'new_dir']

5. Aiosqlite

All I can say is, this module should be a lot more popular than it is. It saved me from falling into the endless abyss of JSON! It helps you perform queries and actions and all that on sqlite databases. What separates it from the sqlite library is that it is completely asynchronous meaning it can perform many tasks at the same time

Here is an example of how I use it:

import aiosqlite

id = 8347234980213
async with aiosqlite.connect("db.sqlite") as db:
  cursor = await db.cursor()
  await cursor.execute("SELECT * FROM queue WHERE id = ?", (id,))

Outro

And that's it for my 5 favourite python modules. Please follow me on github so I stay motivated to spend my time writing these posts and I guess I'll see you on discord! (AtomicByte#0973)

Did you find this article valuable?

Support AtomicByte by becoming a sponsor. Any amount is appreciated!