import six


def _cell(item, column, key, fmt):
    """Constructs a value in table cell."""
    if key is None:
        key = column

    if isinstance(key, six.string_types):
        keys = [key]
    else:
        keys = key

    raw_value = None
    for k in keys:
        if k in item and item[k] is not None:
            raw_value = item[k]
            break

    if callable(fmt):
        try:
            value = fmt(raw_value)
        except Exception:  # pylint: disable=W0703
            if raw_value is None:
                value = '-'
            else:
                raise
    else:
        if raw_value is None:
            value = '-'
        else:
            if isinstance(raw_value, list):
                value = ','.join(six.moves.map(str, raw_value))
            else:
                value = raw_value
    return value