Created
June 25, 2024 19:43
-
-
Save CodeByAidan/764cae6a6c3004713c8d4b558a3714be to your computer and use it in GitHub Desktop.
Check for URL in a String - 5 algorithms compared. - https://www.geeksforgeeks.org/python-check-url-string/
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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 100, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# for algorithms\n", | |
| "import re\n", | |
| "from functools import reduce\n", | |
| "from urllib.parse import urlparse, ParseResult\n", | |
| "\n", | |
| "# for timing\n", | |
| "from timeit import timeit\n", | |
| "\n", | |
| "# types\n", | |
| "from typing import List, Tuple, Callable" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 101, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "string = \"My Profile: https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles in the portal of https://www.geeksforgeeks.org/\"\n", | |
| "string1 = \"My Profile: https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles in the portal of https://www.geeksforgeeks.org/\"\n", | |
| "string2 = \"Some more text without URLs\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 102, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def alg1(string: str) -> List[str]:\n", | |
| " regex: str = (\n", | |
| " r\"(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\\\".,<>?«»“”‘’]))\"\n", | |
| " )\n", | |
| " url: List[str] = re.findall(regex, string)\n", | |
| " return [x[0] for x in url]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 103, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def alg2(string: str) -> List[str]:\n", | |
| " x: List[str] = string.split()\n", | |
| " return [i for i in x if i.startswith(\"https:\") or i.startswith(\"http:\")]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 104, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def alg3(string: str) -> List[str]:\n", | |
| " x: List[str] = string.split()\n", | |
| " return [i for i in x if i.find(\"https:\") == 0 or i.find(\"http:\") == 0]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 105, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def alg4(string: str) -> List[str]:\n", | |
| " words: List[str] = string.split()\n", | |
| " urls: List[str] = []\n", | |
| " for word in words:\n", | |
| " parsed: ParseResult = urlparse(word)\n", | |
| " if parsed.scheme and parsed.netloc:\n", | |
| " urls.append(word)\n", | |
| "\n", | |
| " return urls" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 106, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def alg5(string1: str, string2: str) -> List[str]:\n", | |
| " def merge_url_lists(url_list1: List[str], url_list2: List[str]) -> List[str]:\n", | |
| " return url_list1 + url_list2\n", | |
| "\n", | |
| " def find_urls_in_string(string: str) -> List[str]:\n", | |
| " x: List[str] = string.split()\n", | |
| " return [i for i in x if i.startswith(\"https:\") or i.startswith(\"http:\")]\n", | |
| "\n", | |
| " string_list: List[str] = [string1, string2]\n", | |
| " url_list: List[str] = reduce(merge_url_lists, map(find_urls_in_string, string_list))\n", | |
| " return url_list" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 107, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def time_algorithm(*, algorithm: Callable, number: int = 10000, **kwargs) -> None:\n", | |
| " args: Tuple[str, ...] = kwargs.get(\"args\", ())\n", | |
| " if algorithm == alg5 and len(args) == 2:\n", | |
| " stmt: str = f\"alg5('{args[0]}', '{args[1]}')\"\n", | |
| " else:\n", | |
| " stmt: str = (\n", | |
| " f\"{algorithm.__name__}('{args[0]}')\" if args else f\"{algorithm.__name__}()\"\n", | |
| " )\n", | |
| "\n", | |
| " setup: str = f\"from __main__ import {algorithm.__name__}\"\n", | |
| " time_taken: float = timeit(stmt=stmt, setup=setup, number=number)\n", | |
| " print(f\"{algorithm.__name__}: {time_taken:.5f} seconds\")" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 108, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'https://www.geeksforgeeks.org/']\n", | |
| "alg1: 0.07131 seconds\n", | |
| "['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'https://www.geeksforgeeks.org/']\n", | |
| "alg2: 0.02130 seconds\n", | |
| "['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'https://www.geeksforgeeks.org/']\n", | |
| "alg3: 0.02427 seconds\n", | |
| "['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'https://www.geeksforgeeks.org/']\n", | |
| "alg4: 0.10071 seconds\n", | |
| "['https://auth.geeksforgeeks.org/user/Chinmoy%20Lenka/articles', 'https://www.geeksforgeeks.org/']\n", | |
| "alg5: 0.05229 seconds\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "algorithms: List[Tuple[Callable, Tuple]] = [\n", | |
| " (alg1, (string,)),\n", | |
| " (alg2, (string,)),\n", | |
| " (alg3, (string,)),\n", | |
| " (alg4, (string,)),\n", | |
| " (alg5, (string1, string2)),\n", | |
| "]\n", | |
| "\n", | |
| "for algorithm, args in algorithms:\n", | |
| " if len(args) == 1:\n", | |
| " print(algorithm(*args))\n", | |
| " else:\n", | |
| " print(algorithm(args[0], args[1]))\n", | |
| " time_algorithm(algorithm=algorithm, args=args)" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": ".venv", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.12.3" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment