Python库,用于在ASCII和非ASCII字符之间转换,保持可读性
项目描述
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)上运行。
版权和许可证
Copyright 2019 Box, Inc. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://apache.ac.cn/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
项目详细信息
下载文件
下载您平台的文件。如果您不确定选择哪个,请了解有关 安装包 的更多信息。
源代码发行版
构建发行版
rotunicode-2.3.0.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 080f0cf13a2adfbd23b574b34df1c81279fc1a04006e6dfa75610cde691403cf |
|
MD5 | 2dd5868d2f24ec6275374101d1adafd6 |
|
BLAKE2b-256 | 0cba9c424818d955c8e9c2693d934c43a8dcfc93cbaa639621c2b4c1abec84c6 |