iode.Identities.__setitem__
- Identities.__setitem__(key: str | List[str], value: str | Identity | List[str | Identity])[source]
Update/add a (subset of) identity(ies) referenced by key from/to the Identities database.
The key can represent a single object name (e.g. “ACAF”) or a list of object names (“ACAF;ACAG;AOUC”) or a pattern (e.g. “A*”) or a list of sub-patterns (e.g. “A*;*_”).
If the key represents a list of object names or of sub-patterns, each name or sub-pattern is separated by a separator character which is either a whitespace ` , or a comma `,, or a semi-colon ;, or a tabulation t, or a newline n.
A (sub-)`pattern` is a list of characters representing a group of object names. It includes some special characters which have a special meaning:
* : any character sequence, even empty
? : any character (one and only one)
@ : any alphanumerical char [A-Za-z0-9]
& : any non alphanumerical char
| : any alphanumeric character or none at the beginning and end of a string
! : any non-alphanumeric character or none at the beginning and end of a string
`` : escape the next character
Note that the key can contain references to IODE lists which are prefixed with the symbol $.
- Parameters:
- key: str or list(str)
(the list of) name(s) of the identity(ies) to update/add. The list of identities to update/add can be specified by a pattern or by a list of sub-patterns (e.g. “A*;*_”).
- value: str, Identity, dict(str, str or Identity) or pd.Series(str) or Identities
(new) identity(ies) value(s).
Examples
>>> import pandas as pd >>> from iode import SAMPLE_DATA_DIR >>> from iode import identities >>> identities.load(f"{SAMPLE_DATA_DIR}/fun.idt") Loading .../fun.idt 48 objects loaded
>>> # a) add one identity >>> identities["BDY"] = "YN - YK" >>> identities["BDY"] Identity('YN - YK')
>>> # b) update one identity >>> identities["AOUC"] Identity('((WCRH/QL)/(WCRH/QL)[1990Y1])*(VAFF/(VM+VAFF))[-1]+PM*(VM/(VM+VAFF))[-1]') >>> identities["AOUC"] = '(WCRH / WCRH[1990Y1]) * (VAFF / (VM+VAFF))[-1] + PM * (VM / (VM+VAFF))[-1]' >>> identities["AOUC"] Identity('(WCRH / WCRH[1990Y1]) * (VAFF / (VM+VAFF))[-1] + PM * (VM / (VM+VAFF))[-1]') >>> # or equivalently >>> idt = identities["AOUC"] >>> idt.lec = "(WCRH / WCRH[1990Y1]) * (VAFF / (VM+VAFF))[-1]" >>> identities["AOUC"] Identity('(WCRH / WCRH[1990Y1]) * (VAFF / (VM+VAFF))[-1]')
>>> # c) add/update several identities at once >>> # 1) using a dict of values >>> values = {"GAP2": "0.9 * 100*(QAFF_/(Q_F+Q_I))", "GAP_": "0.9 * 100*((QAF_/Q_F)-1)", ... "GOSFR": "0.9 * (GOSF/VAF_)"} >>> identities["GAP2, GAP_, GOSFR"] = values >>> identities["GAP2, GAP_, GOSFR"] Workspace: Identities nb identities: 3 filename: ...fun.idt name identities GAP2 0.9 * 100*(QAFF_/(Q_F+Q_I)) GAP_ 0.9 * 100*((QAF_/Q_F)-1) GOSFR 0.9 * (GOSF/VAF_)
>>> # 2) using a pandas series >>> data = ["0.8 * 100*(QAFF_/(Q_F+Q_I))", "0.8 * 100*((QAF_/Q_F)-1)", "0.8 * (GOSF/VAF_)"] >>> series = pd.Series(data, index=["GAP2", "GAP_", "GOSFR"]) >>> identities["GAP2, GAP_, GOSFR"] = series >>> identities["GAP2, GAP_, GOSFR"] Workspace: Identities nb identities: 3 filename: ...fun.idt name identities GAP2 0.8 * 100*(QAFF_/(Q_F+Q_I)) GAP_ 0.8 * 100*((QAF_/Q_F)-1) GOSFR 0.8 * (GOSF/VAF_)
>>> # 3) using another Identities database (subset) >>> identities_subset = identities["GAP2, GAP_, GOSFR"].copy() >>> identities_subset["GAP2"] = "0.7 * 100*(QAFF_/(Q_F+Q_I))" >>> identities_subset["GAP_"] = "0.7 * 100*((QAF_/Q_F)-1)" >>> identities_subset["GOSFR"] = "0.7 * (GOSF/VAF_)" >>> identities["GAP2, GAP_, GOSFR"] = identities_subset >>> identities["GAP2, GAP_, GOSFR"] Workspace: Identities nb identities: 3 filename: ...fun.idt name identities GAP2 0.7 * 100*(QAFF_/(Q_F+Q_I)) GAP_ 0.7 * 100*((QAF_/Q_F)-1) GOSFR 0.7 * (GOSF/VAF_)
>>> # d) working on a subset >>> # 1) get subset >>> identities_subset = identities["X*"] >>> identities_subset.names ['XEX', 'XNATY', 'XPOIL', 'XPWMAB', 'XPWMS', 'XPWXAB', 'XPWXS', 'XQWXAB', 'XQWXS', 'XQWXSS', 'XRLBER', 'XTFP', 'XW'] >>> # 2) add an identity to the subset >>> identities_subset["XDPU"] = "grt DPU" >>> identities_subset["XDPU"] Identity('grt DPU') >>> # --> new identity also appears in the global workspace >>> "XDPU" in identities True >>> identities["XDPU"] Identity('grt DPU') >>> # 3) update an identity in the subset >>> identities_subset["XDPU"] = "0" >>> identities_subset["XDPU"] Identity('0') >>> # --> identity is also updated in the global workspace >>> identities["XDPU"] Identity('0')