Wednesday, March 25, 2015

Murano: Couple words about take and skip functions

Almighty little functions

 There is a list type in Murano. It behaves as classical array so that it is possible to have an access to the each element of the list via index. In contrary to Python, there are no slices via indexes in Murano and sometime you really miss them.

Fortunately in Juno release new functions were introduced: list.take(N) and list.skip(N). These little functions make your life easier when you need to work with lists in Murano. Their definition can be found here: https://github.com/stackforge/murano/blob/master/murano/engine/system/yaql_functions.py#L305-L312

As you see, this is just a wrapper around itertool.islice() function which does all the work for you.

These functions take and skip does exactly what you expect from them. List.take(N) returns a list which contains first N elements of the original list. List.skip(N) returns a list of elements starting from N-th element of the original list till the end. And, of course, it is possible to combine these functions to have something like that list.skip(N).take(M) which returns a list which contains elements [N...N+M] of the original list.

Currently our clustering code uses these functions. Lets imagine that you have a cluster with maximum number of instances equal 10. It means that there is a list "instances" which contains 10 elements. Sometime you need to create only few of these cluster instances so that initially after the first deployment you will have only 2 instances running. That is exactly work for take function. instances.take($.currentCount).pselect($.deploy) will initiate deployment only for initial number $.currentCount of instances.

Happy coding!


No comments:

Post a Comment