Python past.builtins.cmp() Examples

The following are 15 code examples of past.builtins.cmp(). 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 past.builtins , or try the search function .
Example #1
Source File: test_builtins.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 6 votes vote down vote up
def test_cmp(self):
        self.assertEqual(cmp(-1, 1), -1)
        self.assertEqual(cmp(1, -1), 1)
        self.assertEqual(cmp(1, 1), 0)
        # verify that circular objects are not handled
        a = []; a.append(a)
        b = []; b.append(b)
        from UserList import UserList
        c = UserList(); c.append(c)
        self.assertRaises(RuntimeError, cmp, a, b)
        self.assertRaises(RuntimeError, cmp, b, c)
        self.assertRaises(RuntimeError, cmp, c, a)
        self.assertRaises(RuntimeError, cmp, a, c)
       # okay, now break the cycles
        a.pop(); b.pop(); c.pop()
        self.assertRaises(TypeError, cmp) 
Example #2
Source File: test_builtins.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 6 votes vote down vote up
def test_basic(self):
        data = range(100)
        copy = data[:]
        random.shuffle(copy)
        self.assertEqual(data, sorted(copy))
        self.assertNotEqual(data, copy)

        data.reverse()
        random.shuffle(copy)
        self.assertEqual(data, sorted(copy, cmp=lambda x, y: cmp(y,x)))
        self.assertNotEqual(data, copy)
        random.shuffle(copy)
        self.assertEqual(data, sorted(copy, key=lambda x: -x))
        self.assertNotEqual(data, copy)
        random.shuffle(copy)
        self.assertEqual(data, sorted(copy, reverse=1))
        self.assertNotEqual(data, copy) 
Example #3
Source File: core.py    From mqttwarn with Eclipse Public License 2.0 5 votes vote down vote up
def __cmp__(self, other):
        return cmp(self.prio, other.prio) 
Example #4
Source File: util.py    From pygraphistry with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def compare_versions(v1, v2):
    try:
        return cmp(StrictVersion(v1), StrictVersion(v2))
    except ValueError:
        return cmp(LooseVersion(v1), LooseVersion(v2)) 
Example #5
Source File: test_futurize.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_cmp(self):
        before = """
        assert cmp(1, 2) == -1
        assert cmp(2, 1) == 1
        """
        after = """
        from past.builtins import cmp
        assert cmp(1, 2) == -1
        assert cmp(2, 1) == 1
        """
        self.convert_check(before, after, stages=(1, 2), ignore_imports=False) 
Example #6
Source File: html_report.py    From clonedigger with GNU General Public License v3.0 5 votes vote down vote up
def sortByCloneSize(self):
        def f(a,b):
            return cmp(b.getMaxCoveredLineNumbersCount(), a.getMaxCoveredLineNumbersCount())
        self._clones.sort(f) 
Example #7
Source File: compat.py    From clonedigger with GNU General Public License v3.0 5 votes vote down vote up
def __cmp__(self, other):
                keys = list(self._data.keys())
                okeys = list(other._data.keys())
                keys.sort()
                okeys.sort()
                return cmp(keys, okeys) 
Example #8
Source File: compat.py    From clonedigger with GNU General Public License v3.0 5 votes vote down vote up
def sorted(iterable, cmp=None, key=None, reverse=False):
        original = list(iterable)
        if key:
            l2 = [(key(elt), index) for index, elt in enumerate(original)]
        else:
            l2 = original
        l2.sort(cmp)
        if reverse:
            l2.reverse()
        if key:
            return [original[index] for elt, index in l2]
        return l2 
Example #9
Source File: win_api_x86_32.py    From miasm with GNU General Public License v2.0 5 votes vote down vote up
def my_lstrcmp(jitter, funcname, get_str):
    ret_ad, args = jitter.func_args_stdcall(["ptr_str1", "ptr_str2"])
    s1 = get_str(args.ptr_str1)
    s2 = get_str(args.ptr_str2)
    log.info("Compare %r with %r", s1, s2)
    jitter.func_ret_stdcall(ret_ad, cmp(s1, s2)) 
