Utilities

Functions on tables

halmd.utility.empty(t)

Test if table is empty.

halmd.utility.keys(t)

Returns table with sorted keys of table t as values.

halmd.utility.sorted(t)

Returns iterator over pairs of table t sorted by key.

halmd.utility.reverse(t)

Returns table with keys as values and values as keys.

Functions on strings

halmd.utility.interp(s, t)

Interpolate strings supporting Pythonic formatting specifications (see http://lua-users.org/wiki/StringInterpolation, example by RiciLake)

Parameters:
  • s (string) – formatting string
  • t (table) – (key, value) pairs
Returns:

interpolated copy of string ‘s’

Occurrences of ‘{key:fmt}’ in the formatting string are substituted by ‘(“%fmt”):format(value)’, where ‘fmt’ is one of the C-printf formatting codes. The method is added to the string type as string.interp.

Example:

print( (“{name:s} is {val:7.2f}%”):interp({name = “concentration”, val = 56.2795}) )

–> “concentration is 56.28%”

Assertions

halmd.utility.assert_kwarg(args, key, level)

Assert keyword argument of table and return its value.

Parameters:
  • args (table) – argument table
  • key (string) – parameter key
  • level (number) – call stack level for error message (default: 2)
Returns:

args[key]

halmd.utility.assert_type(var, name, level)

Assert type of variable.

Parameters:
  • var – variable to check
  • name (string) – Lua type name
  • level (number) – call stack level for error message (default: 2)
Returns:

var