gemseo / core / grammars

Hide inherited members

defaults module

The grammar default values.

class gemseo.core.grammars.defaults.Defaults(grammar, data)[source]

Bases: DisciplineData

A class for handling grammar default values.

A dictionary-like interface to bind grammar names to default values. The namespace settings of the grammar are taken into account.

Examples

>>> from gemseo.core.discipline_data import DisciplineData
>>> import numpy as np
>>> import pandas as pd
>>> data = {
...     "x": 0,
...     "y": pd.DataFrame(data={"a": np.array([0])}),
... }
>>> disc_data = DisciplineData(data)
>>> disc_data["x"]
0
>>> disc_data["y"]
   a
0  0
>>> # DataFrame content can be accessed with the ~ separator.
>>> disc_data["y~a"]
array([0])
>>> # New columns can be inserted into a DataFrame with the ~ separator.
>>> disc_data["y~b"] = np.array([1])
>>> data["y"]["b"]
0    1
Name: b, dtype: int64
>>> # New DataFrame can be added.
>>> disc_data["z~c"] = np.array([2])
>>> data["z"]
   c
0  2
>>> type(data["z"])
<class 'pandas.core.frame.DataFrame'>
>>> # DataFrame's columns can be deleted.
>>> del disc_data["z~c"]
>>> data["z"]
Empty DataFrame
Columns: []
Index: [0]
>>> # Iterating is only done over the exposed keys.
>>> list(disc_data)
['x', 'y~a', 'y~b']
>>> # The length is consistent with the iterator.
>>> len(disc_data)
3

Initialize self. See help(type(self)) for accurate signature.

Parameters:
clear() None.  Remove all items from D.
Return type:

None

copy(keys=(), with_namespace=True)

Create a shallow copy.

Parameters:
  • keys (Iterable[str]) –

    The names of the items to keep, if empty then keep them all.

    By default it is set to ().

  • with_namespace (bool) –

    Whether to the keys are prefixed with the namespace.

    By default it is set to True.

Returns:

The shallow copy.

Return type:

DisciplineData

get(k[, d]) D[k] if k in D, else d.  d defaults to None.
items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised.

popitem() (k, v), remove and return some (key, value) pair

as a 2-tuple; but raise KeyError if D is empty.

rename(name, new_name)[source]

Rename a name.

Parameters:
  • name (str) – The current name.

  • new_name (str) – The new name.

Return type:

None

restrict(*keys)

Remove all but the given keys.

Parameters:

*keys (str) – The keys of the elements to keep.

Return type:

None

setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D
update(other, exclude=())

Update from another mapping but for some keys.

Parameters:
  • other (Mapping[str, Any]) – The data to update from.

  • exclude (Iterable[str]) –

    The keys that shall not be updated.

    By default it is set to ().

Return type:

None

values() an object providing a view on D's values
SEPARATOR = '~'

The character used to separate the shared dict key from the column of a pandas DataFrame.