EasyConfig.jl

Easy-to-write JSON-ish data structure
Author joshday
Popularity
12 Stars
Updated Last
1 Year Ago
Started In
April 2020

EasyConfig

Build Status Codecov

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)))

2) Values can be accessed with getproperty:

c.one.two.three == 1  # same as NamedTuple

vs.

c[:one][:two][:three] == 1

3) Getting/setting can be done with get/set property/index:

(for the purpose of making Symbols 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()

๐Ÿงน Clean up any stranded empty Configs with delete_empty!(::Config).

Example (Try this in Pluto ๐ŸŽˆ!)

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

Used By Packages