Example #10
Source File: win_api_x86_32.py    From miasm with GNU General Public License v2.0 5 votes vote down vote up
def msvcrt_wcscmp(jitter):
    ret_ad, args = jitter.func_args_cdecl(["ptr_str1", "ptr_str2"])
    s1 = get_win_str_w(jitter, args.ptr_str1)
    s2 = get_win_str_w(jitter, args.ptr_str2)
    log.debug("%s('%s','%s')" % (whoami(), s1, s2))
    jitter.func_ret_cdecl(ret_ad, cmp(s1, s2)) 
Example #11
Source File: win_api_x86_32.py    From miasm with GNU General Public License v2.0 5 votes vote down vote up
def msvcrt__wcsicmp(jitter):
    ret_ad, args = jitter.func_args_cdecl(["ptr_str1", "ptr_str2"])
    s1 = get_win_str_w(jitter, args.ptr_str1)
    s2 = get_win_str_w(jitter, args.ptr_str2)
    log.debug("%s('%s','%s')" % (whoami(), s1, s2))
    jitter.func_ret_cdecl(ret_ad, cmp(s1.lower(), s2.lower())) 
Example #12
Source File: win_api_x86_32.py    From miasm with GNU General Public License v2.0 5 votes vote down vote up
def msvcrt__wcsnicmp(jitter):
    ret_ad, args = jitter.func_args_cdecl(["ptr_str1", "ptr_str2", "count"])
    s1 = get_win_str_w(jitter, args.ptr_str1)
    s2 = get_win_str_w(jitter, args.ptr_str2)
    log.debug("%s('%s','%s',%d)" % (whoami(), s1, s2, args.count))
    jitter.func_ret_cdecl(ret_ad, cmp(s1.lower()[:args.count], s2.lower()[:args.count])) 
Example #13
Source File: win_api_x86_32.py    From miasm with GNU General Public License v2.0 5 votes vote down vote up
def msvcrt_memcmp(jitter):
    ret_ad, args = jitter.func_args_cdecl(['ps1', 'ps2', 'size'])
    s1 = jitter.vm.get_mem(args.ps1, args.size)
    s2 = jitter.vm.get_mem(args.ps2, args.size)
    ret = cmp(s1, s2)
    jitter.func_ret_cdecl(ret_ad, ret) 
Example #14
Source File: key.py    From forseti-security with Apache License 2.0 5 votes vote down vote up
def __cmp__(self, other):
        """Compare a Key with another object for sorting purposes.

        Args:
            other (object): The object to compare with

        Returns:
            int: (-1 if self < other, 0 if self == other, 1 if self > other)
        """
        # pylint: disable=protected-access
        if isinstance(other, Key):
            return (cmp(self._object_kind, other._object_kind) or
                    cmp(self._object_path_tuple, other._object_path_tuple))
        return cmp(self, other) 
Example #15
Source File: ob-watcher.py    From joinmarket-clientserver with GNU General Public License v3.0 5 votes vote down vote up
def create_orderbook_table(self, btc_unit, rel_unit):
        result = ''
        try:
            self.taker.dblock.acquire(True)
            rows = self.taker.db.execute('SELECT * FROM orderbook;').fetchall()
        finally:
            self.taker.dblock.release()
        if not rows:
            return 0, result
        #print("len rows before filter: " + str(len(rows)))
        rows = [o for o in rows if o["ordertype"] in filtered_offername_list]
        order_keys_display = (('ordertype', ordertype_display),
                              ('counterparty', do_nothing), ('oid', order_str),
                              ('cjfee', cjfee_display), ('txfee', satoshi_to_unit),
                              ('minsize', satoshi_to_unit),
                              ('maxsize', satoshi_to_unit))

        # somewhat complex sorting to sort by cjfee but with swabsoffers on top

        def orderby_cmp(x, y):
            if x['ordertype'] == y['ordertype']:
                return cmp(Decimal(x['cjfee']), Decimal(y['cjfee']))
            return cmp(offername_list.index(x['ordertype']),
                       offername_list.index(y['ordertype']))

        for o in sorted(rows, key=cmp_to_key(orderby_cmp)):
            result += ' <tr>\n'
            for key, displayer in order_keys_display:
                result += '  <td>' + displayer(o[key], o, btc_unit,
                                               rel_unit) + '</td>\n'
            result += ' </tr>\n'
        return len(rows), result