astro py:docs

- Index
- Modules

### Navigation

- next »
- « previous |
- Astropy v5.2.1 »
- Astropy Core Package Utilities (`astropy.utils`) »
- enable_merge_strategies

# enable_merge_strategies

astropy.utils.metadata.enable_merge_strategies(*merge_strategies)

  Context manager to temporarily enable one or more custom metadata merge strategies.

  Parameters:
    *merge_strategies `MergeStrategy`

      Merge strategies that will be enabled.

  Examples

  Here we define a custom merge strategy that takes an int or float on the left and right sides and returns a list with the two values.

      >>> from astropy.utils.metadata import MergeStrategy
      >>> class MergeNumbersAsList(MergeStrategy):
      ...     types = ((int, float),  # left side types
      ...              (int, float))  # right side types
      ...     @classmethod
      ...     def merge(cls, left, right):
      ...         return [left, right]

  By defining this class the merge strategy is automatically registered to be available for use in merging. However, by default new merge strategies are not enabled. This prevents inadvertently changing the behavior of unrelated code that is performing metadata merge operations.

  In order to use the new merge strategy, use this context manager as in the following example:

      >>> from astropy.table import Table, vstack
      >>> from astropy.utils.metadata import enable_merge_strategies
      >>> t1 = Table([[1]], names=['a'])
      >>> t2 = Table([[2]], names=['a'])
      >>> t1.meta = {'m': 1}
      >>> t2.meta = {'m': 2}
      >>> with enable_merge_strategies(MergeNumbersAsList):
      ...    t12 = vstack([t1, t2])
      >>> t12.meta['m']
      [1, 2]

  One can supply further merge strategies as additional arguments to the context manager.

  As a convenience, the enabling operation is actually done by checking whether the registered strategies are subclasses of the context manager arguments. This means one can define a related set of merge strategies and then enable them all at once by enabling the base class. As a trivial example, all registered merge strategies can be enabled with:

      >>> with enable_merge_strategies(MergeStrategy):
      ...    t12 = vstack([t1, t2])