Skip to main content

Standard Compression Scheme for Unicode

This package implements SCSU as a Python text codec.

Benefits of Unicode compression

Compressed strings typically have fewer bytes than strings encoded as UTF-8 or UTF-16.

Sample Text In UTF-8 In UTF-16 In SCSU
¿Qué es Unicode? 18 bytes 32 bytes 16 bytes
Що таке Юнікод? 27 bytes 30 bytes 16 bytes
Ի՞նչ է Յունիկոդը ? 32 bytes 36 bytes 20 bytes
यूनिकोड क्या है? 42 bytes 32 bytes 17 bytes
ユニコードとは何か? 30 bytes 20 bytes 15 bytes
什麼是Unicode(統一碼/標準萬國碼)? 44 bytes 44 bytes 38 bytes
𐑢𐑳𐑑 𐑦𐑟 𐑿𐑯𐑦𐑒𐑴𐑛? 47 bytes 50 bytes 17 bytes
😀😁😂😃😄😅😆😇😈😉😊😋😌😍😎😏 64 bytes 64 bytes 19 bytes

Requirements

This package requires Python 3.10 or above.

Usage

Source code

Simply import the module and the SCSU codec is ready to use:

import scsu

b = s.encode("SCSU")

To automatically add and remove a byte-order mark signature, use SCSU-SIG instead of SCSU.

Command line interface

To compress a file, use the "encode" command: python3 -m scsu encode unicode.txt > scsu.txt

To decompress a file, use the "decode" command: python3 -m scsu decode scsu.txt > unicode.txt

To automatically add and remove a byte-order mark signature, add the -s option after the encode/decode command.

Errata

CPython bug #79792 causes the sample code (below) to not flush the encoding buffer:

with open(file, mode="w", encoding="SCSU-SIG") as f:
    f.write(s)  # Never flushes the encoding buffer.

A workaround is to import the codecs module, then replace open with codecs.open:

import codecs

with codecs.open(file, "w", encoding="SCSU-SIG") as f:
    f.write(s)  # Always flushes the encoding buffer.

Credits

Encoding logic is heavily based on a sample encoder described in "A survey of Unicode compression" by Doug Ewell and originally written by Richard Gillam in his book Unicode Demystified.

Enhancements to the encoding logic include:

  • A two-character lookahead buffer to avoid a case where switching from Unicode to single-byte mode requires two window switches.
  • Compression of sequential static window characters into a single new dynamic window, to avoid a case where a long string of punctuation is encoded as multiple quoted characters.
  • Uses the Latin-1 Supplement window whenever possible so transcoding text encoded as ISO-8859-1 results in a valid SCSU and ISO-8859-1 byte string.

Decoding logic, however, is entirely original.

Metadata

Release files for scsu 1.1.1

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

Source distribution (sdist)

Source distribution for scsu 1.1.1
File Size Uploaded
scsu-1.1.1.tar.gz 12.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for scsu 1.1.1
File Interpreter ABI Platform
scsu-1.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 29.0 kB

Release files / scsu-1.1.1.tar.gz

Download URL scsu-1.1.1.tar.gz
Size 12.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4a9011b56d8ca49b414c2872af489ab5af3060e9f79340fd20b9a00ee99da528
BLAKE2b-256 checksum
How to use checksums
9502bf973184098372e6f5d93607ef1096221981e51c870cddf8e43318daa03d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.13

Release files / scsu-1.1.1-py3-none-any.whl

Download URL scsu-1.1.1-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
66708da33f7868358d9ec879f217b3a53befd0c558cc7ce46a35909ce9cf59b3
BLAKE2b-256 checksum
How to use checksums
1ac689dbcc99230b8c275e2aa675347aed2649e0c75436f9f328836b8a2c185e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.13

Release history Release notifications | RSS feed

This release

1.1.1 This release

2 release files

1.1

2 release files

1.0

2 release files

0.3

2 release files

0.2

2 release files

0.1

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