The mutablestring module

The AbstractMutableString is the base of a parce Document.

It is a text string that is mutable via item and slice methods, the += operator and some methods like insert() and append().

If you make modifications while inside a context (using the Python context manager protocol), the modifications (that may not overlap then) are only applied when the context exits for the last time.

class AbstractMutableString(text='')[source]

Bases: object

Abstract base class of a MutableString.

Defines the interface for interacting with the mutable string.

The only thing to implement is where the contents are actually stored, which is an implementation detail. To make a mutable string object work, you should at least implement:

  • text() which should return the full text as a string.

  • _update_text(changes) to update the text according to the changes. Those changes are a sorted iterable of (start, end, text) tuples, and will never overlap. All start/end positions refer to the original state of the text.

For efficiency reasons, you might want to reimplement:

  • set_text() to replace all text in one go

  • __len__() to get the length of the text

  • _get_text() called by __getitem__ to get a slice or single character


Should return the text contents.


Set the text contents.


Return the text contents.

__setitem__(key, text)[source]

Replace the position or slice with text.


Delete the chracter or slice of text.


Get a character or a slice of text.


Called to apply the changes to the text.

The changes is a sorted list of (start, end, text) tuples.


Append text at the end of the document.

insert(pos, text)[source]

Insert text at pos.

text_changed(position, removed, added)[source]

Called after _update_text(). The default implementation does nothing.

class MutableString(text='')[source]

Bases: parce.mutablestring.AbstractMutableString

A Mutable string, storing the string contents in an internal attribute.


Return the text.


Apply the changes to the text.