跳转到主要内容

操作变换的Python实现。

项目描述

Python-OTType

操作变换(OT)的Python库。基本思想遵循https://github.com/ottypes/docs上的规范。

支持的Python版本:CPython 3.9 - 3.12,PyPy 3.9 - 3.10

安装

pip install python-ottype

基本用法

import ottype

assert ottype.apply('asdf', [3, '123', {'d':'f'}]) == 'asd123'

OT操作

跳过

对象类型: int

跳过当前位置的 n 个字符表示为 n

assert apply('asdf', [3]) == 'asdf'

插入

对象类型: str

在当前位置插入字符串 s 表示为 s

assert apply('asdf', ['qwer']) == 'qwerasdf'
assert apply('asdf', [2, 'qwer']) == 'asqwerdf'

删除

对象类型: dict

删除当前位置的字符串 s 表示为 {'d': s}

assert apply('asdf', [{'d': 'as'}]) == 'df'
assert apply('asdf', [1, {'d': 'sd'}]) == 'af'

支持的功能

OT = int | str | dict[str, str]

check(ots: Sequence[OT], *, check_unoptimized: bool = True) -> bool

检查序列是否仅包含有效的OT。如果 check_unoptimizedTrue,则仅接受OT的规范化序列。

assert check(['a', 4, 'b'])
assert not check(['a', 'b'])  # is not normalized
assert not check([3])  # is not normalized

apply(doc: str, ots: Sequence[OT], *, check_unoptimized: bool = True) -> str

将一系列OT应用于字符串。

assert apply('abcde', [2, 'qq', {'d': 'c'}, 1, 'w']) == 'abqqdwe'

inverse_apply(doc: str, ots: Sequence[OT], *, check_unoptimized: bool = True) -> str

逆应用一系列OT到字符串。

assert inverse_apply(apply(doc, ots), ots) == doc

normalize(ots: Sequence[OT]) -> Sequence[OT]

规范化一系列OT:合并连续的OT并删除最后一个跳过操作。

assert normalize([1, 2, 'as', 'df', {'d': 'qw'}, {'d': 'er'}, 3]) \
        == [3, 'asdf', {'d': 'qwer'}]

transform(ots1: Sequence[OT], ots2: Sequence[OT]) -> Sequence[OT]

将具有属性的自定义类型序列进行转换

assert apply(apply(doc, ots1), transform(ots2, ots1, 'left')) \
        == apply(apply(doc, ots2), transform(ots1, ots2, 'right'))

compose(ots1: Sequence[OT], ots2: Sequence[OT]) -> Sequence[OT]

组合具有属性的两个自定义类型序列

assert apply(apply(doc, ots1), ots2) == apply(doc, compose(ots1, ots2))

基准测试(在CPython 3.12.1中)

基准测试:apply操作

len(doc) len(ots) python (Kops/s) cython (Kops/s)
100 5 289.58 ( 1.00x) 663.47 ( 2.29x)
100 10 206.50 ( 1.00x) 513.00 ( 2.48x)
100 20 114.52 ( 1.00x) 298.82 ( 2.61x)
100 50 48.37 ( 1.00x) 127.90 ( 2.64x)
100 100 26.96 ( 1.00x) 71.84 ( 2.66x)
1000 5 412.41 ( 1.00x) 974.43 ( 2.36x)
1000 10 220.19 ( 1.00x) 493.53 ( 2.24x)
1000 20 128.66 ( 1.00x) 317.18 ( 2.47x)
1000 50 37.56 ( 1.00x) 98.56 ( 2.62x)
1000 100 23.43 ( 1.00x) 64.25 ( 2.74x)
10000 5 203.14 ( 1.00x) 322.31 ( 1.59x)
10000 10 142.25 ( 1.00x) 279.79 ( 1.97x)
10000 20 95.63 ( 1.00x) 202.97 ( 2.12x)
10000 50 48.58 ( 1.00x) 122.09 ( 2.51x)
10000 100 21.17 ( 1.00x) 57.59 ( 2.72x)

基准测试:inverse_apply操作

len(doc) len(ots) python (Kops/s) cython (Kops/s)
100 5 194.12 ( 1.00x) 408.56 ( 2.10x)
100 10 113.27 ( 1.00x) 269.10 ( 2.38x)
100 20 60.99 ( 1.00x) 150.49 ( 2.47x)
100 50 26.35 ( 1.00x) 64.27 ( 2.44x)
100 100 16.62 ( 1.00x) 42.87 ( 2.58x)
1000 5 309.57 ( 1.00x) 565.47 ( 1.83x)
1000 10 153.62 ( 1.00x) 351.55 ( 2.29x)
1000 20 69.48 ( 1.00x) 162.52 ( 2.34x)
1000 50 37.12 ( 1.00x) 91.88 ( 2.48x)
1000 100 17.06 ( 1.00x) 42.51 ( 2.49x)
10000 5 145.11 ( 1.00x) 257.81 ( 1.78x)
10000 10 96.66 ( 1.00x) 198.97 ( 2.06x)
10000 20 65.18 ( 1.00x) 140.90 ( 2.16x)
10000 50 27.40 ( 1.00x) 67.74 ( 2.47x)
10000 100 13.53 ( 1.00x) 32.23 ( 2.38x)

项目详情


下载文件

下载适合您平台的应用程序文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源代码分发

python_ottype-24.6.0.tar.gz (11.3 kB 查看散列)

上传时间 源代码

构建的分发

python_ottype-24.6.0-pp310-pypy310_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (182.2 kB 查看散列)

上传时间 PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_ottype-24.6.0-pp39-pypy39_pp73-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (182.1 kB 查看散列)

上传时间 PyPy manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_ottype-24.6.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (532.9 kB 查看哈希值)

上传时间 CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_ottype-24.6.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (541.9 kB 查看哈希值)

上传时间 CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_ottype-24.6.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (503.8 kB 查看哈希值)

上传时间 CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

python_ottype-24.6.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (507.9 kB 查看哈希值)

上传时间 CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

支持

AWSAWS 云计算和安全赞助商 DatadogDatadog 监控 FastlyFastly CDN GoogleGoogle 下载分析 MicrosoftMicrosoft PSF赞助商 PingdomPingdom 监控 SentrySentry 错误日志 StatusPageStatusPage 状态页面