Skip to main content

A common library for netloc rule use case.

Project description

os-netloc-rule

Build Status codecov PyPI - Python Version PyPI

A common library for netloc rule use case.

Netloc match is a very common and useful operation on processing URL. For example, netloc blacklist is a series rules of netloc with ALLOWED or DISALLOWED:

abc.example.com ALLOWED
.example.com DISALLOWED

You can skip processing http://www.example.com/001.html becase it match the rule .example.com DISALLOWED.

Install

pip install os-netloc-rule

Usage

  • load rule

    from os_netloc_rule import Matcher
    
    rules = [
        ('www.example.com', 1),
        ('abc.example.com', 2),
        ('abc.example.com:8080', 3),
    ]
    
    matcher = Matcher()
    for netloc, rule in rules:
        matcher.load(netloc, rule)
    
  • match rule

    matcher.match('www.example.com')
    matcher.match('abc.example.com:8080')
    
  • if there are same netloc with different rule, the latter covers the former by default. But you can custom your own cmp function when loading rules

    def cmp(former, latter):
        return former if former > latter else latter
        
    matcher.load(netloc, rule, cmp=cmp)
    
  • dump rules

    for netloc, rule in matcher.dump():
        pass
    
  • delete rule

    delete, rule = matcher.delete('www.example.com')
    

Benchmark

Some benchmarks:

python version operation memory speed
2.7.14 load 100w, 380M 91k/s
2.7.14 match - 118k/s
3.6.4 load 100w, 300M 96k/s
3.6.4 match - 123k/s
pypy-5.7.1 load 100w, 251M 283k/s
pypy-5.7.1 match - 529k/s
pypy3.6-7.2.0 load 100w, 305M 265k/s
pypy3.6-7.2.0 match - 473k/s

Unit Tests

tox

License

MIT licensed.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

os-netloc-rule-0.0.2.tar.gz (6.6 kB view details)

Uploaded Source

File details

Details for the file os-netloc-rule-0.0.2.tar.gz.

File metadata

  • Download URL: os-netloc-rule-0.0.2.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/2.7.15

File hashes

Hashes for os-netloc-rule-0.0.2.tar.gz
Algorithm Hash digest
SHA256 24bb35f51375fba6b4d93f98d2e1e42a4e6847eaf02a7a1de9228cdcee0888c3
MD5 a47f48530d3edabc388eddfaf9615ccd
BLAKE2b-256 2a8a30dca03a8e2e24a5a0170cf5e16890adc928cf067f3c14e386b5e9457237

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page