Packages#

The main feature of Python is its extensive collection of packages that enhance its functionality. On this page, we will explore various aspects of working with some of these packages.

Note: Some packages have dedicated sections.

The table below lists the most useful python packages whose names are easy to forgot.

Package

Purpose / Goal

Install Command

Docs Link

pyperclip

Copy/paste clipboard cross-platform

pip install pyperclip

Docs

python-dotenv

Load .env file into environment variables

pip install python-dotenv

Docs

rich

Pretty terminal output (text, tables, markdown)

pip install rich

Docs

typer

Build beautiful CLI apps with minimal code

pip install typer

Docs

pydantic

Data validation and parsing using Python types

pip install pydantic

Docs

httpx

Modern async/sync HTTP client

pip install httpx

Docs

schedule

Lightweight job scheduler

pip install schedule

Docs

watchdog

Watch for file system changes

pip install watchdog

Docs

click

Create command-line interfaces

pip install click

Docs

loguru

Super simple logging

pip install loguru

Docs

tabulate

Format tables in plain text

pip install tabulate

Docs

tqdm

Add progress bars to loops

pip install tqdm

Docs

colorama

Cross-platform terminal colors

pip install colorama

Docs

validators

Validate emails, URLs, IPs, etc.

pip install validators

Docs

filetype

Guess file type by content, not extension

pip install filetype

Docs

humanize

Convert numbers/dates to human-readable formats

pip install humanize

Docs

yaspin

Pretty terminal spinners

pip install yaspin

Docs

icecream

Debug print with context (like ic())

pip install icecream

Docs

dateparser

Parse natural language dates

pip install dateparser

Docs

shortuuid

Short, URL-safe UUIDs

pip install shortuuid

Docs

tqdm#

tqdm is a package that allows to create progress bars in the terminal using pseudographics. For more details check specific page.


The following cell demonstrates typical usage of tqdm. The tqdm.tqdm object wraps an iterable in a for loop, so each iteration of the loop increments the progress bar by 1.

import tqdm
for _ in tqdm.tqdm(range(10), ncols=100, desc="my bar"): pass
my bar: 100%|███████████████████████████████████████████████████| 10/10 [00:00<00:00, 120180.63it/s]

zipfile#

zipfile is a package that allows you to operate on ZIP archives in Python.


As an example, we’ll create a ZIP file using a Linux command line, and then unpack that archive using Python.

The following cell is creating file that we’ll use as example.

%%bash
echo "zipped message" > zip_example_file
zip -r archive.zip zip_example_file
rm zip_example_file
  adding: zip_example_file (stored 0%)

Now, using zipfile.ZipFile, we’ll open the created archive and extract all its contents to the current directory.

from zipfile import ZipFile
with ZipFile("archive.zip") as f:
    f.extractall(".")

Now we can check the contents of the extracted file - it the same like during creation.

!cat zip_example_file
zipped message

After everything is done, don’t forget to clear the environment.

%%bash
rm -r zip_example_file archive.zip

Psutil#

psutil is a package that allows you to get in python program information about the harware resources used by a Python program. Check details in the documentation of the tool.


The following cell shows code to get information about the RAM used by the program.

import psutil
process = psutil.Process()
(process.memory_info().rss / (1024**2))
62.97265625

To make sure that it really works, the following cell creates a large object and counts the RAM used by the process.

ans = [0 for _ in range(2**25)]
(process.memory_full_info().rss / (1024**2))
318.97265625

Now the number is much larger. Finally, delete the large object and measure the RAM again.

del ans
(process.memory_full_info().rss / (1024**2))
63.171875

APScheduler#

APScheduler is a package that allows to start and controll processes that will do some jobs according to some triggers.

For more check:


The following cell shows how to run the simplest BackgroundScheduler. This code will start a process that prints the current time to the console every 3 seconds.

from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
def periodic_task():
    print(datetime.now().time())

