Inspirel banner

Programming Distributed Systems with YAMI4

3.5.5 Python

Simple values (boolean, integer, long and double) can be set with a single dictionary-like operation:

params["age"] = 25

Above, a new entry named ``age'' is created (or replaces the existing one with the same name) and an integer value 25 is set as its content.

String values are also handled directly:

params["first_name"] = "John"

Binary values are handled differently, depending on the target language version. In Python 2.x the yami.Bytes wrapper type has to be used in order to distinguish between binary and string values:

song_buffer = yami.Bytes(get_song_from_somewhere())

params["favorite_song"] = song_buffer

Python 3.x allows to deal with binary values directly thanks to its built-in bytes type:

song_buffer = get_song_from_somewhere()

params["favorite_song"] = song_buffer

Arrays are handled naturally as lists:

my_numbers = [7, 42, 123]

params["favorite_numbers"] = my_numbers

Arrays of strings are also handled this way:

friends = ["Alice", "Bob", "Nicole"]

params["friends"] = friends

Arrays of binary values are passed as lists of bytes buffers.

Note:

Even though it is possible to pass any iterable object when setting the array entry in the Parameters object, the library will create an internal list object containing the copy of all the elements. That is, there is no sharing of arrays and the array will be always seen as a list during reading. Lists as implementations of array entries are also expected when built-in dictionary objects are passed instead of Parameters objects.

Nested parameters allow to build some form of hierarchic data structures. Nested parameters objects are just inserted into existing objects as any other type:

nested = yami.Parameters()
# ...
params["address"] = nested

The nested object declared above is a stand-alone object, but after setting it in another object its state is shared (aliased) as with lists objects of any type.

The nested object declared above can be used just as any regular parameters object, for example:

nested["street"] = "Flowery"
nested["house_number"] = 17

Similar rules apply for nested parameters arrays.

All the example statements in this section filled a single parameters object with six entries (``age'', ``first_name'', ``favorite_song'', ``favorite_numbers'' ``friends'' and ``address''), one them being an array with 3 elements and one being a nested parameters with 2 entries.