<jupyter_start><jupyter_text>itertools module<jupyter_code># some utilities

import pprint

from data_utils import get_article_items

pp = pprint.PrettyPrinter(indent=4)<jupyter_output><empty_output><jupyter_text>count<jupyter_code>from itertools import count

# Make an iterator that returns evenly spaced values starting with number start.

for num in count(start=100, step=10):
    print(num)<jupyter_output><empty_output><jupyter_text>islice<jupyter_code>from itertools import islice

# Make an iterator that returns selected elements from the iterable.

for num in islice(count(100, 10), 10):
    print(num)<jupyter_output><empty_output><jupyter_text>cycle<jupyter_code>import random

# imagine you had to keep checking these urls until one of them returned a 500
URLS = ['https://foo.com', 'https://bar.org', 'https://spam.co.uk']
SERVER_ERROR = 500


def mock_get(url):
    return random.choice([200, 200, SERVER_ERROR])


response_error = False

while not response_error:
    for url in URLS:
        # do some web things
        response_code = mock_get(url)
        print(response_code)
        
        if response_code == SERVER_ERROR:
            response_error = True
            break
from itertools import cycle

# The cycle iterator from itertools allows you to create an iterator 
# that will cycle through a series of values infinitely.

for url in cycle(URLS):
    response_code = mock_get(url)
    print(response_code)
        
    if response_code == SERVER_ERROR:
        break<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08104575163398693, 'fraction_numerical': 0.016339869281045753, 'mean_word_length': 4.207482993197279, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '459086', 'license': 'MIT', 'n_tokens_mistral': 508, 'n_tokens_neox': 482, 'n_words': 138, 'path': '3_itertools_module.ipynb', 'repo_name': 'seanwiseman/Python-beyond-the-basics', 'size': '1530'}