iode.Comments.__setitem__

Comments.__setitem__(key: str | List[str], value: str | List[str])[source]

Update/add a (subset of) comment(s) referenced by key from/to the Comments 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 comment(s) to update/add. The list of comments 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 pandas.Series(str, str) or Comments

(new) comment(s) value(s).

Examples

>>> import pandas as pd
>>> from iode import SAMPLE_DATA_DIR
>>> from iode import comments
>>> comments.load(f"{SAMPLE_DATA_DIR}/fun.cmt")
Loading .../fun.cmt
317 objects loaded 
>>> # a) add one comment
>>> comments["BDY"] = "Difference net incomes (YN - YK)"
>>> comments["BDY"]
'Difference net incomes (YN - YK)'
>>> # b) update one comment
>>> comments["ACAF"]
'Ondernemingen: ontvangen kapitaaloverdrachten.'
>>> comments["ACAF"] = "New Value"
>>> comments["ACAF"]
'New Value'
>>> # c) add/update several comments at once
>>> # 1) using a dict of values
>>> values = {"AOUC": "Updated AOUC from dict", "ACAF": "Updated ACAF from dict", 
...           "ACAG": "Updated ACAG from dict"}
>>> comments["ACAF, ACAG, AOUC"] = values
>>> comments["ACAF, ACAG, AOUC"]
Workspace: Comments
nb comments: 3
filename: ...fun.cmt

name           comments       
ACAF    Updated ACAF from dict
ACAG    Updated ACAG from dict
AOUC    Updated AOUC from dict
>>> # 2) using a pandas series
>>> data = ["Updated AOUC from series", "Updated ACAF from series", "Updated ACAG from series"]
>>> series = pd.Series(data, index=["AOUC", "ACAF", "ACAG"])
>>> comments["ACAF, ACAG, AOUC"] = series
>>> comments["ACAF, ACAG, AOUC"]
Workspace: Comments
nb comments: 3
filename: ...fun.cmt

name            comments        
ACAF    Updated ACAF from series
ACAG    Updated ACAG from series
AOUC    Updated AOUC from series
>>> # 3) using an iode Comments object (subset)
>>> comments_subset = comments["ACAF, ACAG, AOUC"].copy()
>>> comments_subset["ACAF"] = "Updated ACAF from another iode Comments database"
>>> comments_subset["ACAG"] = "Updated ACAG from another iode Comments database"
>>> comments_subset["AOUC"] = "Updated AOUC from another iode Comments database"
>>> comments["ACAF, ACAG, AOUC"] = comments_subset
>>> comments["ACAF, ACAG, AOUC"]
Workspace: Comments
nb comments: 3
filename: ...fun.cmt

name                        comments                    
ACAF    Updated ACAF from another iode Comments database
ACAG    Updated ACAG from another iode Comments database
AOUC    Updated AOUC from another iode Comments database
>>> # d) working on a subset
>>> # 1) get subset
>>> comments_subset = comments["A*"]
>>> comments_subset.names
['ACAF', 'ACAG', 'AOUC', 'AQC']
>>> # 2) add a comment to the subset 
>>> comments_subset["A0"] = "New Comment"
>>> comments_subset["A0"]
'New Comment'
>>> # --> new comment also appears in the global workspace
>>> "A0" in comments
True
>>> comments["A0"]
'New Comment'
>>> # 3) update a comment in the subset
>>> comments_subset["A0"] = "Updated Comment"
>>> comments_subset["A0"]
'Updated Comment'
>>> # --> comment is also updated in the global workspace
>>> comments["A0"]
'Updated Comment'