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.
- halmd.utility.table_shallow_copy(t)¶
Create a shallow copy of a table, i.e., return a new table with shallow copies of all the original table’s elements.
- halmd.utility.repeat_element(x, n)¶
Return an array with x n-times repeated (at keys 1, …, n).
If x is a table, shallow copies of its elements are created.
- halmd.utility.concat(t1, t2)¶
Concatenates two tables t1 and t2.
Only the array part of the tables are used, i.e., elements with integer indices.
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)
, wherefmt
is one of the C-printf formatting codes. The method is added to thestring
type asstring.interp
.Example:
print( (“{name:s} is {val:7.2f}%”):interp({name = “concentration”, val = 56.2795}) )
–> “concentration is 56.28%”
Functions on filesystem paths¶
- halmd.utility.dirname(path)¶
Return directory name of given path.
- Parameters:
path (string) – pathname of a file, etc.
- Returns:
directory component of
path
The function splits
path
at the most-right/
. It returns the empty string as the default.
- halmd.utility.abspath(relpath)¶
Convert relative path to an absolute path.
- Parameters:
relpath (string) – pathname
- Returns:
absolute version of
relpath
The function prepends the absolute path of the currently executed Lua script
arg[0]
torelpath
.
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