Python past.builtins.range() Examples

The following are 30 code examples of past.builtins.range(). 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: helpers.py    From boofuzz with GNU General Public License v2.0 6 votes vote down vote up
def crc16(string, value=0):
    """CRC-16 poly: p(x) = x**16 + x**15 + x**2 + 1

    @param string: Data over which to calculate crc.
    @param value: Initial CRC value.
    """
    crc16_table = []
    for byte in range(256):
        crc = 0

        for _ in range(8):
            if (byte ^ crc) & 1:
                crc = (crc >> 1) ^ 0xA001  # polly
            else:
                crc >>= 1

            byte >>= 1

        crc16_table.append(crc)

    for ch in string:
        value = crc16_table[ord(ch) ^ (value & 0xFF)] ^ (value >> 8)

    return value 
Example #2
Source File: att_model_proxy.py    From sdvae with MIT License 6 votes vote down vote up
def decode_chunk(raw_logits, use_random, decode_times):
    tree_decoder = ProgTreeDecoder()    
    chunk_result = [[] for _ in range(raw_logits.shape[1])]
        
    for i in tqdm(range(raw_logits.shape[1])):
        pred_logits = raw_logits[:, i, :]
        walker = ConditionalProgramDecoder(np.squeeze(pred_logits), use_random)

        for _decode in range(decode_times):
            new_t = Node('program')
            try:
                tree_decoder.decode(new_t, walker)
                sampled = get_program_from_tree(new_t)
            except Exception as ex:
                print('Warning, decoder failed with', ex)
                # failed. output a random junk.
                import random, string
                sampled = 'JUNK' + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(256))

            chunk_result[i].append(sampled)

    return chunk_result 
Example #3
Source File: test_base_multinomial_cm.py    From pylogit with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_check_type_and_size_of_param_list(self):
        """
        Ensure that a ValueError is raised if param_list is not a list with the
        expected number of elements
        """
        expected_length = 4
        bad_param_list_1 = set(range(4))
        bad_param_list_2 = range(5)
        # Note that for the purposes of the function being tested, good is
        # defined as a list with four elements. Other functions check the
        # content of those elements
        good_param_list = range(4)

        for param_list in [bad_param_list_1, bad_param_list_2]:
            self.assertRaises(ValueError,
                              base_cm.check_type_and_size_of_param_list,
                              param_list,
                              expected_length)

        args = [good_param_list, expected_length]
        func_results = base_cm.check_type_and_size_of_param_list(*args)
        self.assertIsNone(func_results)

        return None 
Example #4
Source File: make_dataset_parallel.py    From sdvae with MIT License 6 votes vote down vote up
def run_job(L):
    chunk_size = 5000
    
    list_binary = Parallel(n_jobs=cmd_args.data_gen_threads, verbose=50)(
        delayed(process_chunk)(L[start: start + chunk_size])
        for start in range(0, len(L), chunk_size)
    )

    all_onehot = np.zeros((len(L), cmd_args.max_decode_steps, DECISION_DIM), dtype=np.byte)
    all_masks = np.zeros((len(L), cmd_args.max_decode_steps, DECISION_DIM), dtype=np.byte)

    for start, b_pair in zip( range(0, len(L), chunk_size), list_binary ):
        all_onehot[start: start + chunk_size, :, :] = b_pair[0]
        all_masks[start: start + chunk_size, :, :] = b_pair[1]

    return all_onehot, all_masks 
Example #5
Source File: test_builtins.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 6 votes vote down vote up
def fcmp(x, y): # fuzzy comparison function
    """
    From Python 2.7 test.test_support
    """
    if isinstance(x, float) or isinstance(y, float):
        try:
            fuzz = (abs(x) + abs(y)) * FUZZ
            if abs(x-y) <= fuzz:
                return 0
        except:
            pass
    elif type(x) == type(y) and isinstance(x, (tuple, list)):
        for i in range(min(len(x), len(y))):
            outcome = fcmp(x[i], y[i])
            if outcome != 0:
                return outcome
        return (len(x) > len(y)) - (len(x) < len(y))
    return (x > y) - (x < y) 
Example #6
Source File: intera.py    From pypath with GNU General Public License v3.0 6 votes vote down vote up
def get_bonds(self, typ=None, mode=None):
        '''
        Gives a generator to iterate throught bonds in
        this interface. If no type given, bonds of all types
        returned.
        '''
        if typ is None:
            typ = self.types
        if type(typ) is str:
            typ = [typ]
        for t in typ:
            if t in self.__dict__:
                for i in range(0, len(self.__dict__[t])):
                    if mode == 'dict':
                        yield {
                            self.id_a: self.__dict__[t][self.id_a][i],
                            self.id_b: self.id_b,
                            'res_b': self.__dict__[t][self.id_b][i],
                            'type': t
                        }
                    else:
                        yield (self.id_a,) + self.__dict__[t][self.id_a][i] + \
                            (self.id_b,) + self.__dict__[t][self.id_b][i] + \
                            (t,) 
Example #7
Source File: att_model_proxy.py    From sdvae with MIT License 6 votes vote down vote up
def decode_chunk(raw_logits, use_random, decode_times):
    tree_decoder = create_tree_decoder()    
    chunk_result = [[] for _ in range(raw_logits.shape[1])]
        
    for i in tqdm(range(raw_logits.shape[1])):
        pred_logits = raw_logits[:, i, :]
        walker = ConditionalDecoder(np.squeeze(pred_logits), use_random)

        for _decode in range(decode_times):
            new_t = Node('smiles')
            try:
                tree_decoder.decode(new_t, walker)
                sampled = get_smiles_from_tree(new_t)
            except Exception as ex:
                if not type(ex).__name__ == 'DecodingLimitExceeded':
                    print('Warning, decoder failed with', ex)
                # failed. output a random junk.
                import random, string
                sampled = 'JUNK' + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(256))

            chunk_result[i].append(sampled)

    return chunk_result 
Example #8
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 #9
Source File: intera.py    From pypath with GNU General Public License v3.0 5 votes vote down vote up
def range_str(self):

        start_end = self.range()

        return '%s-%s' % start_end if start_end else '' 
Example #10
Source File: plot.py    From pypath with GNU General Public License v3.0 5 votes vote down vote up
def get_years(self):
        self.d = pypath.descriptions.descriptions
        self.firstyear = min(
            common.flat_list([
                [r['year']] for r in self.d.values() if 'year' in r
            ] + [r['releases'] for r in self.d.values() if 'releases' in r]))
        self.lastyear = self.lastyear if hasattr(
            self, 'lastyear') else date.today().year
        self.years = list(range(self.lastyear - self.firstyear + 1)) 
Example #11
Source File: intera.py    From pypath with GNU General Public License v3.0 5 votes vote down vote up
def range(self):

        return (
            (self.start, self.end)
                if self.start and self.end else
            None
        ) 
Example #12
Source File: intera.py    From pypath with GNU General Public License v3.0 5 votes vote down vote up
def range_str(self):

        start_end = self.range()

        return '%s-%s' % start_end if start_end else '' 
Example #13
Source File: unichem.py    From pypath with GNU General Public License v3.0 5 votes vote down vote up
def connectivity_search(self,
                            id_list,
                            id_type,
                            parameters=[1, 0, 0, 0, 0, 1, 0]):
        '''
        [1,0,0,0,0,1,0,  1]
        '''
        '''
        parameters is a list of parameters A-H as described in 
        https://www.ebi.ac.uk/unichem/info/widesearchInfo
        '''
        parameters.append(1)  # H parameter must be 1 to process the result
        parameters = [str(i) for i in parameters]
        self.result = {}
        if id_type == 'inchikey':
            id_type = ''
            method = 'key_search'
        elif id_type == 'smiles':
            self.result = None
            return None
        else:
            id_type = str(id_type) if type(id_type) is int else self.name_dict[
                id_type]
            id_type = '%s/' % id_type
            method = 'cpd_search'
        prg = progress.Progress(
            total=len(id_list), name='Connectivity search', interval=1)
        for i in id_list:
            prg.step()
            url = self.cpd_search.format(method, i, id_type,
                                         '/'.join(parameters))
            c = curl.Curl(url, large = False)
            result = c.result
            self.result[i] = []
            if result is not None:
                data = json.loads(result)
                for k, v in iteritems(data):
                    for j in range(1, len(v)):
                        self.result[i].append(v[j][0])
            self.result[i] = list(set(self.result[i]))
        prg.terminate() 
Example #14
Source File: att_model_proxy.py    From sdvae with MIT License 5 votes vote down vote up
def parse(chunk, grammar):
    size = 100
    result_list = Parallel(n_jobs=-1)(delayed(parse_many)(chunk[i: i + size], grammar) for i in range(0, len(chunk), size))
    return [_1 for _0 in result_list for _1 in _0] 
Example #15
Source File: att_model_proxy.py    From sdvae with MIT License 5 votes vote down vote up
def batch_decode(raw_logits, use_random, decode_times):
    size = (raw_logits.shape[1] + 7) / 8

    logit_lists = []
    for i in range(0, raw_logits.shape[1], size):
        if i + size < raw_logits.shape[1]:
            logit_lists.append(raw_logits[:, i : i + size, :])
        else:
            logit_lists.append(raw_logits[:, i : , :])

    result_list = Parallel(n_jobs=-1)(delayed(decode_chunk)(logit_lists[i], use_random, decode_times) for i in range(len(logit_lists)))
    return [_1 for _0 in result_list for _1 in _0] 
Example #16
Source File: att_model_proxy.py    From sdvae with MIT License 5 votes vote down vote up
def decode(self, chunk, use_random=True):
        '''
        Args:
            chunk: A numpy array of dtype np.float32, of shape (n, latent_dim)
        Return:
            a list of `n` strings, each being a SMILES.
        '''
        raw_logits = self.pred_raw_logits(chunk)

        result_list = []
        for i in range(raw_logits.shape[1]):
            pred_logits = raw_logits[:, i, :]

            walker = ConditionalDecoder(np.squeeze(pred_logits), use_random)

            new_t = Node('smiles')
            try:
                self.tree_decoder.decode(new_t, walker)
                sampled = get_smiles_from_tree(new_t)
            except Exception as ex:
                if not type(ex).__name__ == 'DecodingLimitExceeded':
                    print('Warning, decoder failed with', ex)
                # failed. output a random junk.
                import random, string
                sampled = 'JUNK' + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(256))

            result_list.append(sampled)
        
        return result_list 
Example #17
Source File: dump_cfg_trees.py    From sdvae with MIT License 5 votes vote down vote up
def parse(chunk, grammar):
    size = 100
    result_list = Parallel(n_jobs=-1)(delayed(parse_many)(chunk[i: i + size], grammar) for i in range(0, len(chunk), size))
    return [_1 for _0 in result_list for _1 in _0] 
Example #18
Source File: att_model_proxy.py    From sdvae with MIT License 5 votes vote down vote up
def parse(chunk, grammar):
    size = 100
    result_list = Parallel(n_jobs=-1)(delayed(parse_many)(chunk[i: i + size], grammar) for i in range(0, len(chunk), size))
    return [_1 for _0 in result_list for _1 in _0] 
Example #19
Source File: att_model_proxy.py    From sdvae with MIT License 5 votes vote down vote up
def decode(self, chunk, use_random=True):
        '''
        Args:
            chunk: A numpy array of dtype np.float32, of shape (n, latent_dim)
        Return:
            a list of `n` strings, each being a SMILES.
        '''
        raw_logits = self.pred_raw_logits(chunk)

        result_list = []
        for i in range(raw_logits.shape[1]):
            pred_logits = raw_logits[:, i, :]

            walker = ConditionalProgramDecoder(np.squeeze(pred_logits), use_random)

            new_t = Node('program')
            try:
                self.tree_decoder.decode(new_t, walker)
                sampled = get_program_from_tree(new_t)
            except Exception as ex:
                print('Warning, decoder failed with', ex)
                # failed. output a random junk.
                import random, string
                sampled = 'JUNK' + ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(256))

            result_list.append(sampled)
        
        return result_list 
