🔍 Code Extractor

function is_absolute_url

Maturity: 22

Determines whether a given URL is absolute (contains a network location/domain) or relative.

File:
/tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/compat.py
Lines:
44 - 45
Complexity:
simple

Purpose

This function checks if a URL string is absolute by parsing it and verifying the presence of a network location (netloc) component. An absolute URL includes a scheme and domain (e.g., 'https://example.com/path'), while a relative URL does not (e.g., '/path/to/resource'). This is commonly used in web scraping, URL validation, and link processing to distinguish between internal and external links or to determine if a URL needs to be joined with a base URL.

Source Code

def is_absolute_url(url):
    return bool(urlparse(url).netloc)

Parameters

Name Type Default Kind
url - - positional_or_keyword

Parameter Details

url: A string representing the URL to check. Can be any valid URL format including absolute URLs (e.g., 'https://example.com'), relative URLs (e.g., '/path'), or malformed URLs. The function expects a string input; passing None or non-string types may cause errors.

Return Value

Returns a boolean value: True if the URL is absolute (contains a network location component like 'example.com'), False if the URL is relative (no network location). The function uses bool() to convert the netloc attribute to a boolean, so any non-empty netloc string returns True, and an empty string returns False.

Required Imports

from urllib.parse import urlparse

Conditional/Optional Imports

These imports are only needed under specific conditions:

from urlparse import urlparse

Condition: Python 2.x compatibility - urlparse module location differs between Python 2 and 3

Optional

Usage Example

from urllib.parse import urlparse

def is_absolute_url(url):
    return bool(urlparse(url).netloc)

# Example usage
print(is_absolute_url('https://www.example.com/path'))  # True
print(is_absolute_url('http://example.com'))  # True
print(is_absolute_url('/relative/path'))  # False
print(is_absolute_url('path/to/file'))  # False
print(is_absolute_url('//example.com/path'))  # True (protocol-relative URL)
print(is_absolute_url(''))  # False

Best Practices

  • This function only checks for the presence of a network location (netloc), not whether the URL is valid or reachable
  • Protocol-relative URLs (starting with '//') are considered absolute by this function
  • The function does not validate URL syntax or check if the scheme is present; it only checks for netloc
  • For Python 2/3 compatibility, ensure the correct urlparse import is used based on the Python version
  • Consider adding input validation to handle None or non-string inputs if needed in production code
  • This function treats URLs with only a scheme (e.g., 'http://') as relative since netloc would be empty

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function get_absolute_url 69.5% similar

    Extracts the base URL (scheme + netloc) from a given URL by removing the path component.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/compat.py
  • function resolve_base_url 51.3% similar

    Extracts and returns the base URL (protocol and hostname) from a given URL string by removing path and query components.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/runtime/auth/providers/saml_token_provider.py
  • function validate_sharepoint_url 48.9% similar

    Validates that a given URL string conforms to SharePoint site URL format requirements, checking for proper protocol, domain, and path structure.

    From: /tf/active/vicechatdev/SPFCsync/validate_config.py
  • class WebPath 44.4% similar

    WebPath is a specialized ResourcePath subclass that represents web-based resource paths, handling both absolute URLs and relative paths with web-specific path parsing.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/internal/paths/web.py
  • class ResourcePath_v1 43.6% similar

    Represents the full (absolute) or parts (relative) path of SharePoint artifacts like site collections, webs, files, and folders in the database.

    From: /tf/active/vicechatdev/SPFCsync/venv/lib64/python3.11/site-packages/office365/sharepoint/types/resource_path.py
← Back to Browse