bmcfee / presets Goto Github PK
View Code? Open in Web Editor NEWA python module to manipulate default parameters of a module's functions
License: ISC License
A python module to manipulate default parameters of a module's functions
License: ISC License
Currently, the function decorator fails to preserve the docstring and call signature of the wrapped function.
While full call signature preservation is impossible with dynamically adapting default parameters, we could at least trap and modify the docstring so as to not be completely opaque.
The decorator package should be useful here.
Nice package! I've been using something similar with Sacred
and was wondering if I could use something similar to change a package's defaults. And then I stumbled across this!
So I'm thinking about changing the defaults of librosa program-wide, but seeing as I need to wrap it, I need to import everything through wherever I define the Preset.
This is only a minor inconvenience so it's not a big deal, but I was wondering if there would be a way to tap into python's import system to override it everywhere.
import librosa
from presets import patch_preset
def patch_preset(module):
import sys
sys.modules[module.__name__] = Preset(module)
patch_preset(librosa)
# testing
del librosa
import librosa
print(librosa) # it's a Patch
import librosa.display # ModuleNotFoundError: No module named 'librosa.display'; 'librosa' is not a package
It doesn't quite work because it breaks subsequent imports of submodules, which makes sense, so I think we'd have to extend Python's Module
class (I believe I've seen it under ast
? but the docs are lousy) so that it can still read a Patch as a module.
Anyways, I'm not saying that we have to do anything about this, but I thought it could be a cool/convenient feature.
Bonus: I wonder if we could use Python's Import Hooks to do something like this:
# __init__.py
from presets import librosa # overrides sys.modules
librosa.update(...)
# some_scripy.py
import librosa # Preset
not even sure if that's a good idea. but it's an idea ๐คทโโ๏ธ
I think awkward thing is that, if the Preset replacement is called after anywhere else imports the package, then they'd be using the original module reference. So maybe explicit is better.
Replace call to getargspec
with signature
. See here.
Presets currently inspects the wrapped function to check for keyword arguments to be propagated. This fails when the wrapped function has a **kwargs
, which causes presets to drop any anonymous keyword arguments.
This should be an easy fix.
In [7]: librosa = Preset(_librosa)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-f50f34fc9f9e> in <module>()
----> 1 librosa = Preset(_librosa)
/home/bmcfee/git/presets/presets/__init__.py in __init__(self, module, dispatch, defaults)
132 self._dispatch[value] = Preset(value,
133 dispatch=self._dispatch,
--> 134 defaults=self._defaults)
135
136 setattr(self, attr, self._dispatch[value])
/home/bmcfee/git/presets/presets/__init__.py in __init__(self, module, dispatch, defaults)
122 elif isinstance(value, types.ModuleType):
123 # test if this is a submodule of the current module
--> 124 submodpath = inspect.getfile(value)
125
126 if os.path.commonprefix([modpath, submodpath]) == modpath:
/home/bmcfee/miniconda/envs/py35/lib/python3.5/inspect.py in getfile(object)
604 if hasattr(object, '__file__'):
605 return object.__file__
--> 606 raise TypeError('{!r} is a built-in module'.format(object))
607 if isclass(object):
608 if hasattr(object, '__module__'):
TypeError: <module 'sys' (built-in)> is a built-in module
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.