iode.Tables.__setitem__
- Tables.__setitem__(key: str | List[str], value: int | Dict[str, Any] | Table | List[int | Dict[str, Any] | Table])[source]
Update/add a (subset of) table(s) referenced by key from/to the Tables 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 table(s) to update/add. The list of tables to update/add can be specified by a pattern or by a list of sub-patterns (e.g. “A*;*_”).
- value: int, Table, dict(str, …) or Tables
If int, then it is interpreted as the number of columns to create a new empty table. If Table, then it is used to update an existing table or to create a new table if it does not exist yet.
See also
Examples
>>> from iode import SAMPLE_DATA_DIR >>> from iode import tables, Table, comments, lists, TableGraphAxis >>> comments.load(f"{SAMPLE_DATA_DIR}/fun.cmt") Loading .../fun.cmt 317 objects loaded >>> tables.load(f"{SAMPLE_DATA_DIR}/fun.tbl") Loading .../fun.tbl 46 objects loaded >>> lists.load(f"{SAMPLE_DATA_DIR}/fun.lst") Loading .../fun.lst 17 objects loaded
>>> # a) -------- new table -------- >>> # 1. specify list of line titles and list of LEC expressions >>> lines_titles = ["GOSG:", "YDTG:", "DTH:", "DTF:", "IT:", "YSSG+COTRES:", "RIDG:", "OCUG:"] >>> lines_lecs = ["GOSG", "YDTG", "DTH", "DTF", "IT", "YSSG+COTRES", "RIDG", "OCUG"] >>> tables["TABLE_CELL_LECS"] = {"nb_columns": 2, "table_title": "New Table", "lecs_or_vars": lines_lecs, ... "lines_titles": lines_titles, "mode": True, "files": True, "date": True} >>> tables["TABLE_CELL_LECS"] DIVIS | 1 | TITLE | "New Table" ----- | ---------------------------- CELL | | "#S" ----- | ---------------------------- CELL | "GOSG:" | GOSG CELL | "YDTG:" | YDTG CELL | "DTH:" | DTH CELL | "DTF:" | DTF CELL | "IT:" | IT CELL | "YSSG+COTRES:" | YSSG+COTRES CELL | "RIDG:" | RIDG CELL | "OCUG:" | OCUG ----- | ---------------------------- MODE | FILES | DATE | nb lines: 16 nb columns: 2 language: 'ENGLISH' gridx: 'MAJOR' gridy: 'MAJOR' graph_axis: 'VALUES' graph_alignment: 'LEFT'
>>> # 2. specify list of variables >>> vars_list = ["GOSG", "YDTG", "DTH", "DTF", "IT", "YSSG", "COTRES", "RIDG", "OCUG", "$ENVI"] >>> tables["TABLE_VARS"] = {"nb_columns": 2, "table_title": "New Table", "lecs_or_vars": vars_list, ... "mode": True, "files": True, "date": True} >>> tables["TABLE_VARS"] DIVIS | 1 | TITLE | "New Table" ----- | ------------------------------------------------------------------------------ CELL | | "#S" ----- | ------------------------------------------------------------------------------ CELL | "Bruto exploitatie-overschot: overheid (= afschrijvingen)." | GOSG CELL | "Overheid: geïnde indirecte belastingen." | YDTG CELL | "Totale overheid: directe belasting van de gezinnen." | DTH CELL | "Totale overheid: directe vennootschapsbelasting." | DTF CELL | "Totale indirecte belastingen." | IT CELL | "Globale overheid: ontvangen sociale zekerheidsbijdragen." | YSSG CELL | "Cotisation de responsabilisation." | COTRES CELL | "Overheid: inkomen uit vermogen." | RIDG CELL | "Globale overheid: saldo van de ontvangen lopendeoverdrachten." | OCUG CELL | "Wisselkoers van de USD t.o.v. de BEF (jaargemiddelde)." | EX CELL | "Index wereldprijs - invoer van niet-energieprodukten, inUSD." | PWMAB CELL | "Index wereldprijs - invoer van diensten, in USD." | PWMS CELL | "Index wereldprijs - uitvoer van niet-energieprodukten, inUSD." | PWXAB CELL | "Index wereldprijs - uitvoer van diensten, in USD." | PWXS CELL | "Indicator van het volume van de wereldvraag naar goederen,1985=1." | QWXAB CELL | "Indicator van het volume van de wereldvraag naar diensten,1985=1." | QWXS CELL | "Brent olieprijs (USD per barrel)." | POIL CELL | "Totale beroepsbevolking (jaargemiddelde)." | NATY CELL | "TFPFHP_" | TFPFHP_ ----- | ------------------------------------------------------------------------------ MODE | FILES | DATE | nb lines: 27 nb columns: 2 language: 'ENGLISH' gridx: 'MAJOR' gridy: 'MAJOR' graph_axis: 'VALUES' graph_alignment: 'LEFT'
>>> # b) -------- update table -------- >>> table = tables["TABLE_CELL_LECS"] >>> table DIVIS | 1 | TITLE | "New Table" ----- | ---------------------------- CELL | | "#S" ----- | ---------------------------- CELL | "GOSG:" | GOSG CELL | "YDTG:" | YDTG CELL | "DTH:" | DTH CELL | "DTF:" | DTF CELL | "IT:" | IT CELL | "YSSG+COTRES:" | YSSG+COTRES CELL | "RIDG:" | RIDG CELL | "OCUG:" | OCUG ----- | ---------------------------- MODE | FILES | DATE | nb lines: 16 nb columns: 2 language: 'ENGLISH' gridx: 'MAJOR' gridy: 'MAJOR' graph_axis: 'VALUES' graph_alignment: 'LEFT'
>>> # set graph axis type >>> table.graph_axis = TableGraphAxis.SEMILOG >>> # print first line >>> table[0] New Table >>> # print last line >>> table[-1] <DATE> >>> # delete last line >>> del table[-1] >>> # get index of line containing YSSG+COTRES >>> index = table.index("YSSG+COTRES") >>> index 9 >>> table[index] ('"YSSG+COTRES:"', 'YSSG+COTRES') >>> # get line type >>> table[index].line_type 'CELL' >>> # get line graph type >>> table[index].graph_type 'LINE' >>> # know if axis is left >>> table[index].axis_left True >>> # update cells >>> # double quotes " -> STRING cell >>> # no double quotes -> LEC cell >>> table[index] = ('"YSSG:"', 'YSSG') >>> table[index] ('"YSSG:"', 'YSSG') >>> # insert a new title line surrounded by two separator lines >>> table.insert(index + 1, '-') >>> table.insert(index + 2, "New Title") >>> table.insert(index + 3, '-') >>> # append a new sepatator line >>> table += '-'
>>> tables["TABLE_CELL_LECS"] DIVIS | 1 | TITLE | "New Table" ----- | -------------- CELL | | "#S" ----- | -------------- CELL | "GOSG:" | GOSG CELL | "YDTG:" | YDTG CELL | "DTH:" | DTH CELL | "DTF:" | DTF CELL | "IT:" | IT CELL | "YSSG:" | YSSG ----- | -------------- TITLE | "New Title" ----- | -------------- CELL | "RIDG:" | RIDG CELL | "OCUG:" | OCUG ----- | -------------- MODE | FILES | ----- | -------------- nb lines: 19 nb columns: 2 language: 'ENGLISH' gridx: 'MAJOR' gridy: 'MAJOR' graph_axis: 'SEMILOG' graph_alignment: 'LEFT'
>>> # d) working on a subset >>> # 1) get subset >>> tables_subset = tables["C8_*"] >>> tables_subset.names ['C8_1', 'C8_10', 'C8_11', 'C8_13', 'C8_14', 'C8_2', 'C8_3', 'C8_4', 'C8_5', 'C8_6', 'C8_7', 'C8_8', 'C8_9'] >>> # 2) add a table to the subset >>> vars_list = ["XNATY", "XPOIL", "XPWMAB", "XPWXAB"] >>> tables_subset["X_GRT"] = {"nb_columns": 2, "table_title": "Croissance", "lecs_or_vars": vars_list, ... "mode": True, "files": True, "date": True} >>> tables_subset["X_GRT"] DIVIS | 1 | TITLE | "Croissance" ----- | ---------------------------------------------------------------- CELL | | "#S" ----- | ---------------------------------------------------------------- CELL | "Croissance de la population active" | XNATY CELL | "Croissance du prix du pétrole" | XPOIL CELL | "Croissance des prix des biens importés" | XPWMAB CELL | "Croissance des prix des marchés pertinents à l'export" | XPWXAB ----- | ---------------------------------------------------------------- MODE | FILES | DATE | nb lines: 12 nb columns: 2 language: 'ENGLISH' gridx: 'MAJOR' gridy: 'MAJOR' graph_axis: 'VALUES' graph_alignment: 'LEFT' >>> # --> new table also appears in the global workspace >>> "X_GRT" in tables True >>> tables["X_GRT"] DIVIS | 1 | TITLE | "Croissance" ----- | ---------------------------------------------------------------- CELL | | "#S" ----- | ---------------------------------------------------------------- CELL | "Croissance de la population active" | XNATY CELL | "Croissance du prix du pétrole" | XPOIL CELL | "Croissance des prix des biens importés" | XPWMAB CELL | "Croissance des prix des marchés pertinents à l'export" | XPWXAB ----- | ---------------------------------------------------------------- MODE | FILES | DATE | nb lines: 12 nb columns: 2 language: 'ENGLISH' gridx: 'MAJOR' gridy: 'MAJOR' graph_axis: 'VALUES' graph_alignment: 'LEFT' >>> # 3) update a table in the subset >>> table_x_grt = tables_subset["X_GRT"] >>> index = table_x_grt.index("XPWXAB") >>> table_x_grt.insert(index + 1, (f'"{comments["XQWXSS"]}"', "XQWXSS")) >>> tables_subset["X_GRT"] = table_x_grt >>> tables_subset["X_GRT"] DIVIS | 1 | TITLE | "Croissance" ----- | ---------------------------------------------------------------- CELL | | "#S" ----- | ---------------------------------------------------------------- CELL | "Croissance de la population active" | XNATY CELL | "Croissance du prix du pétrole" | XPOIL CELL | "Croissance des prix des biens importés" | XPWMAB CELL | "Croissance des prix des marchés pertinents à l'export" | XPWXAB CELL | "Croissance des marchés pertinents" | XQWXSS ----- | ---------------------------------------------------------------- MODE | FILES | DATE | nb lines: 13 nb columns: 2 language: 'ENGLISH' gridx: 'MAJOR' gridy: 'MAJOR' graph_axis: 'VALUES' graph_alignment: 'LEFT' >>> # --> table is also updated in the global workspace >>> tables["X_GRT"] DIVIS | 1 | TITLE | "Croissance" ----- | ---------------------------------------------------------------- CELL | | "#S" ----- | ---------------------------------------------------------------- CELL | "Croissance de la population active" | XNATY CELL | "Croissance du prix du pétrole" | XPOIL CELL | "Croissance des prix des biens importés" | XPWMAB CELL | "Croissance des prix des marchés pertinents à l'export" | XPWXAB CELL | "Croissance des marchés pertinents" | XQWXSS ----- | ---------------------------------------------------------------- MODE | FILES | DATE | nb lines: 13 nb columns: 2 language: 'ENGLISH' gridx: 'MAJOR' gridy: 'MAJOR' graph_axis: 'VALUES' graph_alignment: 'LEFT'
>>> # d) add/update several equations at once >>> # 1) using a dict of values >>> table_C8_1 = tables["C8_1"].copy() >>> table_C8_1.title = table_C8_1.title + " (copy)" >>> table_C8_2 = tables["C8_2"].copy() >>> table_C8_2.title = table_C8_2.title + " (copy)" >>> table_C8_3 = tables["C8_3"].copy() >>> table_C8_3.title = table_C8_3.title + " (copy)" >>> values = {"C8_1": table_C8_1, "C8_2": table_C8_2, "C8_3": table_C8_3} >>> tables["C8_1, C8_2, C8_3"] = values >>> tables["C8_1, C8_2, C8_3"] Workspace: Tables nb tables: 3 filename: ...fun.tbl name table titles C8_1 Déterminants de l'output potentiel (copy) C8_2 Déterminants de la productivité (copy) C8_3 Output gap (copy)
>>> # 2) using another Tables database (subset) >>> tables_subset = tables["C8_1, C8_2, C8_3"].copy() >>> for tbl_name in tables_subset.names: ... title = tables_subset[tbl_name].title ... tables_subset[tbl_name].title = title.replace("(copy)", "(detached subset)") >>> tables["C8_1, C8_2, C8_3"] = tables_subset >>> tables["C8_1, C8_2, C8_3"] Workspace: Tables nb tables: 3 filename: ...fun.tbl name table titles C8_1 Déterminants de l'output potentiel (detached subset) C8_2 Déterminants de la productivité (detached subset) C8_3 Output gap (detached subset)