MatchedQuantity

class PlasmaCalcs.quantities.quantity_tools.MatchedQuantity(var, loadable=None, *, _match=None, fname=None, deps=UNSET, attr_deps=UNSET, value_deps=UNSET, dims=UNSET, ignores_dims=UNSET, reduces_dims=UNSET, **kw_super)

Bases: object

LoadableQuantity matched to a var, which can be loaded from a QuantityLoader.

var: str

the var matched to this object.
loadable: LoadableQuantity or None
the LoadableQuantity associated with this MatchedQuantity instance.
None –> create one from fname, deps, dims, ignores_dims, and reduces_dims,
using defaults if UNSET. (however, fname must be provided in this case.)
_match: result of re.match
the result of pattern.fullmatch(var) for the pattern associated with this object.
None –> no pattern match; either no associated pattern or var is not a match.
self.fname aliases to self.loadable.fname.
similarly for deps, attr_deps, value_deps, dims, ignores_dims, reduces_dims.
also similarly for self.get_f and self.get_f_module.

Methods

__getitem__(key)

returns self.{key}, if key in self.keys().

all_deps([quantity_loader, missing_ok])

returns list of deps, based on quantity_loader and self.deps, self.attr_deps, and self.value_deps.

dep_vars([quantity_loader, missing_ok])

returns list of dep var strings or 2-tuples of (str, using)

groups(*[, default])

return self._match.groups().

items()

returns (key, self[key]) for key in self.keys().

keys()

returns keys whose values can be accessed via self[key].

load_value(quantity_loader, *args, **kw)

actually get the value of this matched var, from the quantity loader.

_dep_to_strs(dep[, quantity_loader])

convert dep to list of strings (possibly only 1 string),

_list_attr_deps([quantity_loader, missing_ok])

returns list of deps, based on quantity_loader and self.attr_deps.

_list_value_deps([quantity_loader])

returns list of deps, based on quantity_loader and self.value_deps.

_repr_contents()

return contents for __repr__

Attributes

attr_deps

alias to self.loadable.attr_deps

deps

alias to self.loadable.deps

dims

alias to self.loadable.dims

fname

alias to self.loadable.fname

get_f

alias to self.loadable.get_f

get_f_module

alias to self.loadable.get_f_module

ignores_dims

alias to self.loadable.ignores_dims

reduces_dims

alias to self.loadable.reduces_dims

value_deps

alias to self.loadable.value_deps

__getitem__(key)

returns self.{key}, if key in self.keys().

_dep_to_strs(dep, quantity_loader=None)

convert dep to list of strings (possibly only 1 string),

following the logic described in self.dep_vars.__doc__.
_list_attr_deps(quantity_loader=None, *, missing_ok=False)

returns list of deps, based on quantity_loader and self.attr_deps.

Each dep in result is a str, int, tuple, dict, or callable.
quantity_loader must be provided if any attr_deps are provided.
missing_ok: bool
whether to return [] for attr_dep when quantity_loader is a class and attr is a property.
False –> crash with InputError in that case.
_list_value_deps(quantity_loader=None)

returns list of deps, based on quantity_loader and self.value_deps.

Each dep in result is a str, int, tuple, dict, or callable.
quantity_loader must be provided if any value_deps are provided.
Note: includes the “values” as deps too, not just the lookup dicts.
E.g. if value_deps = [(‘var0’: {‘val0’: 0, ‘val1’: [1, ‘b’]})],
and quantity_loader(‘var0’) gives xr.DataArray([‘val1’, ‘val1’]),
result here will be [‘var0’, 1, ‘b’].
_repr_contents()

return contents for __repr__

all_deps(quantity_loader=None, *, missing_ok=False)

returns list of deps, based on quantity_loader and self.deps, self.attr_deps, and self.value_deps.

Each dep in result is a str, int, tuple, dict, or callable.
See self.dep_vars() to convert list of deps to list of strs.
If any attr_deps or value_deps are provided, quantity_loader must be provided.
missing_ok: bool
whether to return [] for attr_dep when quantity_loader is a class and attr is a property.
False –> crash with InputError in that case.
property attr_deps

alias to self.loadable.attr_deps

dep_vars(quantity_loader=None, *, missing_ok=False)

returns list of dep var strings or 2-tuples of (str, using)

