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 |
---|---|---|---|
|
Copy/paste clipboard cross-platform |
|
|
|
Load |
|
|
|
Pretty terminal output (text, tables, markdown) |
|
|
|
Build beautiful CLI apps with minimal code |
|
|
|
Data validation and parsing using Python types |
|
|
|
Modern async/sync HTTP client |
|
|
|
Lightweight job scheduler |
|
|
|
Watch for file system changes |
|
|
|
Create command-line interfaces |
|
|
|
Super simple logging |
|
|
|
Format tables in plain text |
|
|
|
Add progress bars to loops |
|
|
|
Cross-platform terminal colors |
|
|
|
Validate emails, URLs, IPs, etc. |
|
|
|
Guess file type by content, not extension |
|
|
|
Convert numbers/dates to human-readable formats |
|
|
|
Pretty terminal spinners |
|
|
|
Debug print with context (like |
|
|
|
Parse natural language dates |
|
|
|
Short, URL-safe UUIDs |
|
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:
Official APScheduler documentation.
Corresponding page on this website.
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()
Hugging face#
Hugging Face is a site that allows you to publish and download models. It’s convenient because you can automate the process using Python scripting. For this, you need the huggingface-hub
package. Aspects of working with it are discussed on this page.
The first thing you need to do is log in:
huggingface-cli login --token <your HF token>
Check the set of tutorials:
LLM course from hugging face.
The transformers package, provided by the Hugging Face, allows you to use a variety of popular transformer-based models.
Drawsvg#
drawsvg
is a package that allows you to describe SVG graphics with python code. Check out the Drawsvg quick reference.
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
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