Commit 9da5a158 authored by Vincent Michel's avatar Vincent Michel
Browse files

Merge branch 'doc_settings' into 'master'

howto about settings + improve tests on settings types.

See merge request !609
parents 0d7fa8e6 931ecdda
.. _bliss-settings-how-to:
Bliss settings how to
This chapter explains:
* how to use BLISS *settings* (SimpleSetting, HashSetting and QueueSetting)
* how to inspect key-value database so see how and what is saved in REDIS database
What are settings in BLISS
A setting is a *value*, *list* or *dictionary* you want to keep in
memory and to restore after a shutdown of your session or program.
Settings are saved in the *REDIS key-value database*. Scalar values
are saved as *strings* but ``settings`` class transtypes them for you.
Settings Classes
Here you can find various types of setting you can use in a BLISS project.
The ``SimpleSetting`` class is used to keep in memory a *scalar value*.
Python base-type (except tuples) are usable as value:
* int
* float
* string
* bool
* tuple : NO
from bliss.config import settings
magicNumber = 63825363
iii = settings.SimpleSetting('myIntkey', default_value=magicNumber)
assert iii.get() == magicNumber
assert iii.get() == 42
assert type(iii.get()) is int
The ``default_value`` is returned by the ``SimpleSetting`` object until it has been set.
Once the object has been set, the value is saved in redis database in order to be retrieved
after a shutdown of your BLISS session.
The ``HashSetting`` class is used to represent a dictionary of scalar values::
myDict = {"C1":"riri", "C2":"fifi"}
shs = settings.HashSetting('myHkey', default_values=myDict) # note the s :)
The ``QueueSetting`` class is used to represent a list of scalar values::
myList = ["a", "b", "c", "d"]
sqs = settings.QueueSetting('myQkey')
To check REDIS' entrails : ``redis-cli``
To inspect what is saved by the redis database, you can use ``redis-cli``.
(CLI stands for Command Line Interface)
To know if a key is present::
(bliss) pcsht:~/PROJECTS/bliss % redis-cli> keys myIntkey
1) "myIntkey"
or not::> keys licorne
(empty list or set)
You can also use a joker character to get all keys or a specified sub-set::> keys *
1) "myFloatkey"
2) "align_counters:default"
12) "myQkey"
13) "axis.simot1"> keys a*
1) "axis.pzth"
2) "align_counters:default"
3) "align_counters:align"
4) "axis.simot1"
5) "align_counters"
You can read the values attached to a key.
For a scalar value::> get myIntkey
For a dictionary vlaue::> hgetall axis.pzth
1) "velocity"
2) "11.0000002"
3) "offset"
4) "0"
5) "acceleration"
6) "1.0"
For a list value::> LRANGE myQkey 0 -1
1) "a"
2) "b"
3) "c"
4) "d"
Correspondence between REDIS and BLISS
Example of imbrication of BLISS and REDIS::
[80]: from bliss.config.settings import SimpleSetting
[81]: fff = SimpleSetting('fkey', default_value=321)
[82]: fff.get()
out: 321> keys fkey
(empty list or set)> get fkey
[83]: fff.set(987)> keys fkey
1) "fkey"
[84]: fff.get()
out: 987> get fkey
You can set a value or delete a key from redis-cli::
BLISS REDIS> set fkey 123
[85]: fff.get()
out: 123> get fkey
"123"> del fkey
(integer) 1> get fkey
[86]: fff.get()
out: 321 # default_value is returned
......@@ -12,3 +12,5 @@ How Tos
......@@ -18,6 +18,51 @@ def test_simple_setting(beacon):
sss.set("V2") # 'V2' key exists now in redis
assert sss.get() == 'V2'
def test_simple_setting_types(beacon):
session = beacon.get("test_session")
anIntValue = 666
iii = settings.SimpleSetting('myIntkey', default_value=anIntValue)
assert iii.get() == anIntValue
assert iii.get() == 63825363
assert type(iii.get()) is int
aFloatValue = 3.14159
fff = settings.SimpleSetting('myFloatkey', default_value=aFloatValue)
assert fff.get() == aFloatValue
assert fff.get() == pytest.approx(2.71, 0.001)
assert type(fff.get()) is float
aStringValue = "Hello World !"
sss = settings.SimpleSetting('myStringKey', default_value=aStringValue)
assert sss.get() == aStringValue
sss.set("Good bye")
assert sss.get() == "Good bye"
assert type(sss.get()) is str
aBoolValue = False
bbb = settings.SimpleSetting('myBoolKey', default_value=aBoolValue)
assert bbb.get() == aBoolValue
assert bbb.get() == True
assert type(bbb.get()) is bool
aTupleValue = (1, 2, 3)
ttt = settings.SimpleSetting('myTupleKey', default_value=aTupleValue)
assert ttt.get() == aTupleValue
ttt.set(('a', 'b', 'c'))
# Oh oH !!! this is now a string.
assert ttt.get() == "('a', 'b', 'c')"
assert type(ttt.get()) is str
def test_hash_setting(beacon):
session = beacon.get("test_session")
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment