Python operator.truth() Examples

The following are 30 code examples of operator.truth(). 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 operator , or try the search function .
Example #1
Source File: test_bool.py    From ironpython2 with Apache License 2.0 7 votes vote down vote up
def test_operator(self):
        import operator
        self.assertIs(operator.truth(0), False)
        self.assertIs(operator.truth(1), True)
        with test_support.check_py3k_warnings():
            self.assertIs(operator.isCallable(0), False)
            self.assertIs(operator.isCallable(len), True)
        self.assertIs(operator.isNumberType(None), False)
        self.assertIs(operator.isNumberType(0), True)
        self.assertIs(operator.not_(1), False)
        self.assertIs(operator.not_(0), True)
        self.assertIs(operator.isSequenceType(0), False)
        self.assertIs(operator.isSequenceType([]), True)
        self.assertIs(operator.contains([], 1), False)
        self.assertIs(operator.contains([1], 1), True)
        self.assertIs(operator.isMappingType(1), False)
        self.assertIs(operator.isMappingType({}), True)
        self.assertIs(operator.lt(0, 0), False)
        self.assertIs(operator.lt(0, 1), True)
        self.assertIs(operator.is_(True, True), True)
        self.assertIs(operator.is_(True, False), False)
        self.assertIs(operator.is_not(True, True), False)
        self.assertIs(operator.is_not(True, False), True) 
Example #2
Source File: test_operator.py    From oss-ftp with MIT License 6 votes vote down vote up
def test_truth(self):
        class C(object):
            def __nonzero__(self):
                raise SyntaxError
        self.assertRaises(TypeError, operator.truth)
        self.assertRaises(SyntaxError, operator.truth, C())
        self.assertTrue(operator.truth(5))
        self.assertTrue(operator.truth([0]))
        self.assertFalse(operator.truth(0))
        self.assertFalse(operator.truth([])) 
Example #3
Source File: xmlrpclib.py    From meddle with MIT License 6 votes vote down vote up
def __init__(self, value = 0):
            self.value = operator.truth(value) 
Example #4
Source File: xmlrpclib.py    From meddle with MIT License 6 votes vote down vote up
def boolean(value, _truefalse=(False, True)):
        """Convert any Python value to XML-RPC 'boolean'."""
        return _truefalse[operator.truth(value)] 
Example #5
Source File: test_numbers.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_bool_values(self):
        from operator import truth
        for t, v in zip(bool_types, bool_values):
            self.assertEqual(t(v).value, truth(v)) 
Example #6
Source File: test_operator.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_truth(self):
        class C(object):
            def __nonzero__(self):
                raise SyntaxError
        self.assertRaises(TypeError, operator.truth)
        self.assertRaises(SyntaxError, operator.truth, C())
        self.assertTrue(operator.truth(5))
        self.assertTrue(operator.truth([0]))
        self.assertFalse(operator.truth(0))
        self.assertFalse(operator.truth([])) 
Example #7
Source File: xmlrpclib.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def __init__(self, value = 0):
            self.value = operator.truth(value) 
Example #8
Source File: _pydev_xmlrpclib.py    From PyDev.Debugger with Eclipse Public License 1.0 6 votes vote down vote up
def boolean(value, _truefalse=(False, True)):
        """Convert any Python value to XML-RPC 'boolean'."""
        return _truefalse[operator.truth(value)]

##
# Wrapper for XML-RPC DateTime values.  This converts a time value to
# the format used by XML-RPC.
# <p>
# The value can be given as a string in the format
# "yyyymmddThh:mm:ss", as a 9-item time tuple (as returned by
# time.localtime()), or an integer value (as returned by time.time()).
# The wrapper uses time.localtime() to convert an integer to a time
# tuple.
#
# @param value The time, given as an ISO 8601 string, a time
#              tuple, or a integer time value. 
Example #9
Source File: parse_keyboard_buffer.py    From writeups with GNU General Public License v3.0 5 votes vote down vote up
def parse_buffer(data):
    scans, shifted_scans, shift_map, shifts = load_scancode_map()
    state = bytearray(256)
    parsed = []
    def get_shift_state(shift_name):
        return operator.truth(reduce(operator.or_, [ state[x] for x in shifts[shift_name] ]))
    for i in xrange(0, len(data), 12):
        _, scan, updown = struct.unpack('<HHH', data[i:i+6])
        if updown >= 2:
            continue        # unsupported
        if updown:
            # Key Up
            state[scan] = 0
            continue
        else:
            state[scan] = 1
            if scan in shift_map:
                continue
            out = []
            for shift_name in [ 'ctrl', 'alt' ]:
                if get_shift_state(shift_name):
                    out.append(shift_name.upper())
            shift = get_shift_state('shift')
            if shift and scan in shifted_scans:
                out.append(shifted_scans[scan])
            else:
                if shift:
                    out.append('SHIFT')
                if scan in scans:
                    out.append(scans[scan])
                else:
                    out.append('<0x%02x>' % scan)
            parsed.append('-'.join(out))
    return parsed 
Example #10
Source File: paragraph.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def _lineClean(L):
    return join(filter(truth,split(strip(L)))) 
Example #11
Source File: paragraph.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def cleanBlockQuotedText(text,joiner=' '):
    """This is an internal utility which takes triple-
    quoted text form within the document and returns
    (hopefully) the paragraph the user intended originally."""
    L=filter(truth,map(_lineClean, split(text, '\n')))
    return join(L, joiner) 
Example #12
Source File: udp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def setLoopbackMode(self, mode):
        mode = struct.pack("b", operator.truth(mode))
        self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP,
                               mode) 
