[ANN] PropertyFiles.jl - Properties and property files like in Java

I’m very happy to announce my very first published Julia package: PropertyFiles.jl. I really liked the Properties class, when I was working with Java, and thought that replicate this functionality in Julia.

What this package can do

Store string key-value pairs in a dictionary and save it in a file:

julia> using PropertyFiles

julia> p = Properties()
Properties(Dict{String,String}())

julia> setprop(p, "key1", "this is a string")

julia> setprop(p, "key2", 156.0)

julia> getprop(p, "key1")
"this is a string"

julia> getprop(p, "key2")
"156.0"

julia> getprop(p, "not defined key", "default value")
"default value"

julia> store(p, "filename.jlprop")

julia> p1 = load("filename.jlprop")
Properties(Dict("key2"=>"156.0","key1"=>"this is a string"))

More details at the repo, and the docs.

What it can’t do

This package works only with strings and intended for “basic” types like strings and floats/integers (althought the code itself is not restricted to these types).

Notes

The package is not registered (yet) as I have some concerns and wanted to have some community feedback. My main concern is that property is an important part of the type system and the name and functions are maybe confusing (I hope that the package name is not that distractive). My other concern is, that I replicated something that exists in an other language and want to “distribute” it under MIT license. I don’t really understand these licensing things, so I hope it’s ok (I know that Julia itself and most packages are MIT).

1 Like

Congrats! Putting a package together is great fun, and one of my favorite things about julia is how easy this is.

I’m curious what you’re getting out of this that you don’t get out of regular dictionaries and JSON.jl - is it just a different syntax?

julia> p = Dict()
Dict{Any,Any} with 0 entries

julia> p["key1"] = "this is a string"
"this is a string"

julia> p["key2"] = 156.
156.0

julia> get(p, "not defined key", "default")
"default"

julia> get(p, "key1", 0)
"this is a string"

julia> p["key2"]
156.0

julia> using JSON

julia> open("filename.json", "w") do outfile
           JSON.print(outfile, p)
       end

julia> newp = JSON.parsefile("filename.json")
Dict{String,Any} with 2 entries:
  "key2" => 156.0
  "key1" => "this is a string"
5 Likes

Szia and welcome to the Julia community!

What is the advantage of this versus something more standard like a Dictionary coupled with JSON for saving / loading. Compare the following session

julia> using JSON

julia> p = Dict{String,String}()
Dict{String,String} with 0 entries

julia> p["key1"] = "this is a string"
"this is a string"

julia> p["key2"] = string(156.0)
"156.0"

julia> p["key1"]
"this is a string"

julia> p["key2"]
"156.0"

julia> get(p, "not defined key", "default value")
"default value"

julia> open("filename.json", "w") do io
           JSON.print(io, p)
       end

julia> p1 = JSON.parsefile("filename.json")
Dict{String,Any} with 2 entries:
  "key2" => "156.0"
  "key1" => "this is a string"

Looking at the implementation for Properties it basically just forwards everything directly to the corresponding Dict method.

Edit: Sniped!

4 Likes

Wow… beat you by like 2 seconds :laughing:

2 Likes

Yeaah, nothing :smile:. I was so excited about this idea, I didn’t look for alternatives.

:blush:

Yeah, I didn’t think about JSON, this pretty much outperforms my solution on basically everything. :smile:

I decided to forward those methods only for the case if someone needs them for any reason (I don’t need those for my purpose).

1 Like

Since it’s all about the fun and the syntax :wink:

How about changing setprop to setprop! ?
https://docs.julialang.org/en/v1/manual/style-guide/#Append-!-to-names-of-functions-that-modify-their-arguments-1

I see that you already use getprop! for mutating get.

Great! I’ve been there myself :-). It was a good learning experience, I’m sure. Also consider opening issues or making PRs on JSON.jl if there’s something you feel you want that isn’t there.

3 Likes

It’s also very similar to ConfParser:

https://github.com/JuliaIO/ConfParser.jl

I was thinking on it too, but in the end decided against it. But you’re right that setprop! would be better for consistency, I’ll think it through ones more.

Thank you, that looks great! As I mentioned didn’t really looked for alternatives, and checked the registry only for Properties like names. I even forget that it’s basically a configuration file, and should check packages with that name :smile:

It’s not just consistency either. If I see setprop without the !, I will probably assume that it returns a copy of p instead of mutating. I think this is a pretty strong convention.

1 Like