Python more_itertools.always_iterable() Examples

The following are 30 code examples of more_itertools.always_iterable(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module more_itertools , or try the search function .
Example #1
Source File: _cpmodpy.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def send_response(req, status, headers, body, stream=False):
    # Set response status
    req.status = int(status[:3])

    # Set response headers
    req.content_type = 'text/plain'
    for header, value in headers:
        if header.lower() == 'content-type':
            req.content_type = value
            continue
        req.headers_out.add(header, value)

    if stream:
        # Flush now so the status and headers are sent immediately.
        req.flush()

    # Set response body
    for seg in always_iterable(body):
        req.write(seg)


# --------------- Startup tools for CherryPy + mod_python --------------- # 
Example #2
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def test_base_type(self):
        dict_obj = {'a': 1, 'b': 2}
        str_obj = '123'

        # Default: dicts are iterable like they normally are
        default_actual = list(mi.always_iterable(dict_obj))
        default_expected = list(dict_obj)
        self.assertEqual(default_actual, default_expected)

        # Unitary types set: dicts are not iterable
        custom_actual = list(mi.always_iterable(dict_obj, base_type=dict))
        custom_expected = [dict_obj]
        self.assertEqual(custom_actual, custom_expected)

        # With unitary types set, strings are iterable
        str_actual = list(mi.always_iterable(str_obj, base_type=None))
        str_expected = list(str_obj)
        self.assertEqual(str_actual, str_expected) 
Example #3
Source File: _cpmodpy.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def send_response(req, status, headers, body, stream=False):
    # Set response status
    req.status = int(status[:3])

    # Set response headers
    req.content_type = 'text/plain'
    for header, value in headers:
        if header.lower() == 'content-type':
            req.content_type = value
            continue
        req.headers_out.add(header, value)

    if stream:
        # Flush now so the status and headers are sent immediately.
        req.flush()

    # Set response body
    for seg in always_iterable(body):
        req.write(seg)


# --------------- Startup tools for CherryPy + mod_python --------------- # 
Example #4
Source File: _cperror.py    From Tautulli with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, urls, status=None, encoding=None):
        self.urls = abs_urls = [
            # Note that urljoin will "do the right thing" whether url is:
            #  1. a complete URL with host (e.g. "http://www.example.com/test")
            #  2. a URL relative to root (e.g. "/dummy")
            #  3. a URL relative to the current path
            # Note that any query string in cherrypy.request is discarded.
            urllib.parse.urljoin(
                cherrypy.url(),
                tonative(url, encoding or self.encoding),
            )
            for url in always_iterable(urls)
        ]

        status = (
            int(status)
            if status is not None
            else self.default_status
        )
        if not 300 <= status <= 399:
            raise ValueError('status must be between 300 and 399.')

        CherryPyException.__init__(self, abs_urls, status) 
Example #5
Source File: test_more.py    From pipenv with MIT License 6 votes vote down vote up
def test_base_type(self):
        dict_obj = {'a': 1, 'b': 2}
        str_obj = '123'

        # Default: dicts are iterable like they normally are
        default_actual = list(mi.always_iterable(dict_obj))
        default_expected = list(dict_obj)
        self.assertEqual(default_actual, default_expected)

        # Unitary types set: dicts are not iterable
        custom_actual = list(mi.always_iterable(dict_obj, base_type=dict))
        custom_expected = [dict_obj]
        self.assertEqual(custom_actual, custom_expected)

        # With unitary types set, strings are iterable
        str_actual = list(mi.always_iterable(str_obj, base_type=None))
        str_expected = list(str_obj)
        self.assertEqual(str_actual, str_expected) 
Example #6
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_base_type(self):
        dict_obj = {'a': 1, 'b': 2}
        str_obj = '123'

        # Default: dicts are iterable like they normally are
        default_actual = list(mi.always_iterable(dict_obj))
        default_expected = list(dict_obj)
        self.assertEqual(default_actual, default_expected)

        # Unitary types set: dicts are not iterable
        custom_actual = list(mi.always_iterable(dict_obj, base_type=dict))
        custom_expected = [dict_obj]
        self.assertEqual(custom_actual, custom_expected)

        # With unitary types set, strings are iterable
        str_actual = list(mi.always_iterable(str_obj, base_type=None))
        str_expected = list(str_obj)
        self.assertEqual(str_actual, str_expected) 
Example #7
Source File: _cperror.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, urls, status=None, encoding=None):
        self.urls = abs_urls = [
            # Note that urljoin will "do the right thing" whether url is:
            #  1. a complete URL with host (e.g. "http://www.example.com/test")
            #  2. a URL relative to root (e.g. "/dummy")
            #  3. a URL relative to the current path
            # Note that any query string in cherrypy.request is discarded.
            urllib.parse.urljoin(
                cherrypy.url(),
                tonative(url, encoding or self.encoding),
            )
            for url in always_iterable(urls)
        ]

        status = (
            int(status)
            if status is not None
            else self.default_status
        )
        if not 300 <= status <= 399:
            raise ValueError('status must be between 300 and 399.')

        CherryPyException.__init__(self, abs_urls, status) 
