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.
~ $ pip install msgsplitter
or you can install a local development version after cloning the project:
~ $ pip install -e .
>>> 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.']
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)
~ $ pytest