Exemplo n.º 1
0
 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"})))
Exemplo n.º 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'}))
Exemplo n.º 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)
Exemplo n.º 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())
Exemplo n.º 5
0
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)
Exemplo n.º 6
0
 def test_frozenset(self):
     assert is_listy(frozenset())
     assert is_listy(frozenset(['a', 'b', 'c']))
Exemplo n.º 7
0
 def test_dict(self):
     assert not is_listy({})
     assert not is_listy({'a': 'A'})
Exemplo n.º 8
0
 def test_tuple(self):
     self.assertTrue(is_listy(tuple()))
     self.assertTrue(is_listy(("a", "b", "c")))
Exemplo n.º 9
0
 def test_tuple(self):
     assert is_listy(tuple())
     assert is_listy(('a', 'b', 'c'))
Exemplo n.º 10
0
 def test_list(self):
     assert is_listy([])
     assert is_listy(['a', 'b', 'c'])
Exemplo n.º 11
0
 def test_frozenset(self):
     self.assertTrue(is_listy(frozenset()))
     self.assertTrue(is_listy(frozenset(["a", "b", "c"])))
Exemplo n.º 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'))
Exemplo n.º 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)
Exemplo n.º 14
0
 def test_none(self):
     assert not is_listy(None)
Exemplo n.º 15
0
 def test_unsized_builtin(self):
     assert not is_listy(iter([]))
     assert not is_listy(i for i in range(2))
Exemplo n.º 16
0
 def test_string(self):
     self.assertFalse(is_listy(""))
     self.assertFalse(is_listy("test"))
     self.assertFalse(is_listy(u("")))
     self.assertFalse(is_listy(u("test")))
Exemplo n.º 17
0
 def test_excluded(self):
     assert not is_listy({})
     assert not is_listy(u(''))
     assert not is_listy('')
     assert not is_listy(b'')
Exemplo n.º 18
0
 def test_list(self):
     self.assertTrue(is_listy([]))
     self.assertTrue(is_listy(["a", "b", "c"]))
Exemplo n.º 19
0
 def test_object(self):
     self.assertFalse(is_listy(object()))
Exemplo n.º 20
0
 def test_object(self):
     assert not is_listy(object())
Exemplo n.º 21
0
 def test_set(self):
     assert is_listy(set())
     assert is_listy(set(['a', 'b', 'c']))
Exemplo n.º 22
0
 def test_none(self):
     self.assertFalse(is_listy(None))
Exemplo n.º 23
0
 def test_dict(self):
     self.assertFalse(is_listy({}))
     self.assertFalse(is_listy({"a": "A"}))