| 1 |
Preferences system |
| 2 |
================== |
| 3 |
|
| 4 |
.. currentmodule:: brian2.core.preferences |
| 5 |
|
| 6 |
Each preference looks like ``codegen.c.compiler``, i.e. dotted names. Each |
| 7 |
preference has to be registered and validated. The idea is that registering |
| 8 |
all preferences ensures that misspellings of a preference value by a user |
| 9 |
causes an error, e.g. if they wrote ``codgen.c.compiler`` it would raise an |
| 10 |
error. Validation means that the value is checked for validity, so |
| 11 |
``codegen.c.compiler = 'gcc'`` would be allowed, but |
| 12 |
``codegen.c.compiler = 'hcc'`` would cause an error. |
| 13 |
|
| 14 |
An additional requirement is that the preferences system allows for extension |
| 15 |
modules to define their own preferences, including extending the existing |
| 16 |
core brian preferences. For example, an extension might want to define |
| 17 |
``extension.*`` but it might also want to define a new language for |
| 18 |
codegen, e.g. ``codegen.lisp.*``. However, extensions cannot add preferences |
| 19 |
to an existing category. |
| 20 |
|
| 21 |
Accessing and setting preferences |
| 22 |
--------------------------------- |
| 23 |
Preferences can be accessed and set either keyword-based or attribute-based. |
| 24 |
To set/get the value for the preference example mentioned before, the following |
| 25 |
are equivalent:: |
| 26 |
|
| 27 |
prefs['codegen.c.compiler'] = 'gcc' |
| 28 |
prefs.codegen.c.compiler = 'gcc' |
| 29 |
|
| 30 |
if prefs['codegen.c.compiler'] == 'gcc': |
| 31 |
... |
| 32 |
if prefs.codegen.c.compiler == 'gcc': |
| 33 |
... |
| 34 |
|
| 35 |
Using the attribute-based form can be particulary useful for interactive |
| 36 |
work, e.g. in ipython, as it offers autocompletion and documentation. |
| 37 |
In ipython, ``prefs.codegen.c?`` would display a docstring with all |
| 38 |
the preferences available in the ``codegen.c`` category. |
| 39 |
|
| 40 |
Preference files |
| 41 |
---------------- |
| 42 |
|
| 43 |
Preferences are stored in a hierarchy of files, with the following order |
| 44 |
(each step overrides the values in the previous step but no error is raised |
| 45 |
if one is missing): |
| 46 |
|
| 47 |
* The global defaults are stored in the installation directory. |
| 48 |
* The user default are stored in ``~/.brian/preferences`` (which works on |
| 49 |
Windows as well as Linux). |
| 50 |
* The file ``brian_preferences`` in the current directory. |
| 51 |
|
| 52 |
Registration |
| 53 |
------------ |
| 54 |
|
| 55 |
Registration of preferences is performed by a call to |
| 56 |
`BrianGlobalPreferences.register_preferences`, e.g.:: |
| 57 |
|
| 58 |
register_preferences( |
| 59 |
'codegen.c', |
| 60 |
'Code generation preferences for the C language', |
| 61 |
'compiler'= BrianPreference( |
| 62 |
validator=is_compiler, |
| 63 |
docs='...', |
| 64 |
default='gcc'), |
| 65 |
... |
| 66 |
) |
| 67 |
|
| 68 |
The first argument ``'codegen.c'`` is the base name, and every preference of |
| 69 |
the form ``codegen.c.*`` has to be registered by this function (preferences in subcategories |
| 70 |
such as ``codegen.c.somethingelse.*`` have to be specified separately). In other |
| 71 |
words, by calling `~BrianGlobalPreferences.register_preferences`, |
| 72 |
a module takes ownership of all the preferences with one particular base name. The second argument |
| 73 |
is a descriptive text explaining what this category is about. The preferences themselves are |
| 74 |
provided as keyword arguments, each set to a `BrianPreference` object. |
| 75 |
|
| 76 |
Validation functions |
| 77 |
-------------------- |
| 78 |
|
| 79 |
A validation function takes a value for the preference and returns ``True`` (if the value is a valid |
| 80 |
value) or ``False``. If no validation function is specified, a default validator is used that |
| 81 |
compares the value against the default value: Both should belong to the same class (e.g. int or |
| 82 |
str) and, in the case of a `Quantity` have the same unit. |
| 83 |
|
| 84 |
Validation |
| 85 |
---------- |
| 86 |
|
| 87 |
Setting the value of a preference with a registered base name instantly triggers |
| 88 |
validation. Trying to set an unregistered preference using keyword or attribute access raises an |
| 89 |
error. The only exception from this rule is when the preferences are read from configuration files |
| 90 |
(see below). Since this happens before the user has the chance to import extensions that potentially |
| 91 |
define new preferences, this uses a special function (`_set_preference`). In this case,for base |
| 92 |
names that are not yet registered, validation occurs when |
| 93 |
the base name is registered. If, at the time that `Network.run` is called, there |
| 94 |
are unregistered preferences set, a `PreferenceError` is raised. |
| 95 |
|
| 96 |
File format |
| 97 |
----------- |
| 98 |
|
| 99 |
The preference files are of the following form:: |
| 100 |
|
| 101 |
a.b.c = 1 |
| 102 |
# Comment line |
| 103 |
[a] |
| 104 |
b.d = 2 |
| 105 |
[a.b] |
| 106 |
b.e = 3 |
| 107 |
|
| 108 |
This would set preferences ``a.b.c=1``, ``a.b.d=2`` and ``a.b.e=3``. |
| 109 |
|
| 110 |
Built-in preferences |
| 111 |
-------------------- |
| 112 |
Brian itself defines the following preferences: |
| 113 |
|
| 114 |
.. document_brian_prefs:: |
| 115 |
:nolinks: |