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 4 commits 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
88 changes: 65 additions & 23 deletions src/oemof/network/energy_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,40 +216,82 @@ def flows(self):
for target in source.outputs
}

def dump(self, dpath=None, filename=None):
# Begin: to be removed in a future version
@staticmethod
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:
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 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)

if filename is None:
filename = "es_dump.oemof"
filename = self._deprecated_path_handling(
dpath, filename, consider_dpath
)

pickle.dump(self.__dict__, open(os.path.join(dpath, filename), "wb"))
pickle.dump(self.__dict__, open(filename, "wb"))

msg = "Attributes dumped to: {0}".format(os.path.join(dpath, filename))
msg = "Attributes dumped to: {0}".format(filename)
logging.debug(msg)
return msg

def restore(self, dpath=None, filename=None):
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 dpath is None:
dpath = os.path.join(os.path.expanduser("~"), ".oemof", "dumps")

if 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(os.path.join(dpath, filename), "rb"))
self.__dict__ = pickle.load(open(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