Example #20
Source File: drawing.py    From pypath with GNU General Public License v3.0 5 votes vote down vote up
def draw_table(self):
        bg = self.rgb1(self.bgcol)
        self.ctx.set_source_rgba(*bg)
        for xi in range(0, len(self.xcoo)):
            for yi in range(0, len(self.ycoo)):
                ulx = self.margin[0] + sum(self.xcoo[:xi])
                uly = self.margin[2] + sum(self.ycoo[:yi])
                # print 'Drawing rectangle at (%f, %f), size (%f, %f)' % \
                #    (ulx + self.skip, uly + self.skip, self.xcoo[xi] - self.skip,
                #    self.ycoo[yi] - self.skip)
                self.ctx.rectangle(ulx + self.skip, uly + self.skip,
                                   self.xcoo[xi] - self.skip,
                                   self.ycoo[yi] - self.skip)
                self.ctx.stroke_preserve()
                self.ctx.fill() 
Example #21
Source File: test_base_multinomial_cm.py    From pylogit with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_check_param_list_validity(self):
        """
        Go thorough all possible types of 'bad' param_list arguments and
        ensure that the appropriate ValueErrors are raised. Ensure that 'good'
        param_list arguments make it through the function successfully
        """
        # Create a series of good parameter lists that should make it through
        # check_param_list_validity()
        good_list_1 = None
        good_list_2 = [np.zeros(1), np.ones(2), np.ones(2), np.ones(2)]
        good_list_3 = [np.zeros((1, 3)),
                       np.ones((2, 3)),
                       np.ones((2, 3)),
                       np.ones((2, 3))]

        good_lists = [good_list_1, good_list_2, good_list_3]

        # Create a series of bad parameter lists that should all result in
        # ValueErrors being raised.
        bad_list_1 = set(range(4))
        bad_list_2 = range(5)
        bad_list_3 = ['foo', np.zeros(2)]
        bad_list_4 = [np.zeros(2), 'foo']
        bad_list_5 = [np.zeros((2, 3)), np.zeros((2, 4))]
        bad_list_6 = [np.zeros((2, 3)), np.ones(2)]
        bad_list_7 = [np.zeros(3), np.ones((2, 3))]

        bad_lists = [bad_list_1, bad_list_2, bad_list_3,
                     bad_list_4, bad_list_5, bad_list_6,
                     bad_list_7]

        # Alias the function of interest to ensure it fits on one line
        func = self.model_obj.check_param_list_validity

        for param_list in good_lists:
            self.assertIsNone(func(param_list))

        for param_list in bad_lists:
            self.assertRaises(ValueError, func, param_list)

        return None 
Example #22
Source File: test_builtins.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_max(self):
        self.assertEqual(max('123123'), '3')
        self.assertEqual(max(1, 2, 3), 3)
        self.assertEqual(max((1, 2, 3, 1, 2, 3)), 3)
        self.assertEqual(max([1, 2, 3, 1, 2, 3]), 3)

        self.assertEqual(max(1, 2, 3.0), 3.0)
        self.assertEqual(max(1, 2.0, 3), 3)
        self.assertEqual(max(1.0, 2, 3), 3)

        for stmt in (
            "max(key=int)",                 # no args
            "max(1, key=int)",              # single arg not iterable
            "max(1, 2, keystone=int)",      # wrong keyword
            "max(1, 2, key=int, abc=int)",  # two many keywords
            "max(1, 2, key=1)",             # keyfunc is not callable
            ):
            try:
                exec(stmt) in globals()
            except TypeError:
                pass
            else:
                self.fail(stmt)

        self.assertEqual(max((1,), key=neg), 1)     # one elem iterable
        self.assertEqual(max((1,2), key=neg), 1)    # two elem iterable
        self.assertEqual(max(1, 2, key=neg), 1)     # two elems

        data = [random.randrange(200) for i in range(100)]
        keys = dict((elem, random.randrange(50)) for elem in data)
        f = keys.__getitem__
        self.assertEqual(max(data, key=f),
                         sorted(reversed(data), key=f)[-1]) 
