This small package implements properties and property files inspired by Java. Replicating it's functionality the package only handles string keys and string values. Advantage is that saving the files is easy-peasy, downside that the user must parse/convert the strings into other types. The intended use covers mostly "basic" types like strings, integers, floats, but the package does not restrict the use of other types.
Install the package by:
(v1.1) pkg> add https://github.com/cserteGT3/PropertyFiles.jl
The package introduces the Properties
type, which wraps a dictionary: Dict{String,String}
.
julia> using PropertyFiles
julia> p = Properties()
Properties(Dict{String,String}())
setprop()
function can be used to populate the dictionary.
Only strings can be used for keys and values, values with other types are converted.
julia> setprop(p, "key1", "this is a string")
julia> setprop(p, "key2", 156.0)
You can get the stored values with the getprop
function.
julia> getprop(p, "key1")
"this is a string"
julia> getprop(p, "key2")
"156.0"
julia> getprop(p, "not defined key", "default value")
"default value"
As the last example shows, you can give a default value for the case the key not exists.
You can save and load the property-dictionary with or without comments:
julia> store(p, "filename.jlprop")
julia> store(p, "filename2.jlprop", "comments")
julia> p1 = load("filename.jlprop")
Properties(Dict("key2"=>"156.0","key1"=>"this is a string"))
julia> p1.properties == p.properties
true
The produced file looks like (filename2.jlprop):
#comments
#2019-08-02T12:41:09.798
key2=156.0
key3=0
key1=this is a string
- more tests for different types
- add more tests in general
- more documentation
-
- file format description
-
- Usage with user defined types section
- overload
Base.show
- overload more functions from Base
- automatic conversion from string
Many thanks to @tpapp for the PkgSkeleton.jl package which made the publication of this package much more easier.