iode.Lists.__setitem__
- Lists.__setitem__(key: str | List[str], value: str | List[str])[source]
Update/add a (subset of) IODE list(s) referenced by key from/to the Lists 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 IODE list(s) to update/add. The list of lists to update/add can be specified by a pattern or by a list of sub-patterns (e.g. “A*;*_”).
- value: str or dict(str, str) or pd.Series or Lists
(new) IODE list(s) value(s).
Examples
>>> import pandas as pd >>> from iode import SAMPLE_DATA_DIR >>> from iode import lists, variables >>> lists.load(f"{SAMPLE_DATA_DIR}/fun.lst") Loading .../fun.lst 17 objects loaded >>> variables.load(f"{SAMPLE_DATA_DIR}/fun.var") Loading .../fun.var 394 objects loaded
>>> # a) add one list >>> # --- by passing a string >>> lists["A_VAR"] = "ACAF;ACAG;AOUC;AOUC_;AQC" >>> lists["A_VAR"] ['ACAF', 'ACAG', 'AOUC', 'AOUC_', 'AQC'] >>> # --- by passing a Python list >>> b_vars = variables.get_names("B*") >>> b_vars ['BENEF', 'BQY', 'BRUGP', 'BVY'] >>> lists["B_VAR"] = b_vars >>> lists["B_VAR"] ['BENEF', 'BQY', 'BRUGP', 'BVY']
>>> # b) update one list >>> # --- by passing a string >>> lists["A_VAR"] = "ACAF;ACAG;AOUC;AQC" >>> lists["A_VAR"] ['ACAF', 'ACAG', 'AOUC', 'AQC'] >>> # --- by passing a Python list >>> b_y_vars = variables.get_names("B*Y") >>> b_y_vars ['BQY', 'BVY'] >>> lists["B_VAR"] = b_y_vars >>> lists["B_VAR"] ['BQY', 'BVY']
>>> # c) add/update multiple lists at once >>> # 1) using a dict of values >>> values = {"ENVI": "PWMAB; PWMS; PWXAB; PWXS; QWXAB; QWXS; POIL; NATY", ... "IDT": "FLGR; KL; PROD; QL; RDEBT; RENT; RLBER; SBGX; WCRH; IUGR; SBGXR; WBGR; YSFICR", ... "MAINEQ": "NFYH; KNFF; PC; PXAB; PMAB; QXAB; QMAB"} >>> lists["ENVI, IDT, MAINEQ"] = values >>> lists["ENVI, IDT, MAINEQ"] Workspace: Lists nb lists: 3 filename: ...fun.lst description: Modèle fun name lists ENVI PWMAB; PWMS; PWXAB; PWXS; QWXAB; QWXS; POIL; NATY IDT FLGR; KL; PROD; QL; RDEBT; RENT; RLBER; SBGX; WCRH; IUGR; SBGXR; WBGR; YSFICR MAINEQ NFYH; KNFF; PC; PXAB; PMAB; QXAB; QMAB
>>> # 2) using a pandas series >>> data = ["PWMS; PWXAB; PWXS; QWXAB; QWXS; POIL", ... "KL; PROD; QL; RDEBT; RENT; RLBER; SBGX; WCRH; IUGR; SBGXR; WBGR", ... "KNFF; PC; PXAB; PMAB; QXAB"] >>> series = pd.Series(data, index=["ENVI", "IDT", "MAINEQ"]) >>> lists["ENVI, IDT, MAINEQ"] = series >>> lists["ENVI, IDT, MAINEQ"] Workspace: Lists nb lists: 3 filename: ...fun.lst description: Modèle fun name lists ENVI PWMS; PWXAB; PWXS; QWXAB; QWXS; POIL IDT KL; PROD; QL; RDEBT; RENT; RLBER; SBGX; WCRH; IUGR; SBGXR; WBGR MAINEQ KNFF; PC; PXAB; PMAB; QXAB
>>> # 3) using another Lists database (subset) >>> lists_subset = lists["ENVI, IDT, MAINEQ"].copy() >>> lists_subset["ENVI"] = "PWXAB; PWXS; QWXAB; QWXS" >>> lists_subset["IDT"] = "PROD; QL; RDEBT; RENT; RLBER; SBGX; WCRH; IUGR; SBGXR" >>> lists_subset["MAINEQ"] = "PC; PXAB; PMAB" >>> lists["ENVI, IDT, MAINEQ"] = lists_subset >>> lists["ENVI, IDT, MAINEQ"] Workspace: Lists nb lists: 3 filename: ...fun.lst description: Modèle fun name lists ENVI PWXAB; PWXS; QWXAB; QWXS IDT PROD; QL; RDEBT; RENT; RLBER; SBGX; WCRH; IUGR; SBGXR MAINEQ PC; PXAB; PMAB
>>> # d) working on a subset >>> # 1) get subset >>> lists_subset = lists["E*"] >>> lists_subset.names ['ENDO', 'ENDO0', 'ENDO1', 'ENVI'] >>> # 2) add a list to the subset >>> lists_subset["E_VAR"] = variables.get_names("E*") >>> lists_subset["E_VAR"] ['EFMY', 'EFXY', 'EX', 'EXC', 'EXCC', 'EXCCR'] >>> # --> new list also appears in the global workspace >>> "E_VAR" in lists True >>> lists["E_VAR"] ['EFMY', 'EFXY', 'EX', 'EXC', 'EXCC', 'EXCCR'] >>> # 3) update a list in the subset >>> lists_subset["E_VAR"] = "EX;EXC;EXCC;EXCCR" >>> lists_subset["E_VAR"] ['EX', 'EXC', 'EXCC', 'EXCCR'] >>> # --> list is also updated in the global workspace >>> lists["E_VAR"] ['EX', 'EXC', 'EXCC', 'EXCCR']