Add is_sequence util function
This is a helper for identifying whether the var is a sequence, but is not of string-like type (optionally). Co-authored-by: Toshio Kuratomi <toshio@fedoraproject.org> Co-authored-by: Brian Coca <briancoca+dev@gmail.com> Co-authored-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
eb209e92c9
commit
52219c4d55
1 changed files with 29 additions and 0 deletions
29
lib/ansible/module_utils/common/collections.py
Normal file
29
lib/ansible/module_utils/common/collections.py
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
# Copyright (c), Sviatoslav Sydorenko <ssydoren@redhat.com> 2018
|
||||||
|
# Simplified BSD License (see licenses/simplified_bsd.txt or https://opensource.org/licenses/BSD-2-Clause)
|
||||||
|
"""Collection of low-level utility functions."""
|
||||||
|
|
||||||
|
from __future__ import absolute_import, division, print_function
|
||||||
|
__metaclass__ = type
|
||||||
|
|
||||||
|
|
||||||
|
from ..six import binary_type, text_type
|
||||||
|
from ._collections_compat import Sequence
|
||||||
|
|
||||||
|
|
||||||
|
def is_string(seq):
|
||||||
|
"""Identify whether the input has a string-like type (inclding bytes)."""
|
||||||
|
return isinstance(seq, (text_type, binary_type))
|
||||||
|
|
||||||
|
|
||||||
|
def is_sequence(seq, include_strings=False):
|
||||||
|
"""Identify whether the input is a sequence.
|
||||||
|
|
||||||
|
Strings and bytes are not sequences here,
|
||||||
|
unless ``include_string`` is ``True``.
|
||||||
|
|
||||||
|
Non-indexable things are never of a sequence type.
|
||||||
|
"""
|
||||||
|
if not include_strings and is_string(seq):
|
||||||
|
return False
|
||||||
|
|
||||||
|
return isinstance(seq, Sequence)
|
Loading…
Reference in a new issue