Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare for new dump path handling #50

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/oemof/network/energy_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Loading