Util – utility module

This is the utility module, with some utility functions of general use, like list item swap, random utilities and etc.

Util.getch()
Linux platform function to get a pressed key
Util.kbhit()
The linux implementation of the kbhit() function
Util.list2DSwapElement(lst, indexa, indexb)

Swaps elements A and B in a 2D list (matrix).

Example:
>>> l = [ [1,2,3], [4,5,6] ] 
>>> Util.list2DSwapElement(l, (0,1), (1,1) )
>>> l
[[1, 5, 3], [4, 2, 6]]
Parameters:
  • lst – the list
  • indexa – the swap element A
  • indexb – the swap element B
Return type:

None

Util.listSwapElement(lst, indexa, indexb)

Swaps elements A and B in a list.

Example:
>>> l = [1, 2, 3]
>>> Util.listSwapElement(l, 1, 2)
>>> l
[1, 3, 2]
Parameters:
  • lst – the list
  • indexa – the swap element A
  • indexb – the swap element B
Return type:

None

Util.raiseException(message, expt=None)

Raise an exception and logs the message.

Example:
>>> Util.raiseException('The value is not an integer', ValueError)
Parameters:
  • message – the message of exception
  • expt – the exception class
Return type:

None

Util.randomFlipCoin(p)

Returns True with the p probability. If the p is 1.0, the function will always return True, or if is 0.0, the function will return always False.

Example:
>>> Util.randomFlipCoin(1.0)
True
Parameter:p – probability, between 0.0 and 1.0
Return type:True or False
Util.set_curses_term()
This is a linux platform function to set the term to curses
Util.set_normal_term()
This is a linux platform function to set the term back to normal

Recent Blog Posts

Previous topic

Consts – constants module

Next topic

Interaction – interaction module

This Page