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.2.0.tar.gz (12.9 kB view details)

Uploaded Source

File details

Details for the file grapheme-0.2.0.tar.gz.

File metadata

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

File hashes

Hashes for grapheme-0.2.0.tar.gz
Algorithm Hash digest
SHA256 c5090bbe29c9efa35c4fbe5c2988d4a398a31252a1c73c14d3aa56592c187737
MD5 7893a91bdb89a452a57914d7af63a62c
BLAKE2b-256 bf95808d966db2c2fad1bf01cb8c559160a60ec5b9243312650c23e5c8587a8b

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