Table of Contents
Introduction
When programming in Python it is possible to access the kdb database, changing values of existing keys, adding and deleting keys and a few other things.
Installation
Either build the package or install from a repository.
Alpine Linux
The python bindings package is only available in the testing repository (as of 2019-04-29).
1 docker run -it alpine:edge /bin/sh
2 echo "https://dl-cdn.alpinelinux.org/alpine/edge/testing" >> /etc/apk/repositories
3 # Install elektra and the python bindings
4 apk update && apk add elektra elektra-python py3-elektra
Under regular alpine, you have to install python3 from the edge repository. If you do not want to add the edge repositories permanently like above, you can do
1 apk add --repository "https://dl-cdn.alpinelinux.org/alpine/edge/main" python3
2 apk add --repository "https://dl-cdn.alpinelinux.org/alpine/edge/testing" elektra elektra-python py3-elektra
Debian
The Elektra python-binding is currently built for:
- Debian 11
bullseye
- Debian 10
buster
1 docker run -it debian:bullseye
3 apt-get install ca-certificates
4 apt-get install vim gnupg
5 apt-key adv --keyserver keyserver.ubuntu.com --recv-keys F26BBE02F3C315A19BF1F791A9A25CC1CC83E839
6 vim /etc/apt/sources.list
Append deb https://debs.libelektra.org/bullseye bullseye main
to /etc/apt/sources.list
and install it:
2 apt-get install python3-elektra
Import kdb
In order to being able to use kdb
, you at first need to import kdb
. You need access to a Python object of KDB
. This is accomplished by calling kdb.KDB()
and saving this to a variable because later on this object will be needed for various operations. The easiest way to do this would be:
4 print(
"Hello world! I have a kdb-instance! :D")
Keyset
A keyset is basically a list of the keys that lie within the specified range of the database. When creating an empty keyset this range is obviously zero. It is possible to load the whole database into a keyset but in a lot of cases this is not needed and you can specify which keys exactly you need (which I mean with specified range). At first it is necessary to create a new keyset. When simply calling kdb.KeySet()
the keyset is of size 0. There is no restriction to the keyset's size. It is possible to specify a certain (maximum) size for a keyset. To load keys into to keyset from the database you simply call the method get
provided by the kdb-object.
It is also possible to iterate as expected over a keyset and use len
, reversed
and copying. The elements of a keyset can be accessed by indexes and a keyset can be sliced. Another way of accessing a key is by the key name (`keyset_name['/path/to/keys/key_name']). If a key with the given name does not exist within the keyset, a
KeyError` exception is thrown.
An example that shows how to load an existing keyset and then access every key and value of the loaded keyset:
11 print(
"key: {} value: {}".format(key, key.value))
Here an example of how you can easily check if a key exists:
5 path =
'{}/test'.format(namespace)
11 print(
"The value of the key {} is {}!".format(ks[path], ks[path]
14 print(
"The key {} does not exist!".format(path))
Ways of copying a keyset:
7 ks_deepcopy = copy.deepcopy(ks)
9 ks_shallowcopy = copy.copy(ks)
Slicing works just like for normal lists in Python. But be careful: Afterwards the result will be a list - not a keyset.
8 ks_copy_by_slicing = ks[:]
22 for keyset
in [a, b, c]:
25 print(
'{}: {}'.format(key, key.value))
26 except UnicodeDecodeError:
If you have changed anything in the keyset and want those changes to be saved to the database, you need to call set
which is just like get
provided by the kdb-object.
An example of everything up until now could look like this:
5 k.get(ks,
'/path/to/keys')
10 k.set(ks,
'/path/to/keys')
If you have a key a very simple way to get its name and value:
7 print(
"key name: {}".format(key.name))
8 print(
"{0}{1}\n{0}{2}\n".format(
"key value: ", key.value,
9 ks.lookup(key).string))
Keys
It is possible to create new keys:
6 new_key =
kdb.Key(
'/user/sw/pk/key_name', kdb.KEY_VALUE,
'key_value')
7 print(
"{}: {}".format(new_key, new_key.value))
You can also duplicate a key:
4 key1 =
kdb.Key(
'/user/sw/pk/key_name', kdb.KEY_VALUE,
'key_value')
6 print(
"{}: {}".format(new_key, new_key.value))
7 print(
"{}: {}".format(key2, key2.value))
An example for working with meta-keys
4 key1 =
kdb.Key(
"user:/key1", kdb.KEY_VALUE,
"some_value")
5 key1.setMeta(
"foo",
"bar")
6 key1.setMeta(
"owner",
"manuel")
7 key1.setMeta(
"comment/#0",
"this is my example key")
8 for meta
in key1.getMeta():
9 print(
" key1.{0} = \"{1}\"".format(meta.name, meta.value))
Keys can be added to a keyset using append
. If the key already exists, the value will be updated. Calling `keyset_name['/path/to/key'] = 'new_valuedoes not work for updating keys already in a keyset. Keys can be removed with
pop,
removeor
cut`
1 from kdb
import KDB, KEY_VALUE, Key, KeySet
6 """Return a textual representation of this keyset."""
8 [
'{}: {}'.format(key.name, key.value)
for key
in self])
12 """Create a new key with the given name and value."""
13 return Key(name, KEY_VALUE, value)
16 def describe(keyset, title, newline=True):
17 return '{}\n{}\n{}{}'.format(title,
'=' * len(title), keyset,
18 '\n' if newline
else '')
23 data.get(keyset,
'user')
24 print(describe(keyset,
"Initial Keyset"))
26 new_key = key(
'user:/sw/pk/key_name',
'key_value')
29 keyset.append(new_key)
30 print(describe(keyset,
"Add New Key"))
32 newer_key = key(
'user:/sw/pk/key_name',
'other_key_value')
35 keyset.append(newer_key)
36 print(describe(keyset,
"Replace Key", newline=
False))
Merging KeySets
The internal three-way merge algorithm is also included in the Python bindings.
4 kdb.Key(
"system:/test/key1",
"k1"),
5 kdb.Key(
"system:/test/key2",
"k2"),
10 kdb.Key(
"system:/test/key1",
"k1"),
11 kdb.Key(
"system:/test/key3",
"k3"),
16 kdb.Key(
"system:/test/key1",
"k1"),
17 kdb.Key(
"system:/test/key4",
"k4"),
21 base = kdb.merge.MergeKeys(baseKeys,
kdb.Key(
"system:/test"))
22 theirs = kdb.merge.MergeKeys(theirKeys,
kdb.Key(
"system:/test"))
23 ours = kdb.merge.MergeKeys(ourKeys,
kdb.Key(
"system:/test"))
25 merger = kdb.merge.Merger()
26 mergeResult = merger.merge(base, ours, theirs,
kdb.Key(
"system:/test"), kdb.merge.ConflictStrategy.THEIR)
29 print(mergeResult.mergedKeys)