Skip to content

Commit

Permalink
docs: fix docstring, add to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmcgrath13 committed Jun 5, 2020
1 parent d57c9e3 commit a2e02a5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ dm = DotMap(dict)
dm.c.d # returns 3
dm.c.e = 5
dm["c"].e # returns 5

DotMap.todict(dm, keys_as_strings=true) # returns Dict("a"=>1, "b"=>2, "c" => Dict("d"=>3, "e"=>5))
DotMap.todict(dm) # returns Dict(:a=>1, :b=>2, :c => Dict(:d=>3, :e=>5))
```

**NOTE** This is not as performative as using normal dictionaries, but is nice for accessing deeply nested dictionary structures, such as large config/yaml/json files.
13 changes: 6 additions & 7 deletions src/DotMaps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ end
DotMap(d::Any) = d

"""
DotMaps.todict(::DotMap)
DotMaps.todict(::DotMap; keys_as_strings=false)
Constructs a Dict from a DotMap.
Constructs a Dict from a DotMap. If `keys_as_strings`, the keys will be `String` instead of `Symbol`.
"""
function todict(obj::DotMap; keys_as_strings::Bool = false)
new_dict = Dict()
dm = obj.__dict__
for (k, v) in dm
dict = Dict()
for (k, v) in obj
nk = keys_as_strings ? string(k) : k
new_dict[nk] = todict(v, keys_as_strings = keys_as_strings)
dict[nk] = todict(v, keys_as_strings = keys_as_strings)
end

return new_dict
return dict
end

# return at leaves
Expand Down

0 comments on commit a2e02a5

Please sign in to comment.