are keyword arguments that are converted to action parameters and passed to the action. The **kwargs argument is always specified on actions as well. It enables the use of extra parameters for debugging and system use. Now let’s look at the descriptions of the parameters (the following output is continued from the preceding help? invocation).
Docstring:
Shows the parameters for an action or lists all available actions
Parameters
----------
action : string, optional
specifies the name of the action for which you want help. The name
can be in the form 'actionSetName.actionName' or just
'actionName’.
actionset : string, optional
specifies the name of the action set for which you want help. This
parameter is ignored if the action parameter is specified.
verbose : boolean, optional
when set to True, provides more detail for each parameter.
Default: True
You see that action and actionset are declared as strings, and verbose is declared as a Boolean. Action parameters can take many types of values. The following table shows the supported types:
CAS Type | Python Type | Description |
Boolean | bool | Value that indicates true or false. This should always be specified using Python’s True or False values. |
double | floatswat.float64 | 64-bit floating point number |
int32 | intswat.int32 | 32-bit integer |
int64 | long (Python 2)int (Python 3)swat.int64 | 64-bit integer |
string | Unicode (Python 2)str (Python 3) | Character content. Note that if a byte string is passed as an argument, SWAT attempts to convert it to Unicode using the default encoding. |
value list | list or dict | Collection of items. Python lists become indexed CAS value lists. Python dicts become keyed CAS value lists. |
The easiest way to practice more complex arguments is by using the echo action. This action simply prints the value of all parameters that were specified in the action call. The following code demonstrates the echo action with all of the parameter types in the preceding table.
In [13]: out = conn.echo(
...: boolean_true = True,
...: boolean_false = False,
...: double = 3.14159,
...: int32 = 1776,
...: int64 = 2**60,
...: string = u'I like snowmen! \u2603',
...: list = [u'item1', u'item2', u'item3'],
...: dict = {'key1': 'value1',
...: 'key2': 'value2',
...: 'key3': 3}
...: )
NOTE: builtin.echo called with 8 parameters.
NOTE: parameter 1: int32 = 1776
NOTE: parameter 2: boolean_false = false
NOTE: parameter 3: list = {'item1', 'item2', 'item3'}
NOTE: parameter 4: boolean_true = true
NOTE: parameter 5: int64 = 1152921504606846976
NOTE: parameter 6: double = 3.14159
NOTE: parameter 7: string = 'I like snowmen! '
NOTE: parameter 8: dict = {key1 = 'value1', key3 = 3,
key2 = 'value2'}
You might notice that the parameters are printed in a different order than what was specified in the echo call. This is simply because keyword parameters in Python are stored in a dictionary, and dictionaries don’t keep keys in a specified order.
You might also notice that the printed syntax is not Python syntax. It is a pseudo-code syntax more similar to the Lua programming language. Lua is used in other parts of CAS as well (such as the history action), so most code-like objects that are printed from CAS are in Lua or syntax that is like Lua. However, the syntax of the two languages (as far as parameter data structures goes) are similar enough that it is easy to see the mapping from one to the other. The biggest differences are in the value list parameters. Indexed lists in the printout use braces, whereas Python uses square brackets. Also, in the keyed list, Python’s keys must be quoted, and the separator that is used between the key and the value is a colon (:) rather than an equal sign (=).
The complexity of the parameter structures is unlimited. Lists can be nested inside dictionaries, and dictionaries can be nested inside lists. A demonstration of nested structures in echo follows:
In [14]: out = conn.echo(
...: list = ['item1',
...: 'item2',
...: {
...: 'key1': 'value1',
...: 'key2': {
...: 'value2': [0, 1, 1, 2, 3]
...: }
...: }
...: ])
NOTE: builtin.echo called with 1 parameters.
NOTE: parameter 1: list = {'item1', 'item2',
{key1 = 'value1',
key2 = {value2 = {0, 1, 1, 2, 3}}}}
Nested dictionary parameters are fairly common in CAS and can create some confusion with the nesting levels and differences between keyword arguments and dictionary literals. Because of this, some utility functions have been added to SWAT to aid in the construction of nested parameters.
Constructing Nested Action Parameters
While specifying dictionary parameters, you can suffer from cognitive dissonance when you switch from keyword arguments to dictionary literals. In keyword arguments, you don’t quote the name of the argument, but in dictionary literals, you do quote the key value. Also, in keyword arguments, you use an equal sign between the name and value, whereas in dictionary literals, you use a colon. Because of this potential confusion, we prefer to use the dict constructor with keyword arguments when nesting action parameters. The preceding code is shown as follows, but instead uses the dict object in Python for nested dictionaries.
In [15]: out = conn.echo(
...: list = ['item1',
...: 'item2',
...: dict(
...: key1 = 'value1',
...: key2 = dict(
...: value2 = [0, 1, 1, 2, 3]
...: )
...: