def test_ordered_dict(self):
     try:
         from collections import OrderedDict
     except ImportError:
         pass
     else:
         self.assertFalse(is_listy(OrderedDict()))
         self.assertFalse(is_listy(OrderedDict({"a": "A"})))
Exemple #2
0
 def test_ordered_dict(self):
     try:
         from collections import OrderedDict
     except ImportError:
         pass
     else:
         assert not is_listy(OrderedDict())
         assert not is_listy(OrderedDict({'a': 'A'}))
Exemple #3
0
 def test_sized_builtin(self):
     sized = [(), (1,), [], [1], set(), set([1]), frozenset(),
              frozenset([1]), bytearray(), bytearray(1)]
     if six.PY2:
         sized.extend(
             [xrange(0), xrange(2), buffer(''), buffer('x')])  # noqa: F821
     for x in sized:
         assert is_listy(x)
Exemple #4
0
    def test_user_defined_types(self):

        class AlwaysEmptySequence(Sequence):
            def __len__(self): return 0

            def __getitem__(self, i): return [][i]

        assert is_listy(AlwaysEmptySequence())

        class AlwaysEmptySet(Set):
            def __len__(self): return 0

            def __iter__(self): return iter([])

            def __contains__(self, x): return False

        assert is_listy(AlwaysEmptySet())
def splitify(value, separator=",", strip=True, include_empty=False):
    """
    Convert a value to a list using a supercharged `split()`.

    If `value` is a string, it is split by `separator`. If `separator` is
    `None` or empty, no attempt to split is made, and `value` is returned as
    the only item in a list.

    If `strip` is `True`, then the split strings will be stripped of
    whitespace. If `strip` is a string, then the split strings will be
    stripped of the given string.

    If `include_empty` is `False`, then empty split strings will not be
    included in the returned list.

    If `value` is `None` an empty list is returned.

    If `value` is already "listy", it is returned as-is.

    If `value` is any other type, it is returned as the only item in a list.

    >>> splitify("first item, second item")
    ['first item', 'second item']
    >>> splitify("first path: second path: :skipped empty path", ":")
    ['first path', 'second path', 'skipped empty path']
    >>> splitify(["already", "split"])
    ['already', 'split']
    >>> splitify(None)
    []
    >>> splitify(1969)
    [1969]
    """
    if is_listy(value):
        return value

    if isinstance(value, str) and separator:
        parts = value.split(separator)
        if strip:
            strip = None if strip is True else strip
            parts = [s.strip(strip) for s in parts]
        return [s for s in parts if include_empty or s]
    return listify(value)
Exemple #6
0
 def test_frozenset(self):
     assert is_listy(frozenset())
     assert is_listy(frozenset(['a', 'b', 'c']))
Exemple #7
0
 def test_dict(self):
     assert not is_listy({})
     assert not is_listy({'a': 'A'})
 def test_tuple(self):
     self.assertTrue(is_listy(tuple()))
     self.assertTrue(is_listy(("a", "b", "c")))
Exemple #9
0
 def test_tuple(self):
     assert is_listy(tuple())
     assert is_listy(('a', 'b', 'c'))
Exemple #10
0
 def test_list(self):
     assert is_listy([])
     assert is_listy(['a', 'b', 'c'])
 def test_frozenset(self):
     self.assertTrue(is_listy(frozenset()))
     self.assertTrue(is_listy(frozenset(["a", "b", "c"])))
Exemple #12
0
 def test_string(self):
     assert not is_listy('')
     assert not is_listy('test')
     assert not is_listy(u(''))
     assert not is_listy(u('test'))
Exemple #13
0
    def test_miscellaneous(self):
        class Foo(object):
            pass

        for x in [0, 1, False, True, Foo, object, object()]:
            assert not is_listy(x)
Exemple #14
0
 def test_none(self):
     assert not is_listy(None)
Exemple #15
0
 def test_unsized_builtin(self):
     assert not is_listy(iter([]))
     assert not is_listy(i for i in range(2))
 def test_string(self):
     self.assertFalse(is_listy(""))
     self.assertFalse(is_listy("test"))
     self.assertFalse(is_listy(u("")))
     self.assertFalse(is_listy(u("test")))
Exemple #17
0
 def test_excluded(self):
     assert not is_listy({})
     assert not is_listy(u(''))
     assert not is_listy('')
     assert not is_listy(b'')
 def test_list(self):
     self.assertTrue(is_listy([]))
     self.assertTrue(is_listy(["a", "b", "c"]))
 def test_object(self):
     self.assertFalse(is_listy(object()))
Exemple #20
0
 def test_object(self):
     assert not is_listy(object())
Exemple #21
0
 def test_set(self):
     assert is_listy(set())
     assert is_listy(set(['a', 'b', 'c']))
 def test_none(self):
     self.assertFalse(is_listy(None))
 def test_dict(self):
     self.assertFalse(is_listy({}))
     self.assertFalse(is_listy({"a": "A"}))