Python: Libraries you should use — Part-2

Gateway to Efficiency

Pravash
3 min readMay 30, 2024

Hi, Welcome to the next installment of my exploration into Python’s vast library ecosystem! In this segment, I will continue uncovering some of the coolest Python libraries that I think can enhance your projects and simply your development process.

Let’s dive in —

1. Icecream

This library helps debugging easier. And the really nice about this is that you can use print() or log() to debug. It prints expressions and their values, making it 60% faster to type. Data structures are neatly formatted, and the output is syntax-highlighted.

Example —

from icecream import ic

def foo(i):
return i + 333

print(foo(123)) # 456
ic(foo(123)) # ic| foo(123): 456

So in the above example, ic not only going to print the result but also going to print the function and the arguments that were passed to the function. So, yes this is incredibly useful for debugging as you will save lots of time printing variables.

2. rich

Rich is a Python library for enhancing terminal output with rich text and beautiful formatting.

Example —

from rich.console import Console
from rich.table import Table

table = Table(title="Star Wars Movies")
table.add_column("Released", justify="right", style="cyan", no_wrap=True)
table.add_column("Title", style="magenta")
table.add_column("Box Office", justify="right", style="green")
table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690")
table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889")
table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")
console = Console()
console.print(table)

output —

Star Wars Movies                           
┏━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━┓
┃ Released ┃ Title ┃ Box Office ┃
┡━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━┩
│ Dec 20, 2019 │ Star Wars: The Rise of Skywalker │ $952,110,690 │
│ May 25, 2018 │ Solo: A Star Wars Story │ $393,151,347 │
│ Dec 15, 2017 │ Star Wars Ep. V111: The Last Jedi │ $1,332,539,889 │
│ Dec 16, 2016 │ Rogue One: A Star Wars Story │ $1,332,439,889 │
└──────────────┴───────────────────────────────────┴────────────────┘

3. Polars

This is a dataframe library that is really optimised for speed and parallel execution. This can be used with multithreading and it’s really good choice for heavy computation.

Example —

import polars as pl

data = {'name': ['Alice', 'Bob', 'Charlie'],
'age': [25, 30, 35],
'salary': [50000, 60000, 70000]}
df = pl.DataFrame(data)
result = df.lazy().filter(pl.col("age") > 25).select(["name", "age", "salary"])
final_df = result.collect()
print(final_df)

output —

SELECT [col("name"), col("age"), col("salary")] FROM
FILTER [(col("age")) > (25)] FROM
DF ["name", "age", "salary"]; PROJECT */3 COLUMNS; SELECTION: "None"

┌─────────┬─────┬────────┐
│ name ┆ age ┆ salary │
│ --- ┆ --- ┆ --- │
│ str ┆ i64 ┆ i64 │
╞═════════╪═════╪════════╡
│ Bob ┆ 30 ┆ 60000 │
│ Charlie ┆ 35 ┆ 70000 │
└─────────┴─────┴────────┘

If you use lazy() and print the result, it will show how the execution takes place. Then use collect to print the result.

Note:
One of the most efficient ways to process tabular data is to parallelize its processing via the “split-apply-combine” approach. This operation is at the core of the Polars grouping implementation, allowing it to attain lightning-fast operations.
Specifically, both the “split” and “apply” phases are executed in a multi-threaded fashion.

4. FastAPI

This is a modern web framework for building backend API with python. It has better tooling and has more support for python features. for example, it supports concurrency and async await. It also offers inbuilt data validation.

Example —

from enum import Enum
from fastapi import FastAPI

app = FastAPI()
class AvailableItems(str, Enum):
indian = "indian"
american = "american"
italian = "italian"
items = []
food_items = {
'indian': ['food_ind1', 'food_ind2'],
'american': ['food_am1', 'food_am2']
}
@app.get("/")
def root():
return {'payload': 'Hello API'}
@app.post("/items")
def create_item(item: str):
items.append(item)
return items
@app.get("/items/{item_name}")
async def get_items(item_name: AvailableItems):
return food_items.get(item_name)

Output —
In the unix shell run these commands,

uvicorn main:app --reload
o/p = {"payload":"Hello API"}

curl -X POST -H "Content-Type: application/json" 'http://127.0.0.1:8000/items?item=apple'
o/p = ["apple"]

curl -X POST -H "Content-Type: application/json" 'http://127.0.0.1:8000/items?item=mango'
o/p = ["apple","mango"]

curl -X GET http://127.0.0.1:8000/items/indian
o/p = ["food_ind1","food_ind2"]

5. tqdm

This instantly make your loops show as a progress bar meter. You can use this in your code where you require monitoring progress.

Example —

from tqdm import tqdm

for i in tqdm(range(500000000)):
pass

Output —

100%|██████████| 500000000/500000000 [01:43<00:00, 4824014.12it/s]

Conclusion

I hope you enjoyed this list of python libraries. I used almost all the library in my projects and yes they are super helpful.

Python offers a rich ecosystem of libraries that cater to various needs and domains. Whether you’re working on data analysis, machine learning, web development, or scientific computing, there’s likely a library available to streamline your tasks and boost productivity.

So, don’t hesitate to dive into the vast Python library ecosystem and make the most of these invaluable resources for your projects.

--

--

Pravash

I am a passionate Data Engineer and Technology Enthusiast. Here I am using this platform to share my knowledge and experience on tech stacks.