Skip to content

DataFile behaves differently inside with statement #1242

@johnomotani

Description

@johnomotani

I noticed while looking at #1241 that DataFile has unexpected (to me) behaviour inside a with statement.

def __enter__(self):
return self.impl.__enter__()

because __enter__ returns self.impl.__enter__(), in a statement like

with DataFile('BOUT.dmp.0.nc') as f:
    # do stuff here

f is a DataFile_netCDF object, not a DataFile object. In practice this doesn't make much difference because the interfaces are very similar, but DataFile.read does some work

if ranges is not None:
for x in ranges:
if isinstance(x, (list, tuple)):
x = slice(*x)
return self.impl.read(name, ranges=ranges, asBoutArray=asBoutArray)

(although I think both netCDF and hdf5 can handle converting lists and tuples to slices) and bout_type(self, varname) which returns self.attributes(varname)["bout_type"] would be missing.

In summary, I'd expect DataFile.__enter__ to return self so that f in the with-statement is a DataFile object not one of the implementations. Is this a bug?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions