在url中表示数字的最佳方式。
hexahexacontadecimal的Python项目详细描述
将数字编码成url的最简洁的方法。
十六进制是一种表示数字或二进制的压缩格式 url中的数据。它使用URL中允许的所有字符–生成unreserved characters 它是在url中表示正整数的最简洁的方法。
注意urllib.quoteescapes the tilde character (~),这在 rfc3986,所以如果在hhc数据上使用它,将浪费字节。使用 如果必须,请改为提供hhc_url_quote函数。根据定义 尽管hhc值不需要任何url引用。
用法
>>> from hexahexacontadecimal import hhc, hhc_to_int >>> print(hhc(302231454903657293676544)) iFsGUkO.0tsxw >>> print(hhc_to_int('iFsGUkO.0tsxw')) 302231454903657293676544
在url
>>> n = 292231454903657293676544 >>> import base64 >>> print(hhc_url_quote(base64.urlsafe_b64encode(long_to_binary(n)))) PeHmHzZFTcAAAA%3D%3D >>> print(hhc_url_quote(hhc(n))) gpE4Xoy7fw5AO
base64与hhc在base64的错误情况下:
>>> n = 64 ** 5 + 1 >>> print(hhc_url_quote(base64.urlsafe_b64encode(long_to_binary(n)))) QAAAAQ%3D%3D >>> print(hhc(n)) ucrDZ
在hhc的错误情况下,base64与hhc:
>>> n = 66 ** 5 + 1 >>> print(hhc_url_quote(base64.urlsafe_b64encode(long_to_binary(n)))) SqUUIQ%3D%3D >>> print(hhc(n)) 100001
你一直想在url中写的那个大sha-512:
>>> n = 2 ** 512 >>> print(hhc_url_quote(base64.urlsafe_b64encode(long_to_binary(n)))) AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%3D >>> print(hhc(n)) JK84xqGD9FMXPNubPghADlRhBUzlqRscC2h~8xmi99PvuQsUCIB2CHGhMUQR8FLm72.Hbbctkqi89xspay~y4
大量储蓄!
储蓄真的很可观吗?
如果您当前正在以简单的方式进行base64编码,那么可以。 将所有数字编码到100000,hhc将导致更短的总数 URL的长度。
>>> sum(len(hhc_url_quote(base64.urlsafe_b64encode(long_to_binary(n)))) for n in range(10 ** 5)) 531584 >>> sum(len(hhc_url_quote(hhc(n))) for n in range(10 ** 5)) 295578
如果我使用base64而不使用填充呢?
那么节省下来的钱就少得多了。但它们仍然是储蓄。 如果你是一个完美主义者,这是你可能关心的事情 关于。
让我们使用this StackOverFlow question:
中的代码来测试它>>> from hexahexacontadecimal.num_encode_base64 import num_encode as num_encode_base64 >>> n = 64 ** 5 + 1 >>> print(hhc_url_quote(num_encode_base64(n))) BAAAAB >>> from hexahexacontadecimal.num_encode_base64 import num_decode as num_decode_base64 >>> num_decode_base64(hhc_url_quote(num_encode_base64(n))) == n True >>> print(hhc(n)) ucrDZ >>> hhc_to_int(hhc(n)) == n True >>> n = 66 ** 5 + 1 >>> print(hhc_url_quote(num_encode_base64(n))) BKpRQh >>> print(hhc_url_quote(hhc(n))) 100001 >>> n = 2 ** 512 >>> hhc_url_quote(num_encode_base64(n)) 'EAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' >>> print(hhc_url_quote(hhc(n))) JK84xqGD9FMXPNubPghADlRhBUzlqRscC2h~8xmi99PvuQsUCIB2CHGhMUQR8FLm72.Hbbctkqi89xspay~y4 >>> sum(len(hhc_url_quote(num_encode_base64(n))) for n in range(10 ** 5)) 295840 >>> sum(len(hhc_url_quote(hhc(n))) for n in range(10 ** 5)) 295578
为什么满足于不够完美?
排序
如果您希望能够对hhc值列表进行数字排序,则 HHC的一种变体,允许这样做。见sortable_hhc。
>>> hhc(67) < hhc(128) False >>> sortable_hhc(67, width=2) < sortable_hhc(128, width=2) True
负数
HHC通过用^ {TT4}$预先编号来表示负数 减去)。这不是一个URL安全字符,因此如果您的URL编码为 负数加上hhc,你会得到%2C,这会额外占用2个 角色。因此,hhc不一定是最短的 负数的表示。
可排序变量还支持负数,并将产生 自然排序顺序(从小到大),如-2、-1、0、1、2。
安装
pip install hexahexacontadecimal
文档
这个文件和文档字符串。
更改日志
2.2.1
- 修正:意外需要pandoc。
2.2
- 支持Python3(向后兼容Python2.7)。
2.1
- 已修复:hhc(-1)将导致无限循环。
- 新:支持负值。
2.0
- 新的:可排序的hhc。hhc的这个变体按字母顺序排列 如等长字符串的数值。
- 更短,更多的pythonic方法名。现在的主要功能是 调用hhc,在python内置的hex函数之后设置样式。到 解码相同,hhc_to_int现在被使用。
- import * from hexahexacontadecimal现在只导入main 功能。
- urlquote已重命名为hhc_url_quote,以便更轻松地 与标准库方法不同。
1.0
初次发布。
在命令行中
使用pyle可以轻松使用 在命令行上为十六进制。
$ wc -c LICENSE MANIFEST setup.py | pyle -m hexahexacontadecimal -e "'%-10s Hexhexconta bytes:' % words[1], hexahexacontadecimal.hhc(int(words[0]))" LICENSE Hexhexconta bytes: MV MANIFEST Hexhexconta bytes: 1z setup.py Hexhexconta bytes: GI total Hexhexconta bytes: ei
许可证
根据BSD开放源代码许可证的条款可以自由使用和修改。