Stream: helpdesk (published)

Topic: ✔ When should I read config file?


view this post on Zulip Andrey Oskin (Feb 22 2022 at 10:08):

Guess we are both do not understand each other (and I do not understand Preferences.jl). Thank you for your patience, let me explain what I want in more details.

Suppose that I have package Foo, which can do some calculations and it produces some output for these calculations. It can be long description and it can be short description. So, I have to choose which type of description is default, but I do not want to set it in code, I want to make it optional.

My idea is the following: I can create system wide config file, where I can set output = compact or output = long.

For example I set this config file to output = compact

Now, I can go to directory 1 and run

julia --project=.

using Foo: Bar

x = Bar()
calc(x)
# Compact output

I can go to directory 2 and run

julia --project=. # it is new Project.toml and everything else

using Foo: Bar

x = Bar()
calc(x)
# Compact output, since I still use system wide preferences

If I change settings to output = long then in each directory I run the same code, I'll see long output, because that's what I want. I kind of changes my desire for default behaviour.

Now, if I get it right, then Preferences.jl do something different. It allows me to set my settings in each directory individually, but if I am not doing it, then it uses default value from the package. I.e. each time I should do

cd dir3
julia --project=.

setpreferences(output=compact)

otherwise I get package default, not my default.

view this post on Zulip Andrey Oskin (Feb 22 2022 at 10:08):

Hope it makes it more clear.
But may be I misunderstood general idea of Preferences.jl

view this post on Zulip Fredrik Ekre (Feb 22 2022 at 10:11):

Okay, I believe you can have global preferences with Preferences.jl too.

view this post on Zulip Notification Bot (Feb 22 2022 at 10:38):

Andrey Oskin has marked this topic as resolved.

view this post on Zulip Felix Kastner (Feb 22 2022 at 16:11):

You can have preferences stored in your default environments Project.toml or a LocalPreferences.toml at the same location. The only drawback is that the package for which you want to record preferences has to be added to that environment.
So

using Preferences
using Example
set_preferences!(Example, "foo" => "bar")

stores it in ~/.julia/environments/v1.7/LocalPreferences.toml.
This can then be loaded using load_preference(Example, "foo") which will traverse the LOAD_PATH to look for stored preferences.

view this post on Zulip Andrey Oskin (Feb 22 2022 at 16:52):

It will read from ~/.julia/environments/v1.7/LocalPreferences.toml even when I start Julia with --project=. flag? It's going hierarchically? If it can't find local preferences in current directory it will read from global? If yes, it can make this approach more interesting (of course min 1.6 is still a huge drawback)

view this post on Zulip Andrey Oskin (Feb 22 2022 at 16:52):

I guess I should just try it and see how it goes.

view this post on Zulip Fredrik Ekre (Feb 22 2022 at 16:53):

Yes, that should work. Since Julia versions < 1.6 are not supported versions anymore I don't think it is a drawback really.


Last updated: Oct 02 2023 at 04:34 UTC