Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

textcompat

A Python library handling conversions from typing.Text (Python 2 unicode, Python 3 str) to UTF-8, URI, stdin/stdout, and filesystem strs, and vice versa.

Specifically, it handles the following conversions:

Function Python 2 Python 3
text_to_utf_8_str unicode -> UTF-8 str No-op
utf_8_str_to_text UTF-8 str -> unicode No-op
text_to_uri_str unicode -> URI str str -> URI str
uri_str_to_text URI str -> decoded unicode URI str -> decoded str
text_to_stdout_str unicode -> str in stdout encoding No-op
stdin_str_to_text str in stdin encoding -> unicode No-op
text_to_filesystem_str unicode -> str in filesystem encoding No-op
filesystem_str_to_text str in filesystem encoding -> unicode No-op

To determine the filesystem encoding, the library tries to use sys.getfilesystemencoding(). If it returns None, the library detects the operating system and uses reasonable defaults:

  • NT: 'mbcs'
  • POSIX: 'utf-8'

Example

On an NT machine with:

  • sys.getdefaultencoding() == 'ascii'
  • sys.getfilesystemencoding() == 'mbcs'
  • locale.getpreferredencoding() == 'cp936'
  • sys.stdin.encoding == 'cp936'
  • sys.stdout.encoding == 'cp936'
# coding=utf-8
from __future__ import print_function
from textcompat import *

text = u'测试A1你我他中文123!@#¥%()【】~*'

print(repr(text_to_utf_8_str(text)))
# Python 2:
# '\xe6\xb5\x8b\xe8\xaf\x95A1\xe4\xbd\xa0\xe6\x88\x91\xe4\xbb\x96\xe4\xb8\xad\xe6\x96\x87123!@#\xef\xbf\xa5%\xef\xbc\x88\xef\xbc\x89\xe3\x80\x90\xe3\x80\x91\xef\xbd\x9e*'
# Python 3:
# '测试A1你我他中文123!@#¥%()【】~*'

assert type(utf_8_str_to_text(text_to_utf_8_str(text))) is type(text) and utf_8_str_to_text(
    text_to_utf_8_str(text)) == text

print(repr(text_to_uri_str(text)))
# Python 2 and 3:
# '%E6%B5%8B%E8%AF%95A1%E4%BD%A0%E6%88%91%E4%BB%96%E4%B8%AD%E6%96%87123%21%40%23%EF%BF%A5%25%EF%BC%88%EF%BC%89%E3%80%90%E3%80%91%EF%BD%9E%2A'

assert type(utf_8_str_to_text(text_to_utf_8_str(text))) is type(text) and uri_str_to_text(text_to_uri_str(text)) == text

print(repr(text_to_stdout_str(text)))
# Python 2: '\xb2\xe2\xca\xd4A1\xc4\xe3\xce\xd2\xcb\xfb\xd6\xd0\xce\xc4123!@#\xa3\xa4%\xa3\xa8\xa3\xa9\xa1\xbe\xa1\xbf\xa1\xab*'
# Python 3: '测试A1你我他中文123!@#¥%()【】~*'

assert type(stdin_str_to_text(text_to_stdout_str(text))) is type(text) and stdin_str_to_text(
    text_to_stdout_str(text)) == text

print(repr(text_to_filesystem_str(text)))
# Python 2: '\xb2\xe2\xca\xd4A1\xc4\xe3\xce\xd2\xcb\xfb\xd6\xd0\xce\xc4123!@#\xa3\xa4%\xa3\xa8\xa3\xa9\xa1\xbe\xa1\xbf\xa1\xab*'
# Python 3: '测试A1你我他中文123!@#¥%()【】~*'

assert type(utf_8_str_to_text(text_to_utf_8_str(text))) is type(text) and filesystem_str_to_text(
    text_to_filesystem_str(text)) == text

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

Release files for textcompat 0.1.0a1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for textcompat 0.1.0a1
File Size Uploaded
textcompat-0.1.0a1.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for textcompat 0.1.0a1
File Interpreter ABI Platform
textcompat-0.1.0a1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 7.8 kB

Release files / textcompat-0.1.0a1.tar.gz

Download URL textcompat-0.1.0a1.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
09de7dd9400adbaae377a3210055f7164bdafcf35c9d42229fca6eb8124312a7
BLAKE2b-256 checksum
How to use checksums
eb9908c5b20d64f37a18503407cc84182dddb40a6efef009dfc3e2261e4986aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release files / textcompat-0.1.0a1-py2.py3-none-any.whl

Download URL textcompat-0.1.0a1-py2.py3-none-any.whl
Size 4.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
1dc404c03188aa29caf4c176617c07c59f7724a130a1366541f97a743ce8e5fb
BLAKE2b-256 checksum
How to use checksums
59da7f8d84aa16b8318244c744d54a8f377ae99d6131f27c2996a910f89270a8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.2

Release history Release notifications | RSS feed

This release

0.1.0a1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page