EasyConfig
EasyConfig provides a simple nested AbstractDict{Symbol, Any}
data structure.
Advantages over dictionaries/named tuples:
1) Intermediate levels are created on the fly:
c = Config().one.two.three = 1
vs.
c = OrderedDict(:one => OrderedDict(:two => OrderedDict(:three => 1)))
c = (one = (two = (three = 1,),),)
c = (; one = (;two = (;three = 1)))
getproperty
:
2) Values can be accessed with c.one.two.three == 1 # same as NamedTuple
vs.
c[:one][:two][:three] == 1
get/set
property/index
:
3) Getting/setting can be done with (for the purpose of making Symbol
s easier to work with)
c.why."would you"["need to do this?"] = "No reason"
c."why"[var"would you"]."need to do this?" == "No reason"
Conversion to JSON
Simply JSON3.write
it!
Gotchas
If you try to access something that doesn't exist, an empty Config()
will sit there (a consequence of creating intermediate levels on the fly):
c = Config()
c.one.two.three.four.five.six == Config()
Config
s with delete_empty!(::Config)
.
Pluto ๐ !)
Example (Try this in begin
using Random, EasyConfig, JSON3
function plot(config)
id = randstring(20)
HTML("""
<div id="$id""></div>
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script>
<script>
var data = $(JSON3.write(config.data))
var layout = $(JSON3.write(config.layout))
Plotly.newPlot("$id", data, layout, {responsive:true, displaylogo: false, displayModeBar: false})
</script>
""")
end
myplot = Config()
myplot.data = [Config(
x=randn(100), y = randn(100), mode="markers"
)]
myplot.layout.title = "My Plot"
myplot.layout.xaxis.title = "X Axis"
myplot.layout.yaxis.title = "Y Axis"
plot(myplot)
end