Example #8
Source File: test_more.py    From python-netsurv with MIT License 6 votes vote down vote up
def test_base_type(self):
        dict_obj = {'a': 1, 'b': 2}
        str_obj = '123'

        # Default: dicts are iterable like they normally are
        default_actual = list(mi.always_iterable(dict_obj))
        default_expected = list(dict_obj)
        self.assertEqual(default_actual, default_expected)

        # Unitary types set: dicts are not iterable
        custom_actual = list(mi.always_iterable(dict_obj, base_type=dict))
        custom_expected = [dict_obj]
        self.assertEqual(custom_actual, custom_expected)

        # With unitary types set, strings are iterable
        str_actual = list(mi.always_iterable(str_obj, base_type=None))
        str_expected = list(str_obj)
        self.assertEqual(str_actual, str_expected) 
Example #9
Source File: client.py    From irc with MIT License 5 votes vote down vote up
def list(self, channels=None, server=""):
        """Send a LIST command."""
        self.send_items('LIST', ','.join(always_iterable(channels)), server) 
Example #10
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_generator(self):
        def _gen():
            yield 0
            yield 1

        self.assertEqual(list(mi.always_iterable(_gen())), [0, 1]) 
Example #11
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_none(self):
        self.assertEqual(list(mi.always_iterable(None)), []) 
Example #12
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_strings(self):
        for obj in ['foo', b'bar', 'baz']:
            actual = list(mi.always_iterable(obj))
            expected = [obj]
            self.assertEqual(actual, expected) 
Example #13
Source File: test_more.py    From Tautulli with GNU General Public License v3.0 5 votes vote down vote up
def test_single(self):
        self.assertEqual(list(mi.always_iterable(1)), [1]) 
Example #14
Source File: wspbus.py    From cherrypy with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def wait(self, state, interval=0.1, channel=None):
        """Poll for the given state(s) at intervals; publish to channel."""
        states = set(always_iterable(state))

        while self.state not in states:
            time.sleep(interval)
            self.publish(channel) 
Example #15
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_single(self):
        self.assertEqual(list(mi.always_iterable(1)), [1]) 
Example #16
Source File: __init__.py    From wolframalpha with MIT License 5 votes vote down vote up
def from_doc(cls, doc):
        """
        Load instances from the xmltodict result. Always return
        an iterable, even if the result is a singleton.
        """
        return map(cls, always_iterable(doc)) 
Example #17
Source File: client.py    From irc with MIT License 5 votes vote down vote up
def whois(self, targets):
        """Send a WHOIS command."""
        self.send_items('WHOIS', ",".join(always_iterable(targets))) 
Example #18
Source File: client.py    From irc with MIT License 5 votes vote down vote up
def part(self, channels, message=""):
        """Send a PART command."""
        self.send_items('PART', ','.join(always_iterable(channels)), message) 
Example #19
Source File: client.py    From irc with MIT License 5 votes vote down vote up
def names(self, channels=None):
        """Send a NAMES command."""
        self.send_items('NAMES', ','.join(always_iterable(channels))) 
Example #20
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_iterables(self):
        self.assertEqual(list(mi.always_iterable([0, 1])), [0, 1])
        self.assertEqual(
            list(mi.always_iterable([0, 1], base_type=list)), [[0, 1]]
        )
        self.assertEqual(
            list(mi.always_iterable(iter('foo'))), ['f', 'o', 'o']
        )
        self.assertEqual(list(mi.always_iterable([])), []) 
Example #21
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_none(self):
        self.assertEqual(list(mi.always_iterable(None)), []) 
Example #22
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_iterables(self):
        self.assertEqual(list(mi.always_iterable([0, 1])), [0, 1])
        self.assertEqual(
            list(mi.always_iterable([0, 1], base_type=list)), [[0, 1]]
        )
        self.assertEqual(
            list(mi.always_iterable(iter('foo'))), ['f', 'o', 'o']
        )
        self.assertEqual(list(mi.always_iterable([])), []) 
Example #23
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_strings(self):
        for obj in ['foo', b'bar', 'baz']:
            actual = list(mi.always_iterable(obj))
            expected = [obj]
            self.assertEqual(actual, expected) 
Example #24
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_strings(self):
        for obj in ['foo', b'bar', 'baz']:
            actual = list(mi.always_iterable(obj))
            expected = [obj]
            self.assertEqual(actual, expected) 
Example #25
Source File: test_more.py    From pipenv with MIT License 5 votes vote down vote up
def test_single(self):
        self.assertEqual(list(mi.always_iterable(1)), [1]) 
Example #26
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_none(self):
        self.assertEqual(list(mi.always_iterable(None)), []) 
Example #27
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_iterables(self):
        self.assertEqual(list(mi.always_iterable([0, 1])), [0, 1])
        self.assertEqual(
            list(mi.always_iterable([0, 1], base_type=list)), [[0, 1]]
        )
        self.assertEqual(
            list(mi.always_iterable(iter('foo'))), ['f', 'o', 'o']
        )
        self.assertEqual(list(mi.always_iterable([])), []) 
Example #28
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_strings(self):
        for obj in ['foo', b'bar', 'baz']:
            actual = list(mi.always_iterable(obj))
            expected = [obj]
            self.assertEqual(actual, expected) 
Example #29
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_single(self):
        self.assertEqual(list(mi.always_iterable(1)), [1]) 
Example #30
Source File: test_more.py    From python-netsurv with MIT License 5 votes vote down vote up
def test_none(self):
        self.assertEqual(list(mi.always_iterable(None)), [])