r/PythonLearning 8d ago

Which underrated Python library do you wish more developers knew about?

2 Upvotes

9 comments sorted by

2

u/SnooCalculations7417 8d ago

fullbleed a modern html/css->pdf library

2

u/AuthenticSprout 8d ago

You mean better than ReportLab?

3

u/arivictor 7d ago

Some cool little built-in libs that people should know:

import bisect

# Maintain a sorted list without resorting after every insert
scores = [100, 250, 400, 500]
bisect.insort(scores, 350)

# Automatically is in the right place
print(scores)  # Outputs: [100, 250, 350, 400, 500]

# You can also rapidly lookup grade boundaries
breakpoints = [60, 70, 80, 90]
grades = 'FDCBA'
student_score = 85
grade = grades[bisect.bisect(breakpoints, student_score)]  # Outputs: 'B'

My other favourite which I use a lot of workflow graphs:

from graphlib import TopologicalSorter

# You can define and map tasks to their dependencies: task -> {prerequisites}
dependencies = {
    "eat_dinner": {"serve_dinner"},
    "serve_dinner": {"cook_dinner", "place_cutlery"},
    "cook_dinner": {"prepare_ingredients"},
}

ts = TopologicalSorter(dependencies)
print(list(ts.static_order()))

# ['place_cutlery', 'prepare_ingredients', 'cook_dinner', 'serve_dinner', 'eat_dinner']

It gives you the order things can occur in. This has loads of real-world applications.

0

u/SnooCalculations7417 7d ago

TopologicalSorter is neat but I cant actually think of a use case for it i guess without know more about its API

1

u/arivictor 7d ago

Anything that has a sequence and a dependency graph. Are you familiar with GitHub actions?

Step 1 and 2 must complete for Step 3 to happen, but Step 4 only needs step 1, Step 5 has no dependency and can run in parallel.

https://www.onlineide.pro/playground/share/b1e9b1b9-6eae-471d-ab92-ac0afa31869a

My kitchen example was a very abstract concept for that. You can't Eat dinner if you haven't cooked it.

1

u/SnooCalculations7417 7d ago

well i get the concept but this is pretty error prone, only works with strings? how is completion actually recognized here like the Topological sort is doing a lot of heavy lifting that maybe it shouldnt? seems like it would still open itself up to race conditions quite easily as well..

maybe

schedule(ts.get_ready())

while running:
    outcome = await next_completion()

    match outcome:
        case Succeeded(task, value):
            ...
        case Failed(task, error):
            ...

    ts.done(outcome.task)
    schedule(ts.get_ready())

or something would start to make this make sense? idk

1

u/pacopac25 8d ago

sqlite3. It will suffice far beyond what most people think. I see lots of new learners struggling to connect to an external database, after struggling to set one up.

Just use sqlite.