Skip to main content

A Python wrapper for Hyperscan

Project description

Hyperscan Python Wrapper

PyPI Version Python Versions License Build Status Documentation

Hyperscan Python Wrapper 是一个基于 Cython 的 Python 封装库,用于 Intel 的 Hyperscan 库。Hyperscan 是一个高性能的正则表达式匹配库,适用于大规模数据流处理。

本项目提供了对 Hyperscan 核心功能的 Python 接口,使开发者能够轻松地在 Python 中使用 Hyperscan 的强大功能。


功能特性

  • 高性能正则表达式匹配:基于 Hyperscan 的高效匹配引擎。
  • 多模式支持:支持同时编译和匹配多个正则表达式。
  • 跨平台支持:支持 Windows、Linux 和 macOS。
  • 简单易用:提供简洁的 Python API,易于集成到现有项目中。

安装

从 PyPI 安装

pip install pyhyperscan
pip install -i https://test.pypi.org/simple/ pyhyperscan

从源码安装

  1. 克隆仓库:

    git clone https://gitee.com/fgc1/regex_engine_py.git
    cd hyperscan_package
    
  2. 安装依赖:

    pip install -r requirements.txt
    
  3. 构建并安装:

    python setup.py build_ext --inplace  # 本地调试用
    pip install .
    python setup.py sdist bdist_wheel    # 打包发布用
    
  4. 发布到python源:

twine upload --repository testpypi dist/* --verbose
twine upload --repository pypi dist/* --verbose

快速开始

以下是一个简单的示例,展示如何使用 pyhyperscan 进行正则表达式匹配。

import pyhyperscan


# 定义回调函数
def match_callback(id, from_, to, flags):
    print(f"Match found: id={id}, from={from_}, to={to}, flags={flags}")


if __name__ == '__main__':
    # 编译正则表达式
    patterns = ["hello", "world"]

    ret, msg = results = pyhyperscan.check_patterns(patterns, [0, 0])
    if not ret:
        print(f"error patterns info: {msg}")
        exit(1)

    flags = [
        pyhyperscan.HS_FLAG_CASELESS_PY | pyhyperscan.HS_FLAG_SOM_LEFTMOST_PY,
        pyhyperscan.HS_FLAG_SOM_LEFTMOST_PY
    ]
    ids = [1, 2]
    db = pyhyperscan.compile_multi_patterns(patterns, flags, ids)
    # 扫描数据
    data = "Hello, world! Hello, Python!"
    # match_callback也可以是类方法
    pyhyperscan.scan_data(db, data, match_callback)

    # 释放数据库
    pyhyperscan.free_database(db)

文档

API 参考

compile_pattern(pattern: bytes, flags: int = 0) -> Database

编译单个正则表达式模式。

  • 参数
    • pattern: 正则表达式模式(字节串)。
    • flags: 匹配标志(如 HS_FLAG_CASELESS)。
  • 返回:编译后的数据库对象。

compile_multi_patterns(patterns: List[bytes], flags: List[int], ids: List[int]) -> Database

编译多个正则表达式模式。

  • 参数
    • patterns: 正则表达式模式列表(字节串)。
    • flags: 匹配标志列表。
    • ids: 模式 ID 列表。
  • 返回:编译后的数据库对象。

scan_data(db: Database, data: bytes, callback: Callable) -> int

扫描数据并调用回调函数处理匹配结果。

  • 参数
    • db: 编译后的数据库对象。
    • data: 待扫描的数据(字节串)。
    • callback: 匹配回调函数。
  • 返回:匹配次数。

free_database(db: Database)

释放数据库对象。

  • 参数
    • db: 编译后的数据库对象。

贡献

欢迎贡献代码!请阅读 贡献指南 了解如何参与项目开发。


许可证

本项目基于 MIT 许可证 发布。


致谢

  • 感谢 Intel Hyperscan 提供的高性能正则表达式匹配库。
  • 感谢 Cython 提供的 Python C 扩展支持。

联系方式

如有任何问题或建议,请联系:


支持

如果你觉得这个项目有用,请给它一个 ⭐️ GitHub Star


相关项目

  • Hyperscan:Intel 的高性能正则表达式匹配库。
  • Cython:Python 的 C 扩展工具。

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

pyhyperscan-0.1.9.tar.gz (1.8 MB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

pyhyperscan-0.1.9-cp39-cp39-macosx_11_0_arm64.whl (7.6 MB view details)

Uploaded CPython 3.9macOS 11.0+ ARM64

pyhyperscan-0.1.9-cp38-cp38-win_amd64.whl (1.7 MB view details)

Uploaded CPython 3.8Windows x86-64

pyhyperscan-0.1.9-cp38-cp38-manylinux2014_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.8

File details

Details for the file pyhyperscan-0.1.9.tar.gz.

File metadata

  • Download URL: pyhyperscan-0.1.9.tar.gz
  • Upload date:
  • Size: 1.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.6

File hashes

Hashes for pyhyperscan-0.1.9.tar.gz
Algorithm Hash digest
SHA256 0475230e1235a5bc45c66b0e1e9944d0142645daa28ccaf036e49f5f1814b616
MD5 dc9c7c7500350ac0ec5228ef74d783fa
BLAKE2b-256 5226abc43e56d1c90a5425f594281c79de19adbada15be6c426823760fa9a029

See more details on using hashes here.

File details

Details for the file pyhyperscan-0.1.9-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for pyhyperscan-0.1.9-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 e72c9ca940c68fb579ab3c595f10f02210fdc933b5d0a6d8e21d217233f4a5bb
MD5 08fb13678f52f7f9b57e5533dfbbd3e7
BLAKE2b-256 6e4f355b930e4dec29282b8aa0721513926fa550e10b9ead32e1d82754898ab2

See more details on using hashes here.

File details

Details for the file pyhyperscan-0.1.9-cp38-cp38-win_amd64.whl.

File metadata

  • Download URL: pyhyperscan-0.1.9-cp38-cp38-win_amd64.whl
  • Upload date:
  • Size: 1.7 MB
  • Tags: CPython 3.8, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.6

File hashes

Hashes for pyhyperscan-0.1.9-cp38-cp38-win_amd64.whl
Algorithm Hash digest
SHA256 40dbbbdd1e148a259d062a49800b2664c6c772ed12d794772f8494546483f188
MD5 c28f133d35d6a869057cb758ed850440
BLAKE2b-256 563a712000fe2a7a4b38d3d70a20cd2cd53a47c8c3f0d98b05fed1b9dc998fa7

See more details on using hashes here.

File details

Details for the file pyhyperscan-0.1.9-cp38-cp38-manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for pyhyperscan-0.1.9-cp38-cp38-manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3d883ff690b4274d28762bef6e68a96e0fc6c4d946a4fb64ead1b01254c002a0
MD5 55cf098ef5ec6909bd24dcdc3682cf84
BLAKE2b-256 c6ab409a2e50de3bce6f2422c075e002ad3db4ffa066ef2261b12a5617db7582

See more details on using hashes here.

Supported by

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