From 46734bb73168ca23268e51e6f582af402c7ad403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Sch=C3=B6nfeldt?= Date: Tue, 7 May 2024 15:57:29 +0200 Subject: [PATCH 1/2] Prepare for new dump path handling The change is designed to allow a smooth transition to a new default, which will be using the current working directory for dumps. --- src/oemof/network/energy_system.py | 50 +++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/src/oemof/network/energy_system.py b/src/oemof/network/energy_system.py index 3b91818..2c237e1 100644 --- a/src/oemof/network/energy_system.py +++ b/src/oemof/network/energy_system.py @@ -216,8 +216,8 @@ def flows(self): for target in source.outputs } - def dump(self, dpath=None, filename=None): - r"""Dump an EnergySystem instance.""" + @staticmethod + def _deprecated_path_handling(dpath, filename): if dpath is None: bpath = os.path.join(os.path.expanduser("~"), ".oemof") if not os.path.isdir(bpath): @@ -226,30 +226,52 @@ def dump(self, dpath=None, filename=None): if not os.path.isdir(dpath): os.mkdir(dpath) + warnings.warn( + "Default directory for oemof dumps will change" + + "from ~/.oemof/dumps/ to ./ in a future version." + + " Set 'consider_dpath' to False to already use" + + " the new default.", + FutureWarning, + ) + else: + warnings.warn( + "Parameter 'dpath' will be removed in a future" + + " version. You can give the directory as part" + + " of the filename and set 'consider_dpath' to" + + " False to suppress this waring.", + FutureWarning, + ) + if filename is None: filename = "es_dump.oemof" - pickle.dump(self.__dict__, open(os.path.join(dpath, filename), "wb")) + return os.path.join(dpath, filename) - msg = "Attributes dumped to: {0}".format(os.path.join(dpath, filename)) + def dump(self, dpath=None, filename=None, consider_dpath=True): + r"""Dump an EnergySystem instance.""" + if consider_dpath: + filename = self._deprecated_path_handling(dpath, filename) + elif filename is None: + filename = "./es_dump.oemof" + + pickle.dump(self.__dict__, open(filename, "wb")) + + msg = "Attributes dumped to: {0}".format(filename) logging.debug(msg) return msg - def restore(self, dpath=None, filename=None): + def restore(self, dpath=None, filename=None, consider_dpath=True): r"""Restore an EnergySystem instance.""" logging.info( "Restoring attributes will overwrite existing attributes." ) - if dpath is None: - dpath = os.path.join(os.path.expanduser("~"), ".oemof", "dumps") + if consider_dpath: + filename = self._deprecated_path_handling(dpath, filename) + elif filename is None: + filename = "./es_dump.oemof" - if filename is None: - filename = "es_dump.oemof" + self.__dict__ = pickle.load(open(filename, "rb")) - self.__dict__ = pickle.load(open(os.path.join(dpath, filename), "rb")) - - msg = "Attributes restored from: {0}".format( - os.path.join(dpath, filename) - ) + msg = "Attributes restored from: {0}".format(os.path.join(filename)) logging.debug(msg) return msg From 2cd8db50b30d5c86605cdef1934d9b7ec4413885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Sch=C3=B6nfeldt?= Date: Tue, 14 Jan 2025 13:34:06 +0100 Subject: [PATCH 2/2] Add deprecation wrapper for dpath in dump/restore --- src/oemof/network/energy_system.py | 94 ++++++++++++++++++------------ 1 file changed, 57 insertions(+), 37 deletions(-) diff --git a/src/oemof/network/energy_system.py b/src/oemof/network/energy_system.py index 2c237e1..1d5a091 100644 --- a/src/oemof/network/energy_system.py +++ b/src/oemof/network/energy_system.py @@ -216,43 +216,57 @@ def flows(self): for target in source.outputs } + # Begin: to be removed in a future version @staticmethod - def _deprecated_path_handling(dpath, filename): - if dpath is None: - bpath = os.path.join(os.path.expanduser("~"), ".oemof") - if not os.path.isdir(bpath): - os.mkdir(bpath) - dpath = os.path.join(bpath, "dumps") - if not os.path.isdir(dpath): - os.mkdir(dpath) - - warnings.warn( - "Default directory for oemof dumps will change" - + "from ~/.oemof/dumps/ to ./ in a future version." - + " Set 'consider_dpath' to False to already use" - + " the new default.", - FutureWarning, - ) + def _deprecated_path_handling(dpath, filename, consider_dpath): + if consider_dpath: + if dpath is None: + bpath = os.path.join(os.path.expanduser("~"), ".oemof") + if not os.path.isdir(bpath): + os.mkdir(bpath) + dpath = os.path.join(bpath, "dumps") + if not os.path.isdir(dpath): + os.mkdir(dpath) + + warnings.warn( + "Default directory for oemof dumps will change" + + "from ~/.oemof/dumps/ to ./ in a future version." + + " Set 'consider_dpath' to False to already use" + + " the new default.", + FutureWarning, + ) + else: + warnings.warn( + "Parameter 'dpath' will be removed in a future" + + " version. You can give the directory as part" + + " of the filename and set 'consider_dpath' to" + + " False to suppress this waring.", + FutureWarning, + ) + if filename is None: + filename = "es_dump.oemof" + + filename = os.path.join(dpath, filename) else: - warnings.warn( - "Parameter 'dpath' will be removed in a future" - + " version. You can give the directory as part" - + " of the filename and set 'consider_dpath' to" - + " False to suppress this waring.", - FutureWarning, - ) - - if filename is None: - filename = "es_dump.oemof" - - return os.path.join(dpath, filename) + if dpath is not None: + if filename is None: + # Interpret dpath as intended to be filename, + # as it might be given as positional argument. + filename = dpath + else: + raise ValueError( + "You set filename and dpath but told that" + + " dpath should be ignored." + ) + + return filename + # End: to be removed in a future version def dump(self, dpath=None, filename=None, consider_dpath=True): r"""Dump an EnergySystem instance.""" - if consider_dpath: - filename = self._deprecated_path_handling(dpath, filename) - elif filename is None: - filename = "./es_dump.oemof" + filename = self._deprecated_path_handling( + dpath, filename, consider_dpath + ) pickle.dump(self.__dict__, open(filename, "wb")) @@ -260,15 +274,21 @@ def dump(self, dpath=None, filename=None, consider_dpath=True): logging.debug(msg) return msg - def restore(self, dpath=None, filename=None, consider_dpath=True): + def restore( + self, + dpath=None, # to be removed in a future version + filename=None, + consider_dpath=True, # to be removed in a future version + ): r"""Restore an EnergySystem instance.""" logging.info( "Restoring attributes will overwrite existing attributes." ) - if consider_dpath: - filename = self._deprecated_path_handling(dpath, filename) - elif filename is None: - filename = "./es_dump.oemof" + # Start: to be removed in a future version + filename = self._deprecated_path_handling( + dpath, filename, consider_dpath + ) + # End: to be removed in a future version self.__dict__ = pickle.load(open(filename, "rb"))