These are the var names associated with self.deps, self.attr_deps, and self.value_deps.
Each string will be a quant that QuantityLoader might load;
might be a KNOWN_VAR or might be a KNOWN_PATTERN.
(However, this method doesn’t know whether the deps are actually loadable).
Each 2-tuple has a string like above,
and a dict of values essential to set via quantity_loader.using(**dict),
when trying to determine the deps of that string,
e.g. during quantity_loader.match_var_tree().
(If it’s not essential to set values, use string instead of 2-tuple.)
self.deps is a list of str/int/tuple/dict/callable
list of dependencies for this quant. Each dependency is one of the following:
- a str, indicating a known var or pattern name,
- an int, indicating a group index from a pattern match,
- a 2-tuple of (non-tuple-dep, dict),
where dict tells any values essential to set via quantity_loader.using(**dict),
in order to properly determine deps.
E.g. (‘n’, {‘fluid’: ELECTRON}) for ‘depends on n when self.fluid=electron’.
Note: mostly intended for internal use (in value_deps). It’s usually preferable to
just define a different variable, e.g. ‘ne’ which will set values appropriately.
- a dict with length 1, with
key = int or tuple of ints, specifying which groups are relevant.
dep will utilize info about groups = _match.groups().
val = str, or callable f -> str or iterable.
str –> val.format(**group_info), where
group_info = {f’group{i}’: groups[i] for i in key},
replacing groups[i] with ‘’ if groups[i] is None.
callable f –> f(group_info), where
group_info = {i: groups[i] for i in key}
- a callable of the form f(quantity_loader, var, groups) -> str or iterable,
which tells the var name(s) associated with this dependency,
where: quantity_loader is a QuantityLoader instance,
var is the matched var,
and groups=_match.groups() if _match provided else None.
self.attr_deps is a list of 2-tuples of (str, dict or str)
list of attr-based dependencies for this quant. Each dependency is:
(attr, lookup), where lookup.get(v, []) tells str or list of deps associated with v,
where v=quantity_loader.attr.
if ‘__default__’ in lookup, use lookup.get(v, lookup[‘__default__’]) instead.
str lookup –> lookup = getattr(quantity_loader, lookup).
self.value_deps is a list of 2-tuples of (str, dict or str)
list of value-based dependencies for this quant. Each dependency is:
(var, lookup), where lookup.get(val, []) tells str or list of deps associated with val,
for all unique vals in quantity_loader(var).
Each dep associated with val may be str/int/callable, similar to deps.
if ‘__default__’ in lookup, use lookup.get(v, lookup[‘__default__’]) instead.
str lookup –> lookup = getattr(quantity_loader, lookup).
quantity_loader: None or QuantityLoader
if any dep is callable, quantity_loader must be provided,
and is used to get string for dep via dep(quantity_loader, self.var, self.groups(default=None))
if any attr_deps or value_deps are provided, quantity_loaded must be provided,
and is used to convert to list of deps based on attr or values.
missing_ok: bool
whether to be lenient sometimes when missing details that would allow to fully determine deps.
E.g., if quantity_loader is a class (e.g. dep_vars called inside QuantityLoader.cls_var_tree()),
and an attr_dep’s attr’s value is a property, ignore that attr_dep if missing_ok, else crash.
property deps

alias to self.loadable.deps

property dims

alias to self.loadable.dims

property fname

alias to self.loadable.fname

property get_f

alias to self.loadable.get_f

property get_f_module

alias to self.loadable.get_f_module

groups(*, default=UNSET)

return self._match.groups().

default: UNSET or any object
if _match is None, return default if provided (not UNSET), else raise TypeError.
property ignores_dims

alias to self.loadable.ignores_dims

items()

returns (key, self[key]) for key in self.keys().

keys()

returns keys whose values can be accessed via self[key].

i.e.: (‘var’, ‘loadable’, ‘_match’, ‘fname’,
‘deps’, ‘attr_deps’, ‘value_deps’,
‘dims’, ‘ignores_dims’, ‘reduces_dims’).
load_value(quantity_loader, *args, **kw)

actually get the value of this matched var, from the quantity loader.

uses getter = self.get_f(quantity_loader);
if _match is None, calls getter(*args, **kw);
else, calls getter(self.var, *args, _match=_match, **kw).
property reduces_dims

alias to self.loadable.reduces_dims

property value_deps

alias to self.loadable.value_deps