By ATS Staff on February 28th, 2024
Computer Languages Python Programming Software DevelopmentPython is renowned for its simplicity and an extensive ecosystem of third-party libraries, making it one of the most versatile programming languages available today. While popular libraries like NumPy, Pandas, and Django are frequently used, there are several lesser-known but powerful Python modules that can make your coding life much easier. In this article, we’ll explore some hidden gems that can enhance your Python projects, covering a variety of use cases from file handling to data visualization and automation.
Rich is a fantastic library for making your terminal output more visually appealing. It supports colored text, tables, progress bars, tracebacks, and much more. For developers who work on CLI tools or debug code often, Rich can transform how information is presented on the console.
pythonCopy codefrom rich import print
from rich.console import Console
console = Console()
console.print("[bold green]Hello, [yellow]world![/yellow][/bold green]")
Pydantic allows you to define data models that validate and parse data using Python's type hints. It’s great for use cases where you need to ensure that external data, like JSON or environment variables, is properly validated.
pythonCopy codefrom pydantic import BaseModel
class User(BaseModel):
id: int
name: str
email: str
user = User(id=1, name='John Doe', email='john@example.com')
print(user)
If you ever need to automate tasks based on file system events (e.g., moving, deleting, or modifying files), Watchdog is the module for you. This library allows you to monitor the file system and act on events like file creation, deletion, and modification in real time.
pythonCopy codeimport time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class MyHandler(FileSystemEventHandler):
def on_modified(self, event):
print(f"File {event.src_path} has been modified")
observer = Observer()
observer.schedule(MyHandler(), path='.', recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
PyFilesystem2 provides a unified interface for working with filesystems, whether local, cloud-based (like AWS S3), FTP, or even in-memory filesystems. It allows you to write code that is filesystem-agnostic, making it easy to switch between different file storage solutions without changing your code logic.
pythonCopy codeimport fs
# Create an in-memory filesystem and write a file
memory_fs = fs.open_fs('mem://')
memory_fs.writetext('hello.txt', 'Hello, world!')
# Read the file
print(memory_fs.readtext('hello.txt'))
If you need to execute shell commands within your Python script, Sh provides an elegant way to run them, capturing both output and errors. It allows you to execute Unix shell commands like you would in a terminal but with Pythonic syntax.
pythonCopy codeimport sh
# Run the 'ls' command in the current directory
print(sh.ls())
Though it's not technically a Python module you import, Poetry is a fantastic tool for managing Python projects, especially dependencies and packaging. It simplifies the process of installing, updating, and removing dependencies, and it automates version management.
sqlCopy code# Create a new poetry project
poetry new my_project
# Add a new dependency
poetry add requests
For anyone who runs long loops or iterations, Tqdm makes it easy to visualize progress. It provides a simple way to add progress bars to Python code and is often used in data science or ETL processes where feedback is essential.
pythonCopy codefrom tqdm import tqdm
import time
for i in tqdm(range(100)):
time.sleep(0.1) # Simulating work
Python’s built-in datetime module is powerful but can sometimes feel cumbersome. Arrow simplifies many common tasks related to date and time manipulation, such as converting time zones, generating human-friendly time formats, and more.
pythonCopy codeimport arrow
# Get current time and convert to a different timezone
utc = arrow.utcnow()
local = utc.to('US/Pacific')
print(local)
Blessings is another terminal styling library like Rich, but it's lighter and focuses primarily on terminal text decorations and cursor control. If you're building a command-line application, it can be a simple yet effective way to add interactive elements.
pythonCopy codefrom blessings import Terminal
t = Terminal()
print(t.bold('This is bold text!'))
For testing purposes, the Faker module allows you to generate fake data such as names, addresses, and even paragraphs of lorem ipsum text. It’s useful when you need large amounts of random data for testing, development, or seeding a database.
pythonCopy codefrom faker import Faker
fake = Faker()
print(fake.name())
print(fake.address())
While the big names in the Python ecosystem get most of the attention, these lesser-known libraries offer incredible functionality for a wide range of use cases. Whether you’re working on automating workflows, enhancing terminal outputs, or handling data validation, these hidden gems can save you time and effort in your next Python project.