Last active
September 14, 2026 19:13
-
-
Save Cadair/4cf980433d42f1b97bfb1415675f85c5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import inspect | |
| from functools import wraps | |
| def old_to_new_converter(old_init): | |
| """ | |
| Wraps an old init to call it as a new init. | |
| """ | |
| @wraps(old_init) | |
| def wrapper( | |
| self, | |
| data, | |
| wcs=None, | |
| uncertainty=None, | |
| mask=None, | |
| meta=None, | |
| unit=None, | |
| copy=False, | |
| psf=None, | |
| **kwargs, | |
| ): | |
| kwargs.update({"wcs": wcs, "uncertainty": uncertainty, "mask": mask, "unit": unit, "copy": copy, "psf": psf}) | |
| return old_init(self, data=data, header=meta, **kwargs) | |
| return wrapper | |
| def new_to_old_converter(new_init): | |
| """ | |
| Wraps an new init to call it as a old init. | |
| """ | |
| @wraps(new_init) | |
| def wrapper( | |
| self, | |
| data, | |
| header, | |
| plot_settings=None, | |
| **kwargs, | |
| ): | |
| # TODO Plot settings?? | |
| return new_init(self, data=data, meta=header, **kwargs) | |
| return wrapper | |
| class GenericMapDeprecationMeta(type): | |
| def __new__(mcs, name, bases, namespace, **kwargs): | |
| original_init = namespace.get('__init__') | |
| sig = inspect.signature(original_init) | |
| if "header" in sig.parameters: | |
| bases = (GenericMapInitTranslator,) + bases | |
| namespace['__init__'] = old_to_new_converter(original_init) | |
| return super().__new__(mcs, name, bases, namespace) | |
| class GenericMap(metaclass=GenericMapDeprecationMeta): | |
| def __init__(self, data, wcs=None, uncertainty=None, mask=None, meta=None, | |
| unit=None, copy=False, psf=None, *, extra_coords=None, global_coords=None, **kwargs): | |
| print(f"Got {data=}, {wcs=}, {meta=}") | |
| class GenericMapInitTranslator(GenericMap): | |
| __init__ = new_to_old_converter(GenericMap.__init__) | |
| class UpdatedSubClass(GenericMap): | |
| def __init__(self, data, wcs, meta): | |
| super().__init__(data, wcs, meta) | |
| class OldSubClass(GenericMap): | |
| def __init__(self, data, header, plot_settings=None, **kwargs): | |
| super().__init__(data, header, plot_settings, **kwargs) | |
| data = [1, 2, 3] | |
| meta = {"a": 1} | |
| wcs = None | |
| print("Updated") | |
| UpdatedSubClass(data, wcs, meta) | |
| # print("old called old") | |
| # OldSubClass(data, meta) | |
| print("old called new") | |
| OldSubClass(data, wcs, meta) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment