Ask HN: What are some unusual but useful Python libraries you've discovered?

by alexliu518on 7/31/2024, 6:43 AMwith 18 comments

Hey everyone! I'm always on the lookout for new and interesting Python libraries that might not be well-known but are incredibly useful. Recently, I stumbled upon Rich for beautiful console output and Pydantic for data validation, which have been game-changers for my projects. What are some of the lesser-known libraries you've discovered that you think more people should know about? Share your favorites and how you use them!

by networkedon 7/31/2024, 1:34 PM

Last year I posted a similar Ask HN; it got 11 comments: https://news.ycombinator.com/item?id=38505531. I asked, "What lesser-known Python libraries do you wish people knew about?" The suggestions there are worth looking up. Don't miss DiskCache (https://github.com/grantjenks/python-diskcache).

I really like xmltodict (https://github.com/martinblech/xmltodict). Despite the name, it works in both directions. It is the most ergonomic library I have used for creating XML. It has external type stubs: https://pypi.org/project/types-xmltodict/.

Since you have recently discovered Rich, you may want rich-argparse (https://github.com/hamdanal/rich-argparse). It colorizes argparse CLIs with little effort from the user.

DeepDiff (https://github.com/seperman/deepdiff) has helped me with testing. I needed to compare two nested data structures but ignore any differences in floats (timestamps). DeepDiff let me do it:

  diff = DeepDiff(
     run_session(config), run_session(config, force=True), exclude_types=(float,)
  )

  assert not diff
pzp (https://github.com/andreax79/pzp) is like fzf in pure Python to use in your programs. Keep in mind it is currently version 0.0.x. I have found bugs, but I think it is just cool that it exists.

by Crier1002on 7/31/2024, 9:59 AM

I use https://github.com/litl/backoff for configurable backoff + retry

context: OpenAI API used to be super flaky back in the early days, i needed to retry my requests quite frequently and i found this

by srhtftwon 8/4/2024, 6:55 PM

Happy user of Andrew Moffat's https://sh.readthedocs.io for over a decade.

by jmkron 7/31/2024, 7:00 AM

For audio analysis:

https://librosa.org/

Audio track seperation:

https://github.com/adefossez/demucs

demucs works pretty well.

by KolenChon 8/1/2024, 12:05 AM

Defopt generates cli from function interface: https://defopt.readthedocs.io/en/stable/features.html

The alternative is either you maintain two interfaces with boilerplate, or write a cli only if that’s the first priority.

Similar solutions exist, like fire. But fire’s cli is like an afterthought, in the sense it gives people a way to run things in command line where they already know how to run it from Python.

by hcfmanon 7/31/2024, 2:32 PM

Opensoundscape for sound localization

https://opensoundscape.org/en/latest/

by vismit2000on 8/1/2024, 6:10 AM

This has been an absolute game changer: https://github.com/pytries/marisa-trie (succinct trie with fast lookup and minimal RAM) Use case and more details in the wonderful book by Ian and Micha: High Performance Python

by skykeryon 7/31/2024, 7:15 AM

Exporting datasets - Tablib: https://tablib.readthedocs.io/en/stable/

by Qemon 7/31/2024, 8:55 PM

boltons: https://boltons.readthedocs.io/en/latest/

by Hasnepon 7/31/2024, 10:48 AM

pg8000 [1] is a Postgres library implemented in pure Python. I've spent enough time trying to get psycopg installed on MacOS and Docker that I appreciate just being able to pip install it at any time.

[1] https://github.com/tlocke/pg8000

by enjoyyourlifeon 8/5/2024, 12:24 AM

genson for creating JSON schemaa

by pizzaon 8/3/2024, 3:39 AM

- rich

- einops

- pytrees

- torchinfo

- joblib

- symbex

- nbdev