Python itertools.cycle() Examples

The following are 30 code examples of itertools.cycle(). 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 itertools , or try the search function .
Example #1
Source File: umis.py    From umis with MIT License 13 votes vote down vote up
def read_fastq(filename):
    """
    return a stream of FASTQ entries, handling gzipped and empty files
    """
    if not filename:
        return itertools.cycle((None,))
    if filename == "-":
        filename_fh = sys.stdin
    elif filename.endswith('gz'):
        if is_python3():
            filename_fh = gzip.open(filename, mode='rt')
        else:
            filename_fh = BufferedReader(gzip.open(filename, mode='rt'))
    else:
        filename_fh = open(filename)
    return stream_fastq(filename_fh) 
Example #2
Source File: train_all.py    From bricknil with Apache License 2.0 6 votes vote down vote up
def run(self):
        self.message_info("Running")
        self.motor_speed = 0
        self.keep_running = True
        self.sensor_change = False
        self.go = False

        # Blink the color  from purple and yellow
        colors = cycle([Color.purple, Color.yellow])
        while not self.go:  # Wait until the hub button is pushed
            await self.train_led.set_color(next(colors))
            await sleep(1)

        colors = cycle([Color.green, Color.orange])
        # Ready to go, let's change the color to green!
        while self.keep_running:
            if self.sensor_change:
                await self.train_led.set_color(next(colors))
                await self.motor.ramp_speed(self.motor_speed, 900)  # Ramp to new speed in 0.9 seconds
                self.sensor_change = False
                await sleep(1)
                await self.train_led.set_color(next(colors))
            else:
                await sleep(1) 
Example #3
Source File: array.py    From recruit with Apache License 2.0 6 votes vote down vote up
def __setitem__(self, key, value):
        if isinstance(key, numbers.Integral):
            self.data[key] = value
        else:
            if not isinstance(value, (type(self),
                                      compat.Sequence)):
                # broadcast value
                value = itertools.cycle([value])

            if isinstance(key, np.ndarray) and key.dtype == 'bool':
                # masking
                for i, (k, v) in enumerate(zip(key, value)):
                    if k:
                        assert isinstance(v, self.dtype.type)
                        self.data[i] = v
            else:
                for k, v in zip(key, value):
                    assert isinstance(v, self.dtype.type)
                    self.data[k] = v 
Example #4
Source File: servers.py    From Pyro5 with MIT License 6 votes vote down vote up
def count(self, lines):
        # use the name server's prefix lookup to get all registered wordcounters
        with locate_ns() as ns:
            all_counters = ns.list(prefix="example.dc2.wordcount.")

        # chop the text into chunks that can be distributed across the workers
        # uses futures so that it runs the counts in parallel
        # counter is selected in a round-robin fashion from list of all available counters
        with futures.ThreadPoolExecutor() as pool:
            roundrobin_counters = cycle(all_counters.values())
            tasks = []
            for chunk in grouper(200, lines):
                tasks.append(pool.submit(self.count_chunk, next(roundrobin_counters), chunk))

            # gather the results
            print("Collecting %d results (counted in parallel)..." % len(tasks))
            totals = Counter()
            for task in futures.as_completed(tasks):
                try:
                    totals.update(task.result())
                except Pyro5.errors.CommunicationError as x:
                    raise Pyro5.errors.PyroError("Something went wrong in the server when collecting the responses: "+str(x))
            return totals 
Example #5
Source File: waitingbar.py    From bovespaStockRatings with MIT License 6 votes vote down vote up
def start(self, e):
        for index in cycle(range(len(self.MESSAGE))):
            if e.is_set():
                break
            if not self.MESSAGE[index].isalpha():
                continue
            for c in self.CYCLES:
                buff = list(self.MESSAGE)
                buff.append(c)

                try:
                    if sys.stdout.encoding.upper() == 'UTF-8':
                        buff[index] = self.TABLE[buff[index]]
                    else:
                        buff[index] = buff[index].swapcase()
                except KeyError:
                    pass

                sys.stdout.write(''.join(buff))
                time.sleep(0.05)
                sys.stdout.write('\r')
                sys.stdout.flush() 
Example #6
Source File: duplo_train.py    From bricknil with Apache License 2.0 6 votes vote down vote up
def run(self):
        self.message_info("Running")

        colors = cycle([Color.red, Color.purple, Color.yellow, Color.blue, Color.white])

        snd = DuploSpeaker.sounds
        sounds = cycle([snd.brake, snd.station, snd.water, snd.horn, snd.steam])

        self.message_info('Please move the train to start the program')
        while not self.go:
            await self.led.set_color(next(colors))
            await sleep(0.3)

        for i in range(5):
            await self.led.set_color(next(colors))       # Cycle through the colors
            #await self.speaker.play_sound(next(sounds))  # cycle through the sounds
            tgt_speed = 20 + i*15                        # Keep increasing the speed
            await self.motor.ramp_speed(tgt_speed, 2000)
            self.message_info(f"Set speed to {i}")
            await sleep(3)

        self.message_info("Done") 
Example #7
Source File: test_io.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def test_IOBase_finalize(self):
        # Issue #12149: segmentation fault on _PyIOBase_finalize when both a
        # class which inherits IOBase and an object of this class are caught
        # in a reference cycle and close() is already in the method cache.
        class MyIO(self.IOBase):
            def close(self):
                pass

        # create an instance to populate the method cache
        MyIO()
        obj = MyIO()
        obj.obj = obj
        wr = weakref.ref(obj)
        del MyIO
        del obj
        support.gc_collect()
        self.assertIsNone(wr(), wr) 
Example #8
Source File: test_file2k.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def _test_close_open_io(self, io_func, nb_workers=5):
        def worker():
            self._create_file()
            funcs = itertools.cycle((
                lambda: io_func(),
                lambda: self._close_and_reopen_file(),
            ))
            for f in funcs:
                if not self.do_continue:
                    break
                try:
                    f()
                except (IOError, ValueError):
                    pass
        self._run_workers(worker, nb_workers)
        if test_support.verbose:
            # Useful verbose statistics when tuning this test to take
            # less time to run but still ensuring that its still useful.
            #
            # the percent of close calls that raised an error
            percent = 100. - 100.*self.close_success_count/self.close_count
            print self.close_count, ('%.4f ' % percent), 
Example #9
Source File: learn.py    From flappybird-qlearning-bot with MIT License 6 votes vote down vote up
def showWelcomeAnimation():
    """Shows welcome screen animation of flappy bird"""
    # index of player to blit on screen
    playerIndexGen = cycle([0, 1, 2, 1])

    playery = int((SCREENHEIGHT - PLAYER[IM_HEIGTH]) / 2)

    basex = 0

    # player shm for up-down motion on welcome screen
    playerShmVals = {"val": 0, "dir": 1}

    return {
        "playery": playery + playerShmVals["val"],
        "basex": basex,
        "playerIndexGen": playerIndexGen,
    } 
Example #10
Source File: processor_manager.py    From sawtooth-core with Apache License 2.0 5 votes vote down vote up
def add_processor(self, processor):
        with self._lock:
            self._processors.append(processor)
            self._inf_iterator = itertools.cycle(self._processors) 
Example #11
Source File: decomposers.py    From dwave-hybrid with Apache License 2.0 5 votes vote down vote up
def init(self, state, **runopts):
        self.blocks = iter(chimera_tiles(state.problem, *self.size).items())
        if self.loop:
            self.blocks = itertools.cycle(self.blocks) 
Example #12
Source File: tools.py    From nevergrad with MIT License 5 votes vote down vote up
def roundrobin(*iterables: tp.Iterable[tp.Any]) -> tp.Iterator[tp.Any]:
    """roundrobin('ABC', 'D', 'EF') --> A D E B F C
    """
    # Recipe credited to George Sakkis
    num_active = len(iterables)
    nexts = itertools.cycle(iter(it).__next__ for it in iterables)
    while num_active:
        try:
            for next_ in nexts:
                yield next_()
        except StopIteration:
            # Remove the iterator we just exhausted from the cycle.
            num_active -= 1
            nexts = itertools.cycle(itertools.islice(nexts, num_active)) 
Example #13
Source File: mail.py    From bitmask-dev with GNU General Public License v3.0 5 votes vote down vote up
def _unpack_headers(headers_dict):
    """
    Take a "packed" dict containing headers (with repeated keys represented as
    line breaks inside each value, preceded by the header key) and return a
    list of tuples in which each repeated key has a different tuple.
    """
    headers_l = headers_dict.items()
    for i, (k, v) in enumerate(headers_l):
        splitted = v.split(k.lower() + ": ")
        if len(splitted) != 1:
            inner = zip(
                itertools.cycle([k]),
                map(lambda l: l.rstrip('\n'), splitted))
            headers_l = headers_l[:i] + inner + headers_l[i + 1:]
    return headers_l 
Example #14
Source File: googletest.py    From lambda-packs with MIT License 5 votes vote down vote up
def g_main(argv):
  """Delegate to unittest.main after redefining testLoader."""
  if 'TEST_SHARD_STATUS_FILE' in os.environ:
    try:
      f = None
      try:
        f = open(os.environ['TEST_SHARD_STATUS_FILE'], 'w')
        f.write('')
      except IOError:
        sys.stderr.write('Error opening TEST_SHARD_STATUS_FILE (%s). Exiting.'
                         % os.environ['TEST_SHARD_STATUS_FILE'])
        sys.exit(1)
    finally:
      if f is not None: f.close()

  if ('TEST_TOTAL_SHARDS' not in os.environ or
      'TEST_SHARD_INDEX' not in os.environ):
    return unittest_main(argv=argv)

  total_shards = int(os.environ['TEST_TOTAL_SHARDS'])
  shard_index = int(os.environ['TEST_SHARD_INDEX'])
  base_loader = TestLoader()

  delegate_get_names = base_loader.getTestCaseNames
  bucket_iterator = itertools.cycle(range(total_shards))

  def getShardedTestCaseNames(testCaseClass):
    filtered_names = []
    for testcase in sorted(delegate_get_names(testCaseClass)):
      bucket = next(bucket_iterator)
      if bucket == shard_index:
        filtered_names.append(testcase)
    return filtered_names

  # Override getTestCaseNames
  base_loader.getTestCaseNames = getShardedTestCaseNames

  unittest_main(argv=argv, testLoader=base_loader)


# Redefine main to allow running benchmarks 
Example #15
Source File: giveup.py    From gnsq with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, topic, nsqd_hosts=['localhost'],
                 nsqd_class=gnsq.NsqdHTTPClient):
        if not nsqd_hosts:
            raise ValueError('at least one nsqd host is required')
        self.topic = topic
        self.nsqds = itertools.cycle([nsqd_class(host) for host in nsqd_hosts]) 
Example #16
Source File: test_io.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_threads(self):
        try:
            # Write out many bytes with exactly the same number of 0's,
            # 1's... 255's. This will help us check that concurrent reading
            # doesn't duplicate or forget contents.
            N = 1000
            l = list(range(256)) * N
            random.shuffle(l)
            s = bytes(bytearray(l))
            with self.open(support.TESTFN, "wb") as f:
                f.write(s)
            with self.open(support.TESTFN, self.read_mode, buffering=0) as raw:
                bufio = self.tp(raw, 8)
                errors = []
                results = []
                def f():
                    try:
                        # Intra-buffer read then buffer-flushing read
                        for n in cycle([1, 19]):
                            s = bufio.read(n)
                            if not s:
                                break
                            # list.append() is atomic
                            results.append(s)
                    except Exception as e:
                        errors.append(e)
                        raise
                threads = [threading.Thread(target=f) for x in range(20)]
                with support.start_threads(threads):
                    time.sleep(0.02) # yield
                self.assertFalse(errors,
                    "the following exceptions were caught: %r" % errors)
                s = b''.join(results)
                for i in range(256):
                    c = bytes(bytearray([i]))
                    self.assertEqual(s.count(c), N)
        finally:
            support.unlink(support.TESTFN) 
Example #17
Source File: test_peaker.py    From darglint with MIT License 5 votes vote down vote up
def test_next_does_move_stream_forward(self):
        generator = cycle('abc')
        peaker = Peaker(stream=generator)
        self.assertEqual(peaker.next(), 'a')
        self.assertEqual(peaker.next(), 'b') 
Example #18
Source File: test_peaker.py    From darglint with MIT License 5 votes vote down vote up
def test_peak_doesnt_move_stream_forward(self):
        generator = cycle('abc')
        peaker = Peaker(stream=generator)
        self.assertEqual(peaker.peak(), 'a')
        self.assertEqual(peaker.peak(), 'a') 
Example #19
Source File: bindiff.py    From BASS with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, urls):
        """
        >>> client = Client(['http://host-1:4001', 'http://host-2:4001'])
        :param urls: List of addresses of IDA containers including the published port
        """
        if isinstance(urls, str):
            urls = [urls]
        if urls is None or not any(urls):
            raise ValueError('Invalide "urls" value')
        self._urls = itertools.cycle(urls) 
Example #20
Source File: processor_manager.py    From sawtooth-core with Apache License 2.0 5 votes vote down vote up
def remove_processor(self, processor_identity):
        with self._lock:
            idx = self.processor_identities().index(processor_identity)
            self._processors.pop(idx)
            self._inf_iterator = itertools.cycle(self._processors) 
Example #21
Source File: trainer.py    From scVI with MIT License 5 votes vote down vote up
def data_loaders_loop(self):
        """returns an zipped iterable corresponding to loss signature"""
        data_loaders_loop = [self._posteriors[name] for name in self.posteriors_loop]
        return zip(
            data_loaders_loop[0],
            *[cycle(data_loader) for data_loader in data_loaders_loop[1:]]
        ) 
Example #22
Source File: allocator.py    From aiozk with MIT License 5 votes vote down vote up
def round_robin(members, items):
    """
    Default allocator with a round robin approach.

    In this algorithm, each member of the group is cycled over and given an
    item until there are no items left.  This assumes roughly equal capacity
    for each member and aims for even distribution of item counts.
    """
    allocation = collections.defaultdict(set)

    for member, item in zip(itertools.cycle(members), items):
        allocation[member].add(item)

    return allocation 
Example #23
Source File: __init__.py    From aetros-cli with MIT License 5 votes vote down vote up
def loading_text(label='Loading ... '):
    import itertools, sys
    spinner = itertools.cycle(['-', '/', '|', '\\'])
    state = {'active': True}

    sys.stdout.write(label)
    sys.stdout.flush()

    def display_thread(state):
        try:
            while state['active']:
                sys.stdout.write(next(spinner))
                sys.stdout.flush()
                time.sleep(0.05)
                sys.stdout.write('\b')
        except (KeyboardInterrupt, SystemExit):
            return

    thread = None
    if not is_debug():
        thread = Thread(target=display_thread, args=[state])
        thread.daemon = True
        thread.start()

    def stop(done_label="done."):
        state['active'] = False
        thread and thread.join()
        sys.stdout.write(done_label + '\n')
        sys.stdout.flush()

    return stop 
Example #24
Source File: spinner_curio.py    From concurrency2017 with MIT License 5 votes vote down vote up
def spin(msg):  # <1>
    write, flush = sys.stdout.write, sys.stdout.flush
    for char in itertools.cycle('|/-\\'):
        status = char + ' ' + msg
        write(status)
        flush()
        write('\x08' * len(status))
        try:
            await curio.sleep(.1)  # <2>
        except curio.CancelledError:  # <3>
            break
    write(' ' * len(status) + '\x08' * len(status)) 
