Skip to content

Commit

Permalink
Rename names to keys to better align with dict-like interface (#698)
Browse files Browse the repository at this point in the history
  • Loading branch information
musm authored Oct 9, 2020
1 parent 0783ac9 commit f01ad31
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/HDF5.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ using Requires: @require

import Base:
close, convert, eltype, lastindex, flush, getindex, ==,
isempty, isvalid, length, names, ndims, parent, read,
isempty, isvalid, length, ndims, parent, read,
setindex!, show, size, sizeof, write, isopen, iterate, eachindex, axes

import Libdl
Expand Down Expand Up @@ -537,7 +537,7 @@ function h5readattr(filename, name::String)
fid = h5open(filename,"r")
try
a = attrs(fid[name])
dat = Dict(x => read(a[x]) for x in names(a))
dat = Dict(x => read(a[x]) for x in keys(a))
finally
close(fid)
end
Expand Down Expand Up @@ -959,13 +959,13 @@ end
filename(obj::Union{File,Group,Dataset,Attribute,Datatype}) = h5f_get_name(checkvalid(obj).id)
name(obj::Union{File,Group,Dataset,Datatype}) = h5i_get_name(checkvalid(obj).id)
name(attr::Attribute) = h5a_get_name(attr.id)
function names(x::Union{Group,File})
function Base.keys(x::Union{Group,File})
checkvalid(x)
n = length(x)
return [h5l_get_name_by_idx(x, ".", H5_INDEX_NAME, H5_ITER_INC, i-1, H5P_DEFAULT) for i = 1:n]
end

function names(x::Attributes)
function Base.keys(x::Attributes)
checkvalid(x.parent)
n = length(x)
return [h5a_get_name_by_idx(x.parent, ".", H5_INDEX_NAME, H5_ITER_INC, i-1, H5P_DEFAULT) for i = 1:n]
Expand Down Expand Up @@ -1866,7 +1866,7 @@ end
if isa(node, Dataset)
push!(list, node)
else
for c in names(node)
for c in keys(node)
get_datasets!(list, node[c])
end
end
Expand Down
2 changes: 1 addition & 1 deletion src/datafile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ read(f::Base.Callable, parent::DataFile, name::String...) =

# Read every variable in the file
function read(f::DataFile)
vars = names(f)
vars = keys(f)
vals = Vector{Any}(undef,length(vars))
for i = 1:length(vars)
vals[i] = read(f, vars[i])
Expand Down
3 changes: 3 additions & 0 deletions src/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,6 @@ end
@deprecate_binding ScalarOrString Union{ScalarType,String} false
@deprecate_binding HDF5Scalar ScalarType false
@deprecate_binding HDF5BitsKind BitsType false

### Changed in PR#695
@deprecate names(x::Union{Group,File,Attributes}) keys(x)
2 changes: 1 addition & 1 deletion test/mpio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ end
MPI.Barrier(comm)
h5open(fn, comm) do f # default: opened in read mode, with default MPI.Info()
@test isopen(f)
@test names(f) == ["mygroup"]
@test keys(f) == ["mygroup"]

B = read(f, "mygroup/B", dxpl_mpio=HDF5.H5FD_MPIO_COLLECTIVE)
@test !isempty(B)
Expand Down
16 changes: 8 additions & 8 deletions test/plain.jl
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,9 @@ h5open(fn, "w") do fid
end
end
fid = h5open(fn, "r")
@test names(fid) == ["mygroup"]
@test keys(fid) == ["mygroup"]
g = fid["mygroup"]
@test names(g) == ["x"]
@test keys(g) == ["x"]
close(g)
close(fid)
rm(fn)
Expand All @@ -291,8 +291,8 @@ h5rewrite(outfile) do fid
end
@test length(readdir(tmpdir)) == 1
h5open(outfile, "r") do fid
@test names(fid) == ["mygroup"]
@test names(fid["mygroup"]) == ["x"]
@test keys(fid) == ["mygroup"]
@test keys(fid["mygroup"]) == ["x"]
end

# fail to overwrite
Expand All @@ -304,8 +304,8 @@ end
end
@test length(readdir(tmpdir)) == 1
h5open(outfile, "r") do fid
@test names(fid) == ["mygroup"]
@test names(fid["mygroup"]) == ["x"]
@test keys(fid) == ["mygroup"]
@test keys(fid["mygroup"]) == ["x"]
end

# overwrite
Expand All @@ -316,8 +316,8 @@ h5rewrite(outfile) do fid
end
@test length(readdir(tmpdir)) == 1
h5open(outfile, "r") do fid
@test names(fid) == ["mygroup"]
@test names(fid["mygroup"]) == ["y"]
@test keys(fid) == ["mygroup"]
@test keys(fid["mygroup"]) == ["y"]
end
rm(tmpdir, recursive=true)

Expand Down

0 comments on commit f01ad31

Please sign in to comment.