osdir.com


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

preferences file


On Thu, 24 Jan 2019 at 20:50, Dave <dboland9 at offilive.com> wrote:
>
> I'm doing a small application and want to add user preferences.  Did
> some googling to see if there are standard Python ways/tools, but it
> seems not so much.  My specific questions are:
>
> 1. Best practices for a user preference file/system?

Put them in the appropriate config directory (see answer to Q3).
Windows has the registry and macOS has User Defaults, but a custom
format is fine, especially if you?re making something multi-platform.

> 2. File format favored and why - ini, JSON, etc?

If you want/expect humans to edit it, go with INI (configparser).
Otherwise (if the editing is done via your app with a nice UI), JSON
can express more than just key-value mappings, although there are no
comments, and its strict syntax requirements can be problematic for
non-programmers trying to edit the file. Randomly loading .py or
pickle files can potentially be unsafe; writing .py files in an
automated way is not trivial. I wouldn?t bother with any other
formats, mainly since they are not supported in stdlib, although if
you need human editing and more advanced structures, then maybe
YAML/TOML (they aren?t as foolproof as INI though).

> 3. File location?  I'm using Ubuntu and I believe that the correct
> location would be home/.config/<app-name> .  What about Mac and Windows?

https://pypi.org/project/appdirs/

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16