scheduler = BackgroundScheduler()
scheduler.add_job(periodic_task, "interval", seconds=3)
scheduler.start()
11:54:38.109044
11:54:41.108479
11:54:44.108156
11:54:47.108562
11:54:50.108621
11:54:53.108557

Stop the scheduler using the shutdown method.

scheduler.shutdown()

Drawsvg#

drawsvg is a package that allows you to describe SVG graphics with python code. Check out the Drawsvg quick reference.

The following table represents the basic features of the drawsvg package:

Category

Function / Class

Description

Drawing

drawsvg.Drawing

Create a drawing container.

Drawing.append

Append elements to the drawing.

Drawing.to_svg

Export as an SVG string.

Drawing.save_svg

Save SVG directly to file.

Drawing.save_png

Export as PNG (requires cairosvg).

Composition

drawsvg.Group

Group multiple elements (<g>).

drawsvg.Use

Reuse elements by reference (<use>).

Shapes

drawsvg.Rectangle

Draw rectangles.

drawsvg.Circle

Draw circles.

drawsvg.Ellipse

Draw ellipses.

drawsvg.Line

Draw straight lines.

drawsvg.Polygon

Draw polygons.

drawsvg.Path

Freeform path drawing.

Text

drawsvg.Text

Add text elements.

Transformations

drawsvg.Translate

Apply translation transform.

drawsvg.Scale

Apply scaling transform.

drawsvg.Rotate

Apply rotation transform.

element.set_transform

Set transform on an element.

Accessibility

element.append_title

Add title (tooltip).

element.append_desc

Add description (for accessibility/metadata).


The following cell shows how to draw a cross on a red background using svg.

import drawsvg as dw
d = dw.Drawing(500, 500)
d.append(dw.Rectangle(10, 10, 490, 490, fill="red"))
d.append(dw.Line(50, 450, 450, 50, stroke='black', stroke_width=10))
d.append(dw.Line(50, 50, 450, 450, stroke='black', stroke_width=10))
d
../_images/f0b3c8d321c06af7d4f6bdae39804272545d06bec5ec9b8c9b8d9b8271b85c80.svg

As you can see, Jupyter knows how to represent the results of the drawsvg library.

For me, it’s typically important to be able to load the corresponding SVG code. Do that with as_svg method of the drawsvg canvas.

print(d.as_svg())
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
     width="500" height="500" viewBox="0 0 500 500">
<defs>
</defs>
<rect x="10" y="10" width="490" height="490" fill="red" />
<path d="M50,450 L450,50" stroke="black" stroke-width="10" />
<path d="M50,50 L450,450" stroke="black" stroke-width="10" />
</svg>

Viewbox#

Applying a viewbox can be a bit confusing. This is because original SVG syntax allows for some flexibility. In drawsvg, you have to specify exactly viewBox name (camel naming) and enter the value as a sting with four numbers separated by the space.


The following cell illustrates the use of the viewBox. It sets the viewbox to display only a corner of the rectangle added later.

d = dw.Drawing(width=200, height=200, viewBox="-10 -10 100 100")
d.append(dw.Rectangle(
    0, 0, 200, 200,
    fill="none",
    stroke_width=20,
    stroke="black"
))
d
../_images/f28b5e445d7b9cdc357100f4e4e56419e7bb5f1e6db7bd312432b4d08bae7b81.svg

Gradio#

Gradio is an instrument that allows you to build simple user interfaces for taking some input and immediate output. It is ideal for prototyping the machine leaning models, but can also be used in more generally.


The following cell shows the gradio hello world and an approach to visualizing the output in a jupyter notebook.

import gradio as gr

def greet(name: str) -> str:
    return f"Hello {name}!"

demo = gr.Interface(fn=greet, inputs="text", outputs="text")
demo.launch(inline=True)
* Running on local URL:  http://127.0.0.1:7860
* To create a public link, set `share=True` in `launch()`.