Skip to content

Python function to split messages (strings) to fit some character limit, e.g. 160 characters

License

Notifications You must be signed in to change notification settings

Elijas/msgsplitter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

msgsplitter

Splits a long piece of text (messages) to multiple text strings (messages) in order to fit within an arbitrary message length limit (useful for SMS, Twitter, etc.).

Whitespace is used as the breaking point for splitting messages. All whitespace in the text is replaced with a single space.

Installation

~ $ pip install msgsplitter

or you can install a local development version after cloning the project:

~ $ pip install -e .

Quick start

>>> import msgsplitter
>>> msg = 'Hello, this is a really long message.'

>>> msgsplitter.split(msg, length_limit=30)
['Hello, this is a really (1/2)', 'long message. (2/2)']

>>> msgsplitter.split(msg, length_limit=30, append_indicator=False)
['Hello, this is a really long', 'message.']

Formatting extensibility

You can create custom formatting classes. Your class will define, how many characters must be reserved in each message for adding the formatting characters after the content split into chunks is made. You can take a look at the two example classes for inspiration: FormatterBase, which is identical to just using the split(...) with the argument append_indicator=False, and the other class IndicatorFormatter, which is identical to just using split(...) with the argument append_indicator=True.

Custom class is passed through the formatter_cls argument and overrides the append_indicator setting. Usage example:

msgsplitter.split('some text', 10, formatter_cls=IndicatorFormatter)

Run tests

~ $ pytest