Created
August 12, 2020 18:54
-
-
Save raeq/ec5b97a3e2eee5bd4b6e1bdb3dad1cd7 to your computer and use it in GitHub Desktop.
A list which only accepts URLs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import UserList | |
from urllib.parse import urlparse | |
import json | |
import requests | |
def recursive_key_values(dictionary): | |
for key, value in dictionary.items(): | |
i = 0 | |
if type(value) is str: | |
yield (key, value) | |
elif type(value) is dict: | |
yield from recursive_key_values(value) | |
elif type(value) in (list, tuple, set): | |
for seq_item in value: | |
yield from recursive_key_values({f"{key}_{str(i)}": seq_item}) | |
i = i + 1 | |
else: | |
yield (key, str(value)) | |
class URLFilteredList(UserList): | |
""" | |
URLFilteredList. Will only accept URLs via append. | |
""" | |
def __init__(self): | |
self.data = [] | |
def append(self, item) -> None: | |
if self._is_url(item): | |
super().append(item) | |
def __setitem__(self, i: int, item): | |
if self._is_url(item): | |
super().append(item) | |
@staticmethod | |
def _is_url(value: str) -> bool: | |
if value and isinstance(value, str): | |
validation = urlparse(value) | |
if all([validation.scheme, validation.netloc]): | |
return True | |
return False | |
dict1 = dict( | |
json.loads( | |
requests.get("http://ergast.com/api/f1/2014/5/results.json").text)) | |
ul: URLFilteredList = URLFilteredList() | |
for k, v in recursive_key_values(dict1): | |
ul.append(v) | |
assert "http://en.wikipedia.org/wiki/2014_Spanish_Grand_Prix" in ul | |
assert "http://en.wikipedia.org/wiki/Daniel_Ricciardo" in ul | |
ul[0] = "definitely not a url" | |
assert ul[0] == 'http://ergast.com/mrd/1.4' | |
print(ul) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment