opendrift.models.openoil.adios.util.many_many

ManyMany

class for managing a many to many relationship in a pair of dicts

The two mappings are “left” and “right”

The left one is originally populated by the initializer.

After that they are equivalent – when you add something to one the other is updated.

Currently there is no way to remove anything

YOu can access copies of the mappings with:

ManyMany.left and ManyMany.right

They are copies, so that mutating them won’t break the internal data.

Module Contents

Classes

ManyMany

initialize a ManyMany structure

class opendrift.models.openoil.adios.util.many_many.ManyMany(initial_data=None)[source]

initialize a ManyMany structure

Parameters:

initial_data

initial data for the left dict. of the form: {key1: iterable_of_values,

key2: iterable_of_values, … }

all values must be hashable

property left

A copy of the left dict

It’s a copy, so it won’t change the internal ones if mutated

property right

A copy of the right dict

It’s a copy, so it won’t change the internal ones if mutated

_rebuild_right()[source]

rebuilds the right dict to match the left

_rebuild_left()[source]

rebuilds the left dict to match the right

static _rebuild(source)[source]

builds a “reversed” dict from a source dict

static _dict_hash(d)[source]

Provide a hash of a dict with a sequence of hashable items as the values

This was to be used to know if an internal dict was changed,

but it turns out that’s not useful (at least not in a robust way)

add_to_left(key, value)[source]

add a new value to the left dict

Parameters:
  • key – the key the value is to be added to

  • value – the value to be added

If the key is already there, the value will be added to the corresponding set

A new key and set will be created if it is not already there.

add_to_right(key, value)[source]

add a new value to the right dict

Parameters:
  • key – the key the value is to be added to

  • value – the value to be added

If the key is already there, the value will be added to the corresponding set

A new key and set will be created if it is not already there.