Skip to content

Commit

Permalink
Merge pull request #356 from tovrstra/iodata-data
Browse files Browse the repository at this point in the history
Do not use iodata as variable name
  • Loading branch information
tovrstra authored Jun 29, 2024
2 parents 8cf1132 + bccd2fd commit c819ebc
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions iodata/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,14 @@ def load_many(filename: str, fmt: Optional[str] = None, **kwargs) -> Iterator[IO
raise LoadError("Uncaught exception while loading file.", lit) from exc


def _check_required(filename: str, iodata: IOData, dump_func: Callable):
def _check_required(filename: str, data: IOData, dump_func: Callable):
"""Check that required attributes are not None before dumping to a file.
Parameters
----------
filename
The file to be dumped to, only used for error messages.
iodata
data
The data to be written.
dump_func
The dump_one or dump_many function that will write the file.
Expand All @@ -240,15 +240,15 @@ def _check_required(filename: str, iodata: IOData, dump_func: Callable):
When a required attribute is ``None``.
"""
for attr_name in dump_func.required:
if getattr(iodata, attr_name) is None:
if getattr(data, attr_name) is None:
raise PrepareDumpError(
f"Required attribute {attr_name}, for format {dump_func.fmt}, is None.", filename
)


@_reissue_warnings
def dump_one(
iodata: IOData,
data: IOData,
filename: str,
*,
fmt: Optional[str] = None,
Expand All @@ -263,7 +263,7 @@ def dump_one(
Parameters
----------
iodata
data
The object containing the data to be written.
filename
The file to write the data to.
Expand Down Expand Up @@ -296,9 +296,9 @@ def dump_one(
"""
format_module = _select_format_module(filename, "dump_one", fmt)
try:
_check_required(filename, iodata, format_module.dump_one)
_check_required(filename, data, format_module.dump_one)
if hasattr(format_module, "prepare_dump"):
iodata = format_module.prepare_dump(iodata, allow_changes, filename)
data = format_module.prepare_dump(data, allow_changes, filename)
except PrepareDumpError:
raise
except Exception as exc:
Expand All @@ -307,17 +307,17 @@ def dump_one(
) from exc
with open(filename, "w") as f:
try:
format_module.dump_one(f, iodata, **kwargs)
format_module.dump_one(f, data, **kwargs)
except DumpError:
raise
except Exception as exc:
raise DumpError("Uncaught exception while dumping to a file", filename) from exc
return iodata
return data


@_reissue_warnings
def dump_many(
iodatas: Iterable[IOData],
iter_data: Iterable[IOData],
filename: str,
*,
fmt: Optional[str] = None,
Expand All @@ -332,7 +332,7 @@ def dump_many(
Parameters
----------
iodatas
iter_data
An iterator over IOData instances.
filename
The file to write the data to.
Expand All @@ -353,22 +353,24 @@ def dump_many(
e.g. due to missing attributes, and no conversion is available or allowed
to make it compatible.
If the output file already existed, it is not overwritten when this error
is raised while processing the first ``IOData`` instance in the ``iodatas`` argument.
is raised while processing the first ``IOData`` instance in the ``datas`` argument.
When the exception is raised in later iterations, any existing file is overwritten.
PrepareDumpWarning
When an ``IOData`` object is not compatible with the file format,
but it was converted to fix the compatibility issue.
"""
format_module = _select_format_module(filename, "dump_many", fmt)

# Make sure we have an iterator. For example, this turns a list into an iterator.
iter_data = iter(iter_data)

# Check the first item before creating the file.
# If the file already exists, this may prevent data loss:
# The file is not overwritten when it is clear that writing will fail.
iter_iodatas = iter(iodatas)
try:
first = next(iter_iodatas)
first = next(iter_data)
except StopIteration as exc:
raise DumpError("dump_many needs at least one iodata object.", filename) from exc
raise DumpError("dump_many needs at least one IOData object.", filename) from exc
try:
_check_required(filename, first, format_module.dump_many)
if hasattr(format_module, "prepare_dump"):
Expand All @@ -381,10 +383,10 @@ def dump_many(
) from exc

def checking_iterator():
"""Iterate over all iodata items, not checking the first."""
"""Iterate over all data items, not checking the first."""
# The first one was already checked.
yield first
for other in iter_iodatas:
for other in iter_data:
_check_required(filename, other, format_module.dump_many)
yield (
format_module.prepare_dump(other, allow_changes, filename)
Expand All @@ -403,7 +405,7 @@ def checking_iterator():

@_reissue_warnings
def write_input(
iodata: IOData,
data: IOData,
filename: str,
fmt: str,
template: Optional[str] = None,
Expand All @@ -414,7 +416,7 @@ def write_input(
Parameters
----------
iodata
data
An IOData instance containing the information needed to write input.
filename
The input file name.
Expand All @@ -435,7 +437,7 @@ def write_input(
input_module = _select_input_module(filename, fmt)
with open(filename, "w") as fh:
try:
input_module.write_input(fh, iodata, template, atom_line, **kwargs)
input_module.write_input(fh, data, template, atom_line, **kwargs)
except Exception as exc:
raise WriteInputError(
"Uncaught exception while writing an input file.", filename
Expand Down

0 comments on commit c819ebc

Please sign in to comment.