Convert an object to a list if it is not already an iterable.

    Nones are returned unaltered.

    This is an awful function that proliferates an explosion of types, please do not use anymore.
976058
def to_iter(obj):
    
    if isinstance(obj, type(None)):
        return None
    elif isinstance(obj, six.string_types):
        return [obj]
    else:
        # Nesting here since symmetry is broken in isinstance checks.
        # Strings are iterables in python 3, so the relative order of if statements is important.
        if isinstance(obj, collections.Iterable):
            return obj
        else:
            return [obj]
558284
def ensure_list(obj):
        
        if not obj:
            return []
        elif isinstance(obj, (str, unicode, bytes, bytearray)):
            return [obj]
        elif hasattr(obj, '__iter__') or hasattr(obj, '__getitem__'):
            return list(obj)
        else:
            return [obj]
420657
public static <T> List<T> iterableToList(Iterable<T> iterable) {
        if (iterable == null) {
            return Collections.emptyList();
        }
        if (iterable instanceof List) {
            return (List<T>) iterable;
        }
        Iterator<T> i = iterable.iterator();
        if (i.hasNext()) {
            List<T> list = new ArrayList<>();
            while (i.hasNext()) {
                list.add(i.next());
            }
            return list;
        }
        return Collections.emptyList();
    }
358179
def _AsList(arg):
    """"""
    if (isinstance(arg, string_types) or
        not isinstance(arg, collections.Iterable)):
      return [arg]
    else:
      return list(arg)
1277679
def from_iterable(cls, iterable: Iterable) -> 'List':
        """"""

        iterator = iter(iterable)

        def recurse() -> List:
            try:
                value = next(iterator)
            except StopIteration:

                return List.empty()
            return List.unit(value).append(recurse())
        return List.empty().append(recurse())
706704
def filtered(self, *, type_=None, lang=None, attrs={}):
        
        return list(self.filter(type_=type_, lang=lang, attrs=attrs))
451031
function listToObject(list, key) {
        var length, result, i, obj, keyValue;
        if (!list) {
            list = [];
        }
        length = list.length;
        result = { valids: {}, discarded: [] };
        for (i = 0; i < length; i += 1) {
            obj = list[i];
            keyValue = obj[key];
            // discard the object without a key
            if (keyValue) {
                result.valids[keyValue] = obj;
            } else {
                result.discarded.push(obj);
            }
        }
        return result;
    }
769220
function(obj) {

    var __p = 'var index, iterable = ' +
    (obj.firstArg) +
    ', result = ' +
    (obj.init) +
    ';\nif (!iterable) return result;\n' +
    (obj.top) +
    ';';
     if (obj.array) {
    __p += '\nvar length = iterable.length; index = -1;\nif (' +
    (obj.array) +
    ') {  ';
     if (support.unindexedChars) {
    __p += '\n  if (isString(iterable)) {\n    iterable = iterable.split(\'\')\n  }  ';
     }
    __p += '\n  while (++index < length) {\n    ' +
    (obj.loop) +
    ';\n  }\n}\nelse {  ';
     } else if (support.nonEnumArgs) {
    __p += '\n  var length = iterable.length; index = -1;\n  if (length && isArguments(iterable)) {\n    while (++index < length) {\n      index += \'\';\n      ' +
    (obj.loop) +
    ';\n    }\n  } else {  ';
     }

     if (support.enumPrototypes) {
    __p += '\n  var skipProto = typeof iterable == \'function\';\n  ';
     }

     if (support.enumErrorProps) {
    __p += '\n  var skipErrorProps = iterable === errorProto || iterable instanceof Error;\n  ';
     }

        var conditions = [];    if (support.enumPrototypes) { conditions.push('!(skipProto && index == "prototype")'); }    if (support.enumErrorProps)  { conditions.push('!(skipErrorProps && (index == "message" || index == "name"))'); }

     if (obj.useHas && obj.useKeys) {
    __p += '\n  var ownIndex = -1,\n      ownProps = objectTypes[typeof iterable] && keys(iterable),\n      length = ownProps ? ownProps.length : 0;\n\n  while (++ownIndex < length) {\n    index = ownProps[ownIndex];\n';
        if (conditions.length) {
    __p += '    if (' +
    (conditions.join(' && ')) +
    ') {\n  ';
     }
    __p += 
    (obj.loop) +
    ';    ';
     if (conditions.length) {
    __p += '\n    }';
     }
    __p += '\n  }  ';
     } else {
    __p += '\n  for (index in iterable) {\n';
        if (obj.useHas) { conditions.push("hasOwnProperty.call(iterable, index)"); }    if (conditions.length) {
    __p += '    if (' +
    (conditions.join(' && ')) +
    ') {\n  ';
     }
    __p += 
    (obj.loop) +
    ';    ';
     if (conditions.length) {
    __p += '\n    }';
     }
    __p += '\n  }    ';
     if (support.nonEnumShadows) {
    __p += '\n\n  if (iterable !== objectProto) {\n    var ctor = iterable.constructor,\n        isProto = iterable === (ctor && ctor.prototype),\n        className = iterable === stringProto ? stringClass : iterable === errorProto ? errorClass : toString.call(iterable),\n        nonEnum = nonEnumProps[className];\n      ';
     for (k = 0; k < 7; k++) {
    __p += '\n    index = \'' +
    (obj.shadowedProps[k]) +
    '\';\n    if ((!(isProto && nonEnum[index]) && hasOwnProperty.call(iterable, index))';
            if (!obj.useHas) {
    __p += ' || (!nonEnum[index] && iterable[index] !== objectProto[index])';
     }
    __p += ') {\n      ' +
    (obj.loop) +
    ';\n    }      ';
     }
    __p += '\n  }    ';
     }

     }

     if (obj.array || support.nonEnumArgs) {
    __p += '\n}';
     }
    __p += 
    (obj.bottom) +
    ';\nreturn result';

    return __p
  }