Example #25
Source File: sport365.py    From bugatsinho.github.io with GNU General Public License v3.0 5 votes vote down vote up
def xor2(data, key):
    from itertools import izip, cycle
    xored = ''.join(chr(ord(x) ^ ord(y)) for (x, y) in izip(data, cycle(key)))
    return xored 
Example #26
Source File: mobi_utils.py    From Lector with GNU General Public License v3.0 5 votes vote down vote up
def mangle_fonts(encryption_key, data):
    if isinstance(encryption_key, text_type):
        encryption_key = encryption_key.encode('latin-1')
    crypt = data[:1024]
    key = cycle(iter(map(bord, encryption_key)))
    # encrypt = ''.join([chr(ord(x)^key.next()) for x in crypt])
    encrypt = b''.join([bchr(bord(x)^next(key)) for x in crypt])
    return encrypt + data[1024:] 
Example #27
Source File: rdx_cracker.py    From hack4career with Apache License 2.0 5 votes vote down vote up
def xor(key):
	ofile = "cracked_payload.exe"
 
	e = open(ofile, "w")

 	encryptedData = ''.join(chr(ord(x) ^ ord(y)) for (x,y) in izip(payload, cycle(key)))
	e.write(encryptedData) 
	e.close()
        print "[+] XORed payload cracked: cracked_payload.exe" 
Example #28
Source File: follow_spiral.py    From PokemonGo-Bot with MIT License 5 votes vote down vote up
def initialize(self):
        self.steplimit = self.config.get("diameter", 4)
        self.step_size = self.config.get("step_size", 70)
        self.origin_lat = self.bot.position[0]
        self.origin_lon = self.bot.position[1]

        self.diameter_to_steps = (self.steplimit+1) ** 2
        self.spiral = self._generate_spiral(
            self.origin_lat, self.origin_lon, self.step_size, self.diameter_to_steps
        )
        self.points = cycle(self.spiral+list(reversed(self.spiral))[1:-1])
        self.next_point = None 
Example #29
Source File: tutorial_examples.py    From stem with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_persisting_a_consensus(self, query_mock, parse_file_mock, stdout_mock):
    def tutorial_example_2():
      from stem.descriptor import DocumentHandler, parse_file

      consensus = next(parse_file(
        '/tmp/descriptor_dump',
        descriptor_type = 'network-status-consensus-3 1.0',
        document_handler = DocumentHandler.DOCUMENT,
      ))

      for fingerprint, relay in consensus.routers.items():
        print('%s: %s' % (fingerprint, relay.nickname))

    network_status = NetworkStatusDocumentV3.create(routers = (RouterStatusEntryV3.create({
      'r': 'caerSidi p1aag7VwarGxqctS7/fS0y5FU+s oQZFLYe9e4A7bOkWKR7TaNxb0JE 2012-08-06 11:19:31 71.35.150.29 9001 0',
    }),))

    query_mock().run.return_value = [network_status]
    parse_file_mock.return_value = itertools.cycle([network_status])

    exec_documentation_example('persisting_a_consensus.py')
    exec_documentation_example('persisting_a_consensus_with_parse_file.py')

    self.assertEqual(PERSISTING_A_CONSENSUS_OUTPUT, stdout_mock.getvalue())

    if os.path.exists('/tmp/descriptor_dump'):
      os.remove('/tmp/descriptor_dump') 
Example #30
Source File: jvae_trainer.py    From scVI with MIT License 5 votes vote down vote up
def data_loaders_loop(self):
        posteriors = [self._posteriors[name] for name in self.posteriors_loop]
        # find the largest dataset to cycle over the others
        largest = np.argmax(
            [posterior.gene_dataset.X.shape[0] for posterior in posteriors]
        )

        data_loaders = [
            posterior if i == largest else cycle(posterior)
            for i, posterior in enumerate(posteriors)
        ]

        return zip(*data_loaders)