示例#1
0
def filemap_to_flag_list(filemap: dict = None):
    """Convert a map of command line flags and filenames to a list of command line arguments.

    Used to map inputs and outputs of command line tools to and from gmxapi data handles.
    User provides mappings of flags and filenames so that gmxapi can construct an
    executable command line.

    Primary use case is implicit. commandline_operation() instantiates this operation based on
    user input, and sends the output to cli()

    Arguments:
        filemap: key-value map of command line flags and filename arguments

    Returns:
        list of strings and/or gmxapi data references
    """
    result = []
    if filemap is not None:
        for key, value in filemap.items():
            # Note that the value may be a string, a list, an ndarray, or a future
            if not isinstance(value, (list, tuple, NDArray)):
                if hasattr(value, 'result') and value.dtype == NDArray:
                    pass
                elif hasattr(value, 'result') and value.dtype != NDArray:
                    # TODO: Fix this ugly hack when we have proper Future slicing and can make NDArray futures.
                    result_function = value.result
                    value.result = lambda function=result_function: [
                        function()
                    ]
                else:
                    value = [value]
            result = gmx.join_arrays(front=result,
                                     back=gmx.join_arrays(front=[key],
                                                          back=value))
    return result
示例#2
0
    def test_list(self):
        list_a = [1, 2, 3]

        # TODO: test input validation
        list_result = gmx.concatenate_lists(sublists=[list_a])
        assert list_result.dtype == gmx.datamodel.NDArray
        # Note: this is specifically for the built-in tuple type.
        # Equality comparison may work differently for different sequence types.
        assert tuple(list_result.result()) == tuple(list_a)
        assert len(list_result.result()) == len(list_a)

        list_result = gmx.concatenate_lists([list_a, list_a])
        assert len(list_result.result()) == len(list_a) * 2
        assert tuple(list_result.result()) == tuple(list_a + list_a)

        list_b = gmx.ndarray([42])

        list_result = gmx.concatenate_lists(sublists=[list_b])
        assert list_result.result()[0] == 42

        list_result = gmx.join_arrays(front=list_a, back=list_b)
        assert len(list_result.result()) == len(list_a) + 1
        assert tuple(list_result.result()) == tuple(list(list_a) + [42])