837442
def asarray(x, dtype=None):
    ''''''
    iterable = scalarasiter(x)
    if isinstance(iterable, ndarray):
        return iterable
    else:
        if not hasattr(iterable, '__len__'):
            iterable = list(iterable)
        if dtype == object_type:
            a = ndarray((len(iterable),), dtype=dtype)
            for i,v in enumerate(iterable):
                a[i] = v
            return a
        else:
            return array(iterable, dtype=dtype)
665868
def to_utf8_optional_iterator(x):
    
    if isinstance(x, STRING_TYPES):
        return to_utf8(x)

    try:
        l = list(x)
    except TypeError as e:
        assert 'is not iterable' in str(e)
        return x
    else:
        return [ to_utf8_if_string(e) for e in l ]
709897
def dictlist_convert_to_int(dict_list: Iterable[Dict], key: str) -> None:
    
    for d in dict_list:
        try:
            d[key] = int(d[key])
        except ValueError:
            d[key] = None
591702
def require_iterable_of(objs, types, name=None, type_name=None, truncate_at=80):
    
    # Fast pass for common case where all types are correct.
    # This avoids the more expensive loop below. A typical speedup from this
    # optimization is 6.6 sec -> 1.7 sec, for testing a list of size 10,000,000.
    try:
        if all(isinstance(obj, types) for obj in objs):
            return
    except TypeError:
        # We don't require that objs is a list in this function, just that it's
        # iterable. We specify 'list' below as a convenient way to throw the
        # desired error.
        require_instance(objs, list, name, "iterable", truncate_at)

    # Some type isn't correct. We reuse the require_instance function to raise
    # the exception.
    prefix = ("%s: " % name) if name else ""
    for (i, obj) in enumerate(objs):
        element_name = prefix + ("element at index %d" % i)
        require_instance(obj, types, element_name, type_name, truncate_at)
    assert False, "Shouldn't reach here."
371106
def listify(p:OptListOrItem=None, q:OptListOrItem=None):
    ""
    if p is None: p=[]
    elif isinstance(p, str):          p = [p]
    elif not isinstance(p, Iterable): p = [p]
    #Rank 0 tensors in PyTorch are Iterable but don't have a length.
    else:
        try: a = len(p)
        except: p = [p]
    n = q if type(q)==int else len(p) if q is None else len(q)
    if len(p)==1: p = p * n
    assert len(p)==n, f'List len mismatch ({len(p)} vs {n})'
    return list(p)
1104034
def _to_list(obj):
    '''
    
    '''
    ret = {}

    for attr in __attrs:
        if hasattr(obj, attr):
            ret[attr] = getattr(obj, attr)
    return ret
181558
def wrap_iterable(obj):
    
    was_scalar = not isiterable(obj)
    wrapped_obj = [obj] if was_scalar else obj
    return wrapped_obj, was_scalar
314579
public static <T> List<T> toList(Iterable<T> iterable)
    {
        Objects.requireNonNull(iterable, "The iterable is null");
        return Iterables.toCollection(iterable, new ArrayList<T>());
    }
1091159
def __getiterable(value):   # ToDo: refactor
        
        if isinstance(value, Selection):
            return [value]
        try:
            for selection in value:
                if not isinstance(selection, Selection):
                    raise TypeError
            return list(value)
        except TypeError:
            raise TypeError(
                f'Binary operations on Selections objects are defined for '
                f'other Selections objects, single Selection objects, or '
                f'iterables containing `Selection` objects, but the type of '
                f'the given argument is `{objecttools.classname(value)}`.')