跳转到主要内容

基类,使对象能够根据其属性进行比较。

项目描述

Comparable

Build Status
Coverage Status
Scrutinizer Code Quality
PyPI Version
PyPI Downloads

Comparable是一个库,提供抽象基类,使子类能够根据其属性进行比较“相等”和“相似”。

入门

要求

  • Python 3.3+

安装

Comparable可以使用pip安装

$ pip install comparable

或直接从源代码安装

$ git clone https://github.com/jacebrowning/comparable.git
$ cd comparable
$ python setup.py install

基本用法

安装后,可以从包中导入抽象基类

$ python
>>> import comparable
>>> comparable.__version__
>>> from comparable import SimpleComparable, CompoundComparable

Comparable类使用==作为“相等”的操作,使用%作为“相似”的操作。它们还可以覆盖threshold属性来设置“相似度”比率。

简单可比较类型

简单可比较类型必须覆盖equalitysimilarity方法,分别返回bool和Similarity对象。有关示例,请参阅comparable.simple

复合可比较类型

复合可比较类型包含多个简单可比较类型。它们必须覆盖attributes属性来定义应用于比较的属性。请参阅comparable.compound以获取示例。

示例

Comparable包含许多通用可比较类型

$ python
>>> from comparable.simple import Number, Text, TextEnum, TextTitle
>>> from comparable.compound import Group

一个基本脚本可能看起来像以下内容

from comparable.simple import TextTitle
from comparable import tools

base = TextTitle("The Cat and the Hat")
items = [TextTitle("cat & hat"), TextTitle("cat & the hat")]

print("Equality: {}".format(base == items[0]))
print("Similarity: {}".format(base % items[0]))

print("Duplicates: {}".format(tools.duplicates(base, items)))

对于贡献者

要求

安装

创建虚拟环境

$ make env

运行测试

$ make test
$ make tests  # includes integration tests

构建文档

$ make doc

运行静态分析

$ make pep8
$ make pep257
$ make pylint
$ make check  # includes all checks

准备发布

$ make dist  # dry run
$ make upload

变更日志

1.0 (2015/03/19)

  • 冻结公共API。

0.2.1 (2014/05/17)

  • 在基简单类型中添加了 __bool__。

0.2 (2014/05/12)

  • 向 TestCase 类中添加了相似性断言。

0.1.1 (2014/03/03)

  • README 清理。

0.1 (2014/03/03)

  • 代码清理。

0.0.3 (2013/10/13)

  • Comparable.threshold 现在是一个属性(而不是属性)。

  • 修复了包级别的导入。

0.0.2 (2013/10/04)

  • 用属性替换了 Comparable.equality_list/similarity_dict。

  • 用 threshold 替换了 Comparable.similarity_threshold。

0.0.1 (2013/10/02)

  • Comparable 的初始发布。

项目详情


下载文件

下载您平台上的文件。如果您不确定要选择哪个,了解更多关于 安装包 的信息。

源分发

Comparable-1.0.tar.gz (15.3 kB 查看哈希值)

上传时间

构建分发

Comparable-1.0-py3-none-any.whl (18.1 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面