iode.Lists.copy
- Lists.copy(pattern: str = None) Self
Create a new database instance in which each object is a copy of the original object from the global IODE database. Any change made to the copied database (subset) will not be applied to the global workspace. This can be useful for example if you want to save previous values of scalars before estimating an equation or a block of equations and then restore the original values if the estimated values are not satisfying.
- Parameters:
- patternstr, optional
If provided, the copied database will only contain the objects whose name matches the provided pattern. By default (None), the copied database will contain all the objects from the global IODE database. The pattern syntax is the same as the one used for the __getitem__ method. If the pattern is an empty string, the copied database will be empty, creating a new detached database. Default to None.
- Returns:
- Database
See also
IodeDatabase.new_detached
Examples
>>> from iode import SAMPLE_DATA_DIR >>> from iode import comments >>> comments.load(f"{SAMPLE_DATA_DIR}/fun.cmt") Loading .../fun.cmt 317 objects loaded
Database subset
>>> # without using copy(), any modification made on >>> # the subset will also change the corresponding >>> # global IODE workspace >>> cmt_subset = comments["A*"] >>> cmt_subset.names ['ACAF', 'ACAG', 'AOUC', 'AQC'] >>> # a) add a comment >>> cmt_subset["A_NEW"] = "New comment" >>> "A_NEW" in cmt_subset True >>> "A_NEW" in comments True >>> comments["A_NEW"] 'New comment' >>> # b) modify a comment >>> cmt_subset["ACAF"] = "Modified Comment" >>> cmt_subset["ACAF"] 'Modified Comment' >>> comments["ACAF"] 'Modified Comment' >>> # c) delete a comment >>> del cmt_subset["ACAG"] >>> "ACAG" in cmt_subset False >>> "ACAG" in comments False
Copied database subset
>>> cmt_subset_copy = comments["B*"].copy() >>> cmt_subset_copy.names ['BENEF', 'BENEF_', 'BQY', 'BVY'] >>> # or equivalently >>> cmt_subset_copy = comments.copy("B*") >>> cmt_subset_copy.names ['BENEF', 'BENEF_', 'BQY', 'BVY'] >>> # by using copy(), any modification made on the copy subset >>> # let the global workspace unchanged >>> # a) add a comment -> only added in the copied subset >>> cmt_subset_copy["B_NEW"] = "New Comment" >>> "B_NEW" in cmt_subset_copy True >>> "B_NEW" in comments False >>> # b) modify a comment -> only modified in the copied subset >>> cmt_subset_copy["BENEF"] = "Modified Comment" >>> cmt_subset_copy["BENEF"] 'Modified Comment' >>> comments["BENEF"] 'Ondernemingen: niet-uitgekeerde winsten.' >>> # c) delete a comment -> only deleted in the copied subset >>> del cmt_subset_copy["BENEF_"] >>> "BENEF_" in cmt_subset_copy False >>> "BENEF_" in comments True
New detached database
>>> # a new empty *detached* database can be created by passing >>> # an empty string to the copy() method >>> cmt_detached = comments.copy("") >>> cmt_detached.names [] >>> # or equivalently by using the new_detached() method >>> cmt_detached = comments.new_detached() >>> cmt_detached.names []