Example #13
Source File: tcp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def getTcpNoDelay(self):
        return operator.truth(self.socket.getsockopt(socket.IPPROTO_TCP,
                                                     socket.TCP_NODELAY)) 
Example #14
Source File: tcp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def getTcpKeepAlive(self):
        return operator.truth(self.socket.getsockopt(socket.SOL_SOCKET,
                                                     socket.SO_KEEPALIVE)) 
Example #15
Source File: udp.py    From python-for-android with Apache License 2.0 5 votes vote down vote up
def setLoopbackMode(self, mode):
        mode = struct.pack("b", operator.truth(mode))
        self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, mode) 
Example #16
Source File: tcp.py    From BitTorrent with GNU General Public License v3.0 5 votes vote down vote up
def getTcpKeepAlive(self):
        return operator.truth(self.socket.getsockopt(socket.SOL_SOCKET,
                                                     socket.SO_KEEPALIVE)) 
Example #17
Source File: ssdp.py    From script.tubecast with MIT License 5 votes vote down vote up
def set_loopback_mode(self, mode):
        mode = struct.pack("b", operator.truth(mode))
        self.socket.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP,
                               mode) 
Example #18
Source File: test_numbers.py    From odoo13-x64 with GNU General Public License v3.0 5 votes vote down vote up
def test_bool_values(self):
        from operator import truth
        for t, v in zip(bool_types, bool_values):
            self.assertEqual(t(v).value, truth(v)) 
Example #19
Source File: _pydev_xmlrpclib.py    From filmkodi with Apache License 2.0 5 votes vote down vote up
def __init__(self, value=0):
            self.value = operator.truth(value) 
Example #20
Source File: xmlrpclib.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def boolean(value, _truefalse=(False, True)):
        """Convert any Python value to XML-RPC 'boolean'."""
        return _truefalse[operator.truth(value)] 
Example #21
Source File: xmlrpclib.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def __init__(self, value = 0):
            self.value = operator.truth(value) 
Example #22
Source File: tcp.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def getTcpNoDelay(self):
        return operator.truth(self.socket.getsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY)) 
Example #23
Source File: expr.py    From owasp-pysec with Apache License 2.0 5 votes vote down vote up
def __bool__(self):
        return Expression((self,), operator.truth) 
Example #24
Source File: test_numbers.py    From datafari with Apache License 2.0 5 votes vote down vote up
def test_bool_values(self):
        from operator import truth
        for t, v in zip(bool_types, bool_values):
            self.assertEqual(t(v).value, truth(v)) 
Example #25
Source File: test_bool.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_operator(self):
        import operator
        self.assertIs(operator.truth(0), False)
        self.assertIs(operator.truth(1), True)
        with test_support.check_py3k_warnings():
            self.assertIs(operator.isCallable(0), False)
            self.assertIs(operator.isCallable(len), True)
        self.assertIs(operator.isNumberType(None), False)
        self.assertIs(operator.isNumberType(0), True)
        self.assertIs(operator.not_(1), False)
        self.assertIs(operator.not_(0), True)
        self.assertIs(operator.isSequenceType(0), False)
        self.assertIs(operator.isSequenceType([]), True)
        self.assertIs(operator.contains([], 1), False)
        self.assertIs(operator.contains([1], 1), True)
        self.assertIs(operator.isMappingType(1), False)
        self.assertIs(operator.isMappingType({}), True)
        self.assertIs(operator.lt(0, 0), False)
        self.assertIs(operator.lt(0, 1), True)
        self.assertIs(operator.is_(True, True), True)
        self.assertIs(operator.is_(True, False), False)
        self.assertIs(operator.is_not(True, True), False)
        self.assertIs(operator.is_not(True, False), True) 
Example #26
Source File: test_operator.py    From gcblue with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_truth(self):
        class C(object):
            def __nonzero__(self):
                raise SyntaxError
        self.assertRaises(TypeError, operator.truth)
        self.assertRaises(SyntaxError, operator.truth, C())
        self.assertTrue(operator.truth(5))
        self.assertTrue(operator.truth([0]))
        self.assertFalse(operator.truth(0))
        self.assertFalse(operator.truth([])) 
Example #27
Source File: test_bool.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_operator(self):
        import operator
        self.assertIs(operator.truth(0), False)
        self.assertIs(operator.truth(1), True)
        self.assertIs(operator.not_(1), False)
        self.assertIs(operator.not_(0), True)
        self.assertIs(operator.contains([], 1), False)
        self.assertIs(operator.contains([1], 1), True)
        self.assertIs(operator.lt(0, 0), False)
        self.assertIs(operator.lt(0, 1), True)
        self.assertIs(operator.is_(True, True), True)
        self.assertIs(operator.is_(True, False), False)
        self.assertIs(operator.is_not(True, True), False)
        self.assertIs(operator.is_not(True, False), True) 
Example #28
Source File: test_numbers.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_bool_values(self):
        from operator import truth
        for t, v in zip(bool_types, bool_values):
            self.assertEqual(t(v).value, truth(v)) 
Example #29
Source File: tcp.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def getTcpKeepAlive(self):
        return operator.truth(self.socket.getsockopt(socket.SOL_SOCKET,
                                                     socket.SO_KEEPALIVE)) 
Example #30
Source File: udp.py    From learn_python3_spider with MIT License 5 votes vote down vote up
def getBroadcastAllowed(self):
        """
        Checks if broadcast is currently allowed on this port.

        @return: Whether this port may broadcast.
        @rtype: L{bool}
        """
        return operator.truth(
            self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST))