Skip to main content

A UniqueQueue class - a FIFO queue, but which ignores attempts to re-add duplicate items, even after they're popped.

Project description

UniqueQueue

A Python UniqueQueue class - a FIFO queue, but which ignores attempts to re-add duplicate items, even after they're popped.

Usage

from unique_queue import UniqueQueue

# Initialize a UniqueQueue with optional initial items
my_queue = UniqueQueue(['a', 'b', 'c'])

# Push items into the queue
my_queue.push('d')

# Pop an item from the queue
popped_item = my_queue.pop()

# Extend the queue with multiple items
my_queue.extend(['e', 'f', 'g'])

# Get the total count of unique items in the queue
total_count = my_queue.total_count()

# Get the count of completed items (i.e., items that have been popped)
completed_count = my_queue.completed_count()

# Get the count of remaining items in the queue
remaining_count = my_queue.remaining_count()

# Check if the queue is empty
is_empty = my_queue.empty()

# Get the length of the queue
queue_length = len(my_queue)

Methods

  • push(item): Adds an item to the queue if it is not already present.

  • pop(): Removes and returns the first item from the queue.

  • extend(items: Iterable): Extends the queue with multiple items.

  • total_count(): Returns the total count of unique items in the queue.

  • completed_count(): Returns the count of completed items (i.e., items that have been popped).

  • remaining_count(): Returns the count of remaining items in the queue.

  • empty(): Returns True if the queue is empty, False otherwise.

  • __len__(): Returns the length of the queue.

Example

When doing activities like web scraping, you must keep a queue of pending pages to visit/scrape, based on links found on already-scraped pages. You must also avoid re-visiting already-visited pages, or risk duplicates in the output dataset.

This is an example of one such simple scraper:

from unique_queue import UniqueQueue
import requests

def extract_urls(html: str) -> list[str]:
    """Crude function to extract a list of URL links from a webpage."""
    urls: list[str] = []
    if '<a href="' in html:
        for x in html.split('<a href="')[1:]:
            url = x.split('"')[0]
            if 'choosealicense.com' in url and url.startswith('https://'):
                urls.append(url)
    return urls

# Seed URL to start traversal
seed_url = "https://choosealicense.com/"

# Initialize a UniqueQueue instance
q = UniqueQueue([seed_url])

# For the example, say this is what we're trying to solve:
# the number of time a string "example" appears
number_of_times_EXAMPLE_appears = 0

# Perform URL traversal
while not q.empty():
    # Get the next URL from the queue
    url = q.pop()

    # Load the page
    html = requests.get(url).text

    # Update the end result goal
    number_of_times_EXAMPLE_appears += html.lower().count('example')

    # Add the new URLs to search
    q.extend(extract_urls(html))
    
    # Print stats
    print(f"Completed: {q.completed_count()}. Remaining: {q.remaining_count()}.")

print(f"The string 'example' appears {number_of_times_EXAMPLE_appears} times on 'choosealicense.com'.")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unique_queue-0.2.1.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unique_queue-0.2.1-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file unique_queue-0.2.1.tar.gz.

File metadata

  • Download URL: unique_queue-0.2.1.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for unique_queue-0.2.1.tar.gz
Algorithm Hash digest
SHA256 8db42e3753bb091e007924faf2e9be1b792fb3e5a83287dac7ffb0a4032cebcc
MD5 dd8beacd7e879bcd0ea5e7055a71d5d6
BLAKE2b-256 12361e528ae4730692706aef1031aca81ee3b703bf752fa03987ad05295247db

See more details on using hashes here.

File details

Details for the file unique_queue-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: unique_queue-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.10.12

File hashes

Hashes for unique_queue-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5a20cd4c43476ab5941848c6d4078482d8ed5933b607b0b04df077bed0f872c4
MD5 7d4a45ae7c31c41abc4bad01955a79c0
BLAKE2b-256 7262424924dea72e05ff648d74cb445550217083b484b6120780de460a339347

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page