跳转到主要内容

Python库,用于在ASCII和非ASCII字符之间转换,保持可读性

项目描述

http://opensource.box.com/badges/active.svg https://travis-ci.org/box/rotunicode.png?branch=master https://coveralls.io/repos/box/rotunicode/badge.png https://img.shields.io/pypi/v/rotunicode.svg https://img.shields.io/pypi/dm/rotunicode.svg

RotUnicode 是一个 Python 库,可以将包含 ASCII 字符的字符串转换为包含非 ASCII 字符的字符串,而不会失去可读性。

>>> 'Hello World!'.encode('rotunicode')
Ĥȅľľő Ŵőŕľď!
>>> 'Ĥȅľľő Ŵőŕľď!'.decode('rotunicode')
Hello World!

在上面的例子中,'Hello World' 字符串包含所有 ASCII 字符。使用 RotUnicode 编码它将得到 'Ĥȅľľő Ŵőŕľď',它看起来像 'Hello World',但包含所有非 ASCII 字符。

为什么命名为 RotUnicode?

RotUnicode 表示 rotate-to-unicode。对于那些对 Unicode 有噩梦的人来说,它可以称为 rotten-unicode。它受到 Rot13 的启发。

支持的字符

RotUnicode 将英文字母的大小写和数字 0 到 9 转换为非 ASCII 字符。所有超出此范围的字符保持不变。

>>> 'हेलो World!'.encode('rotunicode')
हेलो Ŵőŕľď!
>>> 'हेलो Ŵőŕľď!'.decode('rotunicode')
हेलो World!

安装

要安装,只需

pip install rotunicode

使用

>>> from rotunicode import ruencode
>>> ruencode('Hello World!')
Ĥȅľľő Ŵőŕľď!
>>> rudecode('Ĥȅľľő Ŵőŕľď!')
Hello World!

作为编解码器

在 Python 2 中,RotUnicode 也可以用作编解码器,但必须首先使用 codecs 库进行注册。这允许 Python 知道如何调用函数来使用 RotUnicode 编码或解码字符串。

>>> import codecs
>>> from rotunicode import RotUnicode
>>> codecs.register(RotUnicode.search_function)
>>> 'Hello World!'.encode('rotunicode')
Ĥȅľľő Ŵőŕľď!

命令行

安装 RotUnicode 还包括一个命令行工具。

$ rotunicode "Hello World"
Ĥȅľľő Ŵőŕľď!
$ rotunicode -d "Ĥȅľľő Ŵőŕľď!"
Hello World!
$ echo "Hello World!" > hello.txt
$ rotunicode -f hello.txt
Ĥȅľľő Ŵőŕľď!
$ cat hello.txt | rotunicode -f
Ĥȅľľő Ŵőŕľď!

为什么我应该使用 RotUnicode?

RotUnicode 在测试中非常有用,因为它减少了开发者测试非 ASCII 字符串的摩擦。例如,假设您有一个用于地址簿应用的联系人类的类

class Contact(object):

    def __init__(self, first_name, last_name):
        super(Contact, self).__init__()
        self.first_name = first_name
        self.last_name = last_name

    def display_name(self):
        return '{} {}'.format(self.first_name, self.last_name)

大多数开发者会这样测试

from unittest import TestCase
from contact import Contact

class ContactTests(TestCase):

    def test_display_name(self):
        contact = Contact('John', 'Doe’)
        self.assertEqual('John Doe', contact.display_name()))

这个测试是好的。但它将错过捕获代码中非 ASCII 字符的问题。要求开发者记住如何输入非 ASCII 字符是不切实际的。使用 RotUnicode,这很简单

from unittest import TestCase
from contact import Contact

class ContactTests(TestCase):

    def test_display_name_with_ascii_name(self):
        contact = Contact(u'John', u'Doe')
        self.assertEqual(u'John Doe', contact.display_name())

    def test_display_name_with_non_ascii_name(self):
        contact = Contact(ruencode(u'John'), ruencode(u'Doe'))
        self.assertEqual(ruencode(u'John Doe'), contact.display_name())

这是一个 Python (问题18695) 中非 ASCII 字符的 bug 的示例 -

>>> import os, errno
>>> name = 'foo'.encode('rotunicode')
>>> os.mkdir(name)
>>> print(name)
ƒőő
>>> os.path.exists(name)
True
>>> os.statvfs(name)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2:
ordinal not in range(128)

贡献

查看 CONTRIBUTING

设置

创建虚拟环境并安装包

mkvirtualenv rotunicode
pip install -r requirements-dev.txt

测试

使用以下命令运行所有测试

tox

tox 测试包括通过 pep8 和 pylint 进行代码风格检查。

tox 测试配置为在 Python 2.7、3.4、3.5、3.6、3.7 和 PyPy2.7(版本 5.10)上运行。

项目详细信息


下载文件

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

源代码发行版

rotunicode-2.3.0.tar.gz (15.2 kB 查看散列)

上传时间 源代码

构建发行版

rotunicode-2.3.0-py2.py3-none-any.whl (12.8 kB 查看散列)

上传时间 Python 2 Python 3

支持者