Skip to content

Commit

Permalink
Add parameter copy helper.
Browse files Browse the repository at this point in the history
  • Loading branch information
riga committed Dec 17, 2022
1 parent 2c9321a commit 118f977
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions law/patches.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import re
import functools
import copy
import logging

import luigi
Expand Down Expand Up @@ -56,6 +57,7 @@ def patch_all():
patch_keepalive_run()
patch_cmdline_parser()
patch_interface_logging()
patch_parameter_copy()

logger.debug("applied all law-specific luigi patches")

Expand Down Expand Up @@ -362,3 +364,36 @@ def _default(cls, opts):
luigi.setup_logging.InterfaceLogging._default = classmethod(_default)

logger.debug("patched luigi.setup_logging.InterfaceLogging._default")


def patch_parameter_copy():
"""
Patches ``luigi.parameter.Parameter`` to add a convenience methods that allows to copy parameter
instances and assigning new attributes such as descriptions or default values. This same
functionality will eventually be moved to luigi, but the patch might be kept for versions of
luigi where it was not addded yet.
"""
def _copy(self, add_default_to_description=False, **kwargs):
# copy the instance
inst = copy.copy(self)

# kwargs should in general match those accepted in the constructor, which are mostly saved
# as instance attributes using the same name, except in some cases which must be redirected
if "default" in kwargs and "_default" not in kwargs:
kwargs["_default"] = kwargs.pop("default")
if "config_path" in kwargs and "_config_path" not in kwargs:
kwargs["_config_path"] = kwargs.pop("config_path")

# overwrite attributes
inst.__dict__.update(kwargs)

# amend the description
if add_default_to_description:
prefix = "; " if inst.description else ""
inst.description += "{}default: {}".format(prefix, inst._default)

return inst

luigi.parameter.Parameter.copy = _copy

logger.debug("patched luigi.parameter.Parameter.copy")

0 comments on commit 118f977

Please sign in to comment.