Package ganeti :: Package utils :: Module algo
[hide private]
[frames] | no frames]

Module algo

source code

Utility functions with algorithms.

Classes [hide private]
  RunningTimeout
Class to calculate remaining timeout when doing several operations.
Functions [hide private]
list
UniqueSequence(seq)
Returns a list with unique elements.
source code
dict
JoinDisjointDicts(dict_a, dict_b)
Joins dictionaries with no conflicting keys.
source code
list
FindDuplicates(seq)
Identifies duplicates in a list.
source code
 
_NiceSortTryInt(val)
Attempts to convert a string to an integer.
source code
 
NiceSortKey(value)
Extract key for sorting.
source code
list
NiceSort(values, key=None)
Sort a list of strings based on digit and non-digit groupings.
source code
 
InvertDict(dict_in)
Inverts the key/value mapping of a dict.
source code
 
InsertAtPos(src, pos, other)
Inserts other at given pos into src.
source code
dict
SequenceToDict(seq, key=compat.fst)
Converts a sequence to a dictionary with duplicate detection.
source code
 
_MakeFlatToDict(data)
Helper function for FlatToDict.
source code
 
FlatToDict(data, field_sep="/")
Converts a flat structure to a fully fledged dict.
source code
Variables [hide private]
  _SORTER_GROUPS = 8
  _SORTER_RE = re.compile("^%s(.*)$" %(_SORTER_GROUPS* r"(\D+|\d...

Imports: re, time, itertools, compat, text


Function Details [hide private]

UniqueSequence(seq)

source code 

Returns a list with unique elements.

Element order is preserved.

Parameters:
  • seq (sequence) - the sequence with the source elements
Returns: list
list of unique elements from seq

JoinDisjointDicts(dict_a, dict_b)

source code 

Joins dictionaries with no conflicting keys.

Enforces the constraint that the two key sets must be disjoint, and then merges the two dictionaries in a new dictionary that is returned to the caller.

Parameters:
  • dict_a (dict) - the first dictionary
  • dict_b (dict) - the second dictionary
Returns: dict
a new dictionary containing all the key/value pairs contained in the two dictionaries.

FindDuplicates(seq)

source code 

Identifies duplicates in a list.

Does not preserve element order.

Parameters:
  • seq (sequence) - Sequence with source elements
Returns: list
List of duplicate elements from seq

NiceSort(values, key=None)

source code 

Sort a list of strings based on digit and non-digit groupings.

Given a list of names ['a1', 'a10', 'a11', 'a2'] this function will sort the list in the logical order ['a1', 'a2', 'a10', 'a11'].

The sort algorithm breaks each name in groups of either only-digits or no-digits. Only the first eight such groups are considered, and after that we just use what's left of the string.

Parameters:
  • values (list) - the names to be sorted
  • key (callable or None) - function of one argument to extract a comparison key from each list element, must return string
Returns: list
a copy of the name list sorted with our algorithm

InvertDict(dict_in)

source code 

Inverts the key/value mapping of a dict.

Parameters:
  • dict_in - The dict to invert
Returns:
the inverted dict

InsertAtPos(src, pos, other)

source code 

Inserts other at given pos into src.

Parameters:
  • src (list) - The source list in which we want insert elements
  • pos (int) - The position where we want to start insert other
  • other (list) - The other list to insert into src
Returns:
A copy of src with other inserted at pos

Note: This function does not modify src in place but returns a new copy

SequenceToDict(seq, key=compat.fst)

source code 

Converts a sequence to a dictionary with duplicate detection.

Parameters:
  • seq (sequen) - Input sequence
  • key (callable) - Function for retrieving dictionary key from sequence element
Returns: dict

_MakeFlatToDict(data)

source code 

Helper function for FlatToDict.

This function is recursively called

Parameters:
  • data - The input data as described in FlatToDict, already splitted
Returns:
The so far converted dict

FlatToDict(data, field_sep="/")

source code 

Converts a flat structure to a fully fledged dict.

It accept a list of tuples in the form:

 [
   ("foo/bar", {"key1": "data1", "key2": "data2"}),
   ("foo/baz", {"key3" :"data3" }),
 ]

where the first element is the key separated by field_sep.

This would then return:

 {
   "foo": {
     "bar": {"key1": "data1", "key2": "data2"},
     "baz": {"key3" :"data3" },
     },
 }
Parameters:
  • data (list of tuple) - Input list to convert
  • field_sep (str) - The separator for the first field of the tuple
Returns:
A dict based on the input list

Variables Details [hide private]

_SORTER_RE

Value:
re.compile("^%s(.*)$" %(_SORTER_GROUPS* r"(\D+|\d+)?"))