User defaults in a package

I’m currently working on a package that makes dealing with various inputs and outputs of DFT calculations easier.

Since the workflow of most people doing these calculations on a daily basis involve a number of default settings/inputs that they mostly use, I would also like to implement some way of saving these in some kind of user specific config file/ defaults file. An example of one of those defaults would be a dictionary with pseudopotentials for each element, so that people don’t need to manually find the directory and name of the respective pseudopotentials each time.

Now, implementing this is kind of trivial, I’m mostly wondering what the best/recommended way of handling this storage is. Do I generate a user_defaults folder inside of the package directory when someone installs it? Do I generate such a folder in a designated .xxx folder inside their home directory?

One way is using environment variables. For example, in your .juliarc.jl you could provide is like this:

ENV["CUSTOM_SETTING"] = "my custom option"

So that in your module you could then have something like

try
    if ENV["CUSTOM_SETTING"] = "my custom option"
        # do custom stuff
    end
end

If you create an actual settings file in the package directory, I recommend adding that filename to your package’s .gitignore file though, to ensure it doesn’t get tracked by git. You then need to either check for its existence, and if not existing then use defaults or you can generate a file with the defaults in it.

1 Like

Thanks for the reply. The idea is that people specify some default setting/config once, and it being defined everytime they using Package . I’m not sure if that is possible using environment variables, other than storing them inside their .juliarc.jl file, which seems pretty invasive (can maybe also cause unwanted behaviour with other packages?). Maybe it’s possible by actually changing some of the codebase depending on their defined defaults, but this seems cumbersome.

I guess also since the codes all work with input files, having default input files combined with a config file works best. Bearing this in mind, is it ok practice to change their specific version of .gitignore after they define their defaults?

The reason I suggested this approach is because I thought you wanted the users to be able to provide the settings at their own discretion. With the environment variable approach, the default settings would automatically be loaded if no environment variables are set at all. Only when the user has changed their environment variables before they load the package will they get a different setting. If that’s not what you had in mind for allowing users to provide an option at package initialization then disregard that idea.

Your .gitignore file should be hosted in your git repository. That’s where you should update it. When people clone your repository, they will also get the .gitignore file you have stored in your repo. So I don’t really understand what your question is…

Ok I guess my question was because I’m not fluent in git stuff. If I understand correctly, if I add the user_defaults folder to .gitignore, their specific generated defaults will stay there also when users would update the package. If they remove the package, will the julia package manager delete everyting (just the entire package folder), or everything apart from things in the gitignore file.

I believe the package manager would not delete the extra files, but the best way to find out is to just test it and see what happens.

Not to do this again, but I wrote a package that does this.

Every time you call using Package, a call gets made to load the defaults.jl file

You’ll need to checkout the master version though


relevant code:

  • boiler-plate template for bootload.jl from Julz repo
  • an example defaults.jl file from a codebase that uses Julz

https://github.com/djsegal/Julz.jl