| 1 |
Persistence |
| 2 |
=========== |
| 3 |
|
| 4 |
Persistence describes the ability for the plugins to persist data even |
| 5 |
if Errbot is restarted. |
| 6 |
|
| 7 |
How to use it |
| 8 |
------------- |
| 9 |
|
| 10 |
Your plugin *is* the store, simply use self as a dictionary. |
| 11 |
|
| 12 |
Here is a simple example for storing and retrieving a value from the store. |
| 13 |
|
| 14 |
.. code-block:: python |
| 15 |
|
| 16 |
from errbot import BotPlugin, botcmd |
| 17 |
|
| 18 |
class PluginExample(BotPlugin): |
| 19 |
@botcmd |
| 20 |
def remember(self, msg, args): |
| 21 |
self['TODO'] = args |
| 22 |
|
| 23 |
@botcmd |
| 24 |
def recall(self, msg, args): |
| 25 |
return self['TODO'] |
| 26 |
|
| 27 |
Caveats |
| 28 |
------- |
| 29 |
|
| 30 |
The storing occurs when you *assign the key*: |
| 31 |
|
| 32 |
.. code-block:: python |
| 33 |
|
| 34 |
# THIS WON'T WORK |
| 35 |
d = {} |
| 36 |
self['FOO'] = d |
| 37 |
d['subkey'] = 'NONONONONONO' |
| 38 |
|
| 39 |
What you need to do instead: |
| 40 |
(manual method) |
| 41 |
|
| 42 |
.. code-block:: python |
| 43 |
|
| 44 |
# THIS WORKS |
| 45 |
d = {} |
| 46 |
self['FOO'] = d |
| 47 |
|
| 48 |
# later ... |
| 49 |
d['subkey'] = 'NONONONONONO' |
| 50 |
self['FOO'] = d # restore the full key if something changed in memory. |
| 51 |
|
| 52 |
Or use the mutable contex manager: |
| 53 |
|
| 54 |
.. code-block:: python |
| 55 |
|
| 56 |
# THIS WORKS AND IS CLEANER |
| 57 |
d = {} |
| 58 |
self['FOO'] = d |
| 59 |
|
| 60 |
# later ... |
| 61 |
|
| 62 |
with self.mutable('FOO') as d: |
| 63 |
d['subkey'] = 'NONONONONONO' |
| 64 |
# it will save automatically the key |