Example #23
Source File: test_builtins.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_next(self):
        it = iter(range(2))
        self.assertEqual(next(it), 0)
        self.assertEqual(next(it), 1)
        self.assertRaises(StopIteration, next, it)
        self.assertRaises(StopIteration, next, it)
        self.assertEqual(next(it, 42), 42)

        class Iter(object):
            def __iter__(self):
                return self
            def next(self):
                raise StopIteration

        it = iter(Iter())
        self.assertEqual(next(it, 42), 42)
        self.assertRaises(StopIteration, next, it)

        def gen():
            yield 1
            return

        it = gen()
        self.assertEqual(next(it), 1)
        self.assertRaises(StopIteration, next, it)
        self.assertEqual(next(it, 42), 42) 
Example #24
Source File: test_builtins.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_reduce(self):
        add = lambda x, y: x+y
        self.assertEqual(reduce(add, ['a', 'b', 'c'], ''), 'abc')
        self.assertEqual(
            reduce(add, [['a', 'c'], [], ['d', 'w']], []),
            ['a','c','d','w']
        )
        self.assertEqual(reduce(lambda x, y: x*y, range(2,8), 1), 5040)
        self.assertEqual(
            reduce(lambda x, y: x*y, range(2,21), 1),
            2432902008176640000
        )
        self.assertEqual(reduce(add, Squares(10)), 285)
        self.assertEqual(reduce(add, Squares(10), 0), 285)
        self.assertEqual(reduce(add, Squares(0), 0), 0)
        self.assertRaises(TypeError, reduce)
        self.assertRaises(TypeError, reduce, 42)
        self.assertRaises(TypeError, reduce, 42, 42)
        self.assertRaises(TypeError, reduce, 42, 42, 42)
        self.assertRaises(TypeError, reduce, None, range(5))
        self.assertRaises(TypeError, reduce, add, 42)
        self.assertEqual(reduce(42, "1"), "1") # func is never called with one item
        self.assertEqual(reduce(42, "", "1"), "1") # func is never called with one item
        self.assertRaises(TypeError, reduce, 42, (42, 42))
        self.assertRaises(TypeError, reduce, add, []) # arg 2 must not be empty sequence with no initial value
        self.assertRaises(TypeError, reduce, add, "")
        self.assertRaises(TypeError, reduce, add, ())
        self.assertEqual(reduce(add, [], None), None)
        self.assertEqual(reduce(add, [], 42), 42)

        class BadSeq:
            def __getitem__(self, index):
                raise ValueError
        self.assertRaises(ValueError, reduce, 42, BadSeq()) 
Example #25
Source File: test_noniterators.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 5 votes vote down vote up
def test_noniterators_produce_lists(self):
        l = range(10)
        self.assertTrue(isinstance(l, list))

        l2 = zip(l, list('ABCDE')*2)
        self.assertTrue(isinstance(l2, list))

        double = lambda x: x*2
        l3 = map(double, l)
        self.assertTrue(isinstance(l3, list))

        is_odd = lambda x: x % 2 == 1
        l4 = filter(is_odd, range(10))
        self.assertEqual(l4, [1, 3, 5, 7, 9])
        self.assertTrue(isinstance(l4, list)) 
Example #26
Source File: drawing.py    From pypath with GNU General Public License v3.0 5 votes vote down vote up
def hex2rgb(self, rgbhex):
        rgbhex = rgbhex.lstrip('#')
        lv = len(rgbhex)
        return tuple(int(rgbhex[i:i + 2], 16) for i in range(0, lv, 2)) 
Example #27
Source File: drawing.py    From pypath with GNU General Public License v3.0 5 votes vote down vote up
def colnames(self):
        # font size for labels:
        self.xlabpt = self.max_text(self.xlabs, self.cellw)
        y = self.margin[2] + self.ycoo[0] + self.ycoo[1] / 2.0
        for xi in range(2, len(self.xcoo)):
            x = self.margin[0] + sum(self.xcoo[:xi]) + self.cellw / 2.0
            lab = self.xlabs[xi - 2]
            self.label(lab, x, y, self.xlabpt, self.colors['embl_gray875']) 
