Right now I have one function that would be useful in a number of distinct packages that I work on. The function is only a handful of lines. But I would like to be able to use this code in a number of packages/projects that I work on and are deployed, I would like this code to be version controlled etc. There isn't, for example, one package that all the other packages already have as a requirement, otherwise I could put this code inside of that one and import it that way.
During my time coding I've come across this issue a couple times. For concreteness some functions f
that have this characteristic might be:
- A wrapper or context manager which times a block of code with some log statements
- A function which divides a range of integers into as small number of evenly spaced strides while not exceeding a maximum number of steps
- A function which converts a center value and a span into a lower and upper limit
Basically the options I see are:
- Put the code
f
in one of the existing packagesR
and then make any packageA
that wants to usef
haveR
as a requirement. The downside here is thatA
may require nothing fromR
other thanf
in which case most of the requirement is wasteful. - Copy the code
f
into every packageA
that requires it. One question that comes up is then where shouldf
live withinA
? Because the functionality off
is really outside the scope of packageA
does. This is sort of a minor problem, the bigger problem is that if an improvement is made tof
in one package it would be challenging to maintain uniformity across multiple packages that havef
in them. - I could make an entire package
F
dedicated to functions likef
and import it into each package that needsf
. This seems like technically the best approach from a requirements management and separation of responsibility management perspective. But like I said, right now this would be an entire package dedicated to literally one function with a few lines of code. - If there is a stdlib function that has the functionality I want I should definitely use that. If there is not, I may be able to find a 3rd party package that has the functionality I want, but this brings about a zoo of other potential problems that I'd prefer to avoid.
What would be the suggested way to do this? Are there other approaches I haven't mentioned?