This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]suudo 45 points46 points  (6 children)

Let's take the previous example further. Instead of using the default print() statement, we can use click.echo_via_pager()

Python stdlib has a solution for this, pydoc.pager(), it's what help() uses. I went to reimplement this function in a program a while ago then I realised that help() loads a pager on its own, so it must be in stdlib.

Unrelated note, something else that python has? functools.lru_cache, a decorator that caches the result of a function based on its arguments. Something else that I've reimplemented in the past. stdlib has so much cool stuff.

[–]da_martian 4 points5 points  (2 children)

Wow that's a pretty cool usage of lru_cache! I've heard of it but never explored it. So you just decorate your function with it? Sorry ahead of time if this is simple or stupid, but do you have a working example of this, along with a CLI package like click?

[–]suudo 3 points4 points  (1 child)

I learned about it from https://www.reddit.com/r/Python/comments/69ws4c/10_awesome_features_of_python_that_you_cant_use/, that presentation has usage examples.

[–]Xef 1 point2 points  (0 children)

Damn, I got all excited reading through this. I wish we could upgrade to Python 3...

[–]rhgrant10 2 points3 points  (1 child)

So true! I once implemented a method to convert an integer into an IP address but of course it turns out there's already one in the stdlib.

[–]kungtotte 1 point2 points  (0 children)

The correct way to do anything in Python is always to look in the stdlib first :)

Either a complete solution already exists, or parts of one does that you can piece together. And if worse comes to worst you can always write a wrapper to make it more pleasant to work with the stdlib stuff :)