Example #28
Source File: drawing.py    From pypath with GNU General Public License v3.0 5 votes vote down vote up
def rownames(self):
        # font size for labels:
        self.xlabpt = self.max_text(self.xlabs, self.cellw)
        x = self.margin[0] + self.xcoo[0] + self.xcoo[1] / 2.0
        for yi in range(2, len(self.ycoo)):
            y = self.margin[2] + sum(self.ycoo[:yi]) + self.cellh / 2.0
            lab = self.ylabs[yi - 2]
            self.label(
                lab, x, y, self.xlabpt, self.colors['embl_gray875'], rot=-90) 
Example #29
Source File: test_builtins.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 4 votes vote down vote up
def test_zip(self):
        a = (1, 2, 3)
        b = (4, 5, 6)
        t = [(1, 4), (2, 5), (3, 6)]
        self.assertEqual(zip(a, b), t)
        b = [4, 5, 6]
        self.assertEqual(zip(a, b), t)
        b = (4, 5, 6, 7)
        self.assertEqual(zip(a, b), t)
        class I:
            def __getitem__(self, i):
                if i < 0 or i > 2: raise IndexError
                return i + 4
        self.assertEqual(zip(a, I()), t)
        self.assertEqual(zip(), [])
        self.assertEqual(zip(*[]), [])
        self.assertRaises(TypeError, zip, None)
        class G:
            pass
        self.assertRaises(TypeError, zip, a, G())

        # Make sure zip doesn't try to allocate a billion elements for the
        # result list when one of its arguments doesn't say how long it is.
        # A MemoryError is the most likely failure mode.
        class SequenceWithoutALength:
            def __getitem__(self, i):
                if i == 5:
                    raise IndexError
                else:
                    return i
        self.assertEqual(
            zip(SequenceWithoutALength(), xrange(2**30)),
            list(enumerate(range(5)))
        )

        class BadSeq:
            def __getitem__(self, i):
                if i == 5:
                    raise ValueError
                else:
                    return i
        self.assertRaises(ValueError, zip, BadSeq(), BadSeq()) 
Example #30
Source File: test_builtins.py    From kgsgo-dataset-preprocessor with Mozilla Public License 2.0 4 votes vote down vote up
def test_min(self):
        self.assertEqual(min('123123'), '1')
        self.assertEqual(min(1, 2, 3), 1)
        self.assertEqual(min((1, 2, 3, 1, 2, 3)), 1)
        self.assertEqual(min([1, 2, 3, 1, 2, 3]), 1)

        self.assertEqual(min(1, 2, 3.0), 1)
        self.assertEqual(min(1, 2.0, 3), 1)
        self.assertEqual(min(1.0, 2, 3), 1.0)

        self.assertRaises(TypeError, min)
        self.assertRaises(TypeError, min, 42)
        self.assertRaises(ValueError, min, ())
        class BadSeq:
            def __getitem__(self, index):
                raise ValueError
        self.assertRaises(ValueError, min, BadSeq())
        class BadNumber:
            def __cmp__(self, other):
                raise ValueError
        self.assertRaises(ValueError, min, (42, BadNumber()))

        for stmt in (
            "min(key=int)",                 # no args
            "min(1, key=int)",              # single arg not iterable
            "min(1, 2, keystone=int)",      # wrong keyword
            "min(1, 2, key=int, abc=int)",  # two many keywords
            "min(1, 2, key=1)",             # keyfunc is not callable
            ):
            try:
                exec(stmt) in globals()
            except TypeError:
                pass
            else:
                self.fail(stmt)

        self.assertEqual(min((1,), key=neg), 1)     # one elem iterable
        self.assertEqual(min((1,2), key=neg), 2)    # two elem iterable
        self.assertEqual(min(1, 2, key=neg), 2)     # two elems

        data = [random.randrange(200) for i in range(100)]
        keys = dict((elem, random.randrange(50)) for elem in data)
        f = keys.__getitem__
        self.assertEqual(min(data, key=f),
                         sorted(data, key=f)[0])