GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - ada-url/ada-python: Python bindings for Ada URL parser

BranchesTags
Open more actions menu

Latest commit

 

History

119 Commits
119 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
ada_url
ada_url
 
 
docs
docs
 
 
requirements
requirements
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.readthedocs.yaml
.readthedocs.yaml
 
 
LICENSE
LICENSE
 
 
MANIFEST.in
MANIFEST.in
 
 
Makefile
Makefile
 
 
README.rst
README.rst
 
 
benchmark.py
benchmark.py
 
 
pyproject.toml
pyproject.toml
 
 
setup.py
setup.py
 
 
update-wpt.sh
update-wpt.sh
 
 
update_ada.py
update_ada.py
 
 
update_sdist.py
update_sdist.py
 
 

Repository files navigation

ada-url

This is ada_url, a fast standard-compliant Python library for working with URLs based on the Ada URL parser.

  • Documentation
  • Development
  • Ada

Installation

Install from PyPI:

pip install ada_url

Usage examples

Parsing URLs

The URL class is intended to match the one described in the WHATWG URL spec.

>>> from ada_url import URL
>>> urlobj = URL('https://example.org/path/../file.txt')
>>> urlobj.href
'https://example.org/path/file.txt'

The parse_url function returns a dictionary of all URL elements:

>>> from ada_url import parse_url
>>> parse_url('https://user:[email protected]:80/api?q=1#2')
{
    'href': 'https://user:[email protected]:80/api?q=1#2',
    'username': 'user',
    'password': 'pass',
    'protocol': 'https:',
    'port': '80',
    'hostname': 'example.org',
    'host': 'example.org:80',
    'pathname': '/api',
    'search': '?q=1',
    'hash': '#2',
    'origin': 'https://example.org:80',
    'host_type': <HostType.DEFAULT: 0>,
    'scheme_type': <SchemeType.HTTPS: 2>
}

Altering URLs

Replacing URL components with the URL class:

>>> from ada_url import URL
>>> urlobj = URL('https://example.org/path/../file.txt')
>>> urlobj.host = 'example.com'
>>> urlobj.href
'https://example.com/file.txt'

Replacing URL components with the replace_url function:

>>> from ada_url import replace_url
>>> replace_url('https://example.org/path/../file.txt', host='example.com')
'https://example.com/file.txt'

Search parameters

The URLSearchParams class is intended to match the one described in the WHATWG URL spec.

>>> from ada_url import URLSearchParams
>>> obj = URLSearchParams('key1=value1&key2=value2')
>>> list(obj.items())
[('key1', 'value1'), ('key2', 'value2')]

The parse_search_params function returns a dictionary of search keys mapped to value lists:

>>> from ada_url import parse_search_params
>>> parse_search_params('key1=value1&key2=value2')
{'key1': ['value1'], 'key2': ['value2']}

Internationalized domain names

The idna class can encode and decode IDNs:

>>> from ada_url import idna
>>> idna.encode('Bücher.example')
b'xn--bcher-kva.example'
>>> idna.decode(b'xn--bcher-kva.example')
'bücher.example'

WHATWG URL compliance

This library is compliant with the WHATWG URL spec. This means, among other things, that it properly encodes IDNs and resolves paths:

>>> from ada_url import URL
>>> parsed_url = URL('https://www.GOoglé.com/./path/../path2/')
>>> parsed_url.hostname
'www.xn--googl-fsa.com'
>>> parsed_url.pathname
'/path2/'

Contrast that with the Python standard library's urllib.parse module, which loosely follows the older RFC 3978 standard:

>>> from urllib.parse import urlparse
>>> parsed_url = urlparse('https://www.GOoglé.com/./path/../path2/')
>>> parsed_url.hostname
'www.googlé.com'
>>> parsed_url.path
'/./path/../path2/'

Performance

This package uses CFFI to call the Ada C library's functions, which makes it faster than the Python standard library's urllib.parse module for most applications.

An alternative package, can_ada, uses pybind11 to interact with the Ada C++ library functions, which is even faster.

About

Python bindings for Ada URL parser

ada-url.com

Topics

whatwg-url

Resources

Readme
MIT license
Activity
Custom properties

Stars

71 stars

Watchers

4 watching

Forks

9 forks
Report repository

Releases

Packages

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.