Python Libraries That Shocked Me With Their Power

Python Libraries That Shocked Me With Their Power

rich (Beautiful Console Apps With No Design Degree Required)

If your CLI output looks like a Windows 98 error screen, this is your fix.

>>> from rich.console import Console
>>> from rich.table import Table
>>>
>>> console = Console()
>>> table = Table(title="Crypto Prices")
>>>
>>> table.add_column("Coin", style="cyan")
>>> table.add_column("Price", justify="right", style="green")
>>>
>>> table.add_row("Bitcoin", "$43,210")
>>> table.add_row("Ethereum", "$3,210")
>>>
>>> console.print(table)
    Crypto Prices
┌──────────┬─────────┐
 Coin        Price 
├──────────┼─────────┤
 Bitcoin   $43,210 
 Ethereum   $3,210 
└──────────┴─────────┘

python rich tables example python rich tables example

boltons (Batteries You Didn’t Know Were Missing)

Think of boltons as the "secret drawer" of Python utilities.

boltons.iterutils.unique_iter(src, key=None)
unique docs
Yield unique elements from the iterable, src, based on key, in the order in which they first appeared in src.
By default, key is the object itself, but key can either be a callable or, for convenience, a string name of the attribute on which to uniqueify objects, falling back on identity when the attribute is not present.
import boltons

boltons.iterutils.unique([1,2,3,4,5,6,1,2])
# [1, 2, 3, 4, 5, 6]

list(boltons.iterutils.unique_iter(['hi', 'hello', 'ok', 'bye', 'yes'], key=lambda x: len(x)))
# ['hi', 'hello', 'bye']

1 > 2
# False
2 > 2
# False
# 3 > 2
True

boltons.iterutils.unique([1,2,3,4,5,6], key=lambda x : x > 2)
# [1, 3]
boltons.iterutils.redundant(src, key=None, groups=False)
The complement of unique()
redundant docs
By default returns non-unique/duplicate values as a list of the first redundant value in src. Pass groups=True to get groups of all values with redundancies, ordered by position of the first redundant value. This is useful in conjunction with some normalizing key function.
from boltons.iterutils import redundant

redundant([1, 2, 3, 4])
# []
redundant([1, 2, 3, 2, 3, 3, 4])
# [2, 3]
redundant([1, 2, 3, 2, 3, 3, 4], groups=True)
# [[2, 2], [3, 3, 3]]


# An example using a key function to do case-insensitive redundancy detection.

redundant(['hi', 'Hi', 'HI', 'hello'], key=str.lower)
# ['Hi']

redundant(['hi', 'Hi', 'HI', 'hello'], groups=True, key=str.lower)
# [['hi', 'Hi', 'HI']]

pywhat (What Even Is This File?)

Ever get a weird file, string, or hex blob and wonder what on earth it is?

from pywhat import whatis

print(whatis("d41d8cd98f00b204e9800998ecf8427e"))
# Output: MD5 hash of an empty string
  • It identifies file types, encodings, and hashes like a forensic detective.
  • This library can detect 300+ patterns, from obscure hash algorithms to credit card numbers.
  • pywhat docs

Playwright — Automating the Web

Problem: Logging into portals and downloading files manually. Every. Single. Day. \
Project: Auto-login bot.

Steps
Use Playwright to launch Chromium in headless mode.
Log into your account.
Download the latest statement automatically.
# pip install playwright
# pip install pytest-playwright (optional)

# Install the required browsers:
# playwright install

from playwright.sync_api import sync_playwright
import re

with sync_playwright() as p:
    browser = p.firefox.launch(headless=False, slow_mo=5000)
    page = browser.new_page()
    page.goto("https://playwright.dev")
    print(page.title())
    browser.close()

The best part? It doesn’t break every time the site sneezes (looking at you, Selenium 👀).

PyAutoGUI: Automate Your Computer Like a Hacker

Ever wanted your script to literally move your mouse, type into apps, or take screenshots? That’s PyAutoGUI.

import pyautogui

pyautogui.moveTo(500, 500)
pyautogui.click()
pyautogui.typewrite("Automation is fun!")

SUBSCRIBE FOR NEW ARTICLES

@
comments powered by Disqus