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.
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file unique_queue-0.2.tar.gz.
File metadata
- Download URL: unique_queue-0.2.tar.gz
- Upload date:
- Size: 3.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7df59c11b24ed468294e06741eceadb85b7dfa99e3808f0b35c5f82f9ca4126f
|
|
| MD5 |
f5a8f845e8ba00e62197d36f37a7bb2e
|
|
| BLAKE2b-256 |
ca1b54cdb60d2c25b2c37764257d44833534042cfde02acf87e4e71fe737bf94
|
File details
Details for the file unique_queue-0.2-py3-none-any.whl.
File metadata
- Download URL: unique_queue-0.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
71deb9dfa4e3fba59834db8da96ac2f3eefe8d8064c590b252dc57bdb0a51aba
|
|
| MD5 |
3036014c2ecaf94df33a81980ff6fa5f
|
|
| BLAKE2b-256 |
dc6f3d39e7e2879510ee8e203d1dd9d948230d21bef65cc7b4f0770b506df49e
|