Skip to main content

Unicode grapheme helpers

Project description

grapheme

A Python package for working with user perceived characters. More specifically, string manipulation and calculation functions for workong with grapheme cluster groups (graphemes) as defined by the Unicode Standard Annex #29.

documentation

pip install grapheme

What? Why?

Unicode strings are made up of a series of unicode characters, but a unicode character does not always map to a user perceived character. Some human perceived characters are represented as two or more unicode characters.

However, all built in python string functions and string methods work with single unicode characters without considering their connection to each other.

>>> string = 'u̲n̲d̲e̲r̲l̲i̲n̲e̲d̲'
>>> len(string)
20
>>> grapheme.length(string)
10
>>> string[:3]
'u̲n'
>>> grapheme.substr(string, 0, 3)
'u̲n̲d̲'

This library implements the unicode default rules for extended grapheme clusters, and provides a set of functions for string manipulation based on graphemes.

Documentation

todo: add docs

When should I consider graphemes instead of unicode characters?

You should consider working with graphemes over unicode code points when:

  • You wish to count characters as perceived by users.

  • You wish to split or truncate text at some user perceived lengths.

  • You wish to split or truncate text without risk of corrupting the characters.

  • Formatting text by length, such as creating text based tables in monospaced fonts

You should work with normal python string functions when:

  • You wish to count or split by unicode codepoints for compliance with storage limitations (such as database maximum length)

  • When working with systems that put limits on strings by unicode character lengths

  • You absolutely require the performance benefits of using the built ins

  • If simplicity is more important than accuracy

Performance

todo: make some benchmarks and work with performance fixes.

Examples of grapheme cluster groups

This is not a completet list, but a some examples of when graphemes use multiple characters:

  • CR+LF

  • Hangul (korean)

  • Emoji with modifiers

  • Combining marks

  • Zero Width Join

Development quick start

If you wish to contribute or edit this package, create a fork and clone it.

Then install in locally editable (-e) mode and run the tests.

pip install -e .[test]
py.test

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grapheme-0.1.0.dev0.tar.gz (12.5 kB view details)

Uploaded Source

File details

Details for the file grapheme-0.1.0.dev0.tar.gz.

File metadata

  • Download URL: grapheme-0.1.0.dev0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for grapheme-0.1.0.dev0.tar.gz
Algorithm Hash digest
SHA256 fe0fd0e0b74f816dd7381e14549ca3e50726b0023c859c08a0ca5579a30aa8f8
MD5 e61fb0da959155d1cd12e29c46a07192
BLAKE2b-256 55ad0cc0b4f80a8d67f0e6877a937d9e082e54fb086359bbf9bc734bf2eb050e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page