On 11Sep2020 23:09, The Nomadic Coder
oops, was not aware of "not every three-line function needs to be a built-in"
This came out personal frustration, as I use this 3 line function very, very often, and the whole community does. Still learning-to-navigate what's accepted here and what's not :)
DRY.
Why do not people use modules more often (I don't mean the stdlib, I
mean extra modules)?
My personal solution to this kind of thing is to keep a little
library/module of these 3 line things if I use them. Then you just
import stuff and use it.
2 trite examples:
I've got a cs.lex module with a bunch of little things in there for
parsing stuff - identifiers, quoted strings, etc etc; it's on PyPI so
using it elsewhere is trivial.
Closer to Nomadic's use case, I've got an @strable decorator, thus:
@strable
def func(f, ...):
... do something with an open file ...
It intercepts the first argument: if a str, it opens it as a file (by
default, you can provide an arbitrary function for the "open" action).
Then the function just has to work with a file (or whatever a str should
turn into, domain specific).
You could also write:
load_json = strable(json.load)
and be on your way.
This is also in a module (cs.deco), also on PyPI for reuse.
Not every three-line function needs to be a built-in, but for the
three-line functions _you_ use a lot, write them _once_ and import them
from your personal little module-of-three-line-functions.
No need to publish to PyPI (extra work) - it's as easy to keep them
locally unless you need them elsewhere. But don't rewrite - reuse!
Cheers,
Cameron Simpson