Mutable Strings - Any libraries that offer this?

C

casebash

Hi,

I have searched this list and found out that Python doesn't have a
mutable string class (it had an inefficient one, but this was removed
in 3.0). Are there any libraries outside the core that offer this?

Thanks,

Chris
 
C

Carl Banks

Hi,

I have searched this list and found out that Python doesn't have a
mutable string class (it had an inefficient one, but this was removed
in 3.0). Are there any libraries outside the core that offer this?

I just did a brief search on Python Packaging Index (pypi.python.org)
and saw nothing. You might have better luck.

I suspect there is not a mutable string class partly because there is
little demand, partly because it'd be hard to make an object that is
usable everywhere a string is. For instance, a third-party string
object might not work with the re module.

The core does have some classes that are kind of like strings but
mutable. The array module can create mutable arrays of characters
which are somewhat like strings. Depending on your use case some
other things might suffice (such as mmap, io.StringIO, or even a list
of characters).


Carl Banks
 
J

John Machin

A mutable string would not (AFAICT) be usefully implementable as a
subclass of the built-in string types. So even if such a type existed,
it would not be useable with all the functionality that works with
strings.

What is it you're trying to do that makes you search for a mutable
string type? It's likely that a better approach can be found.

OK, I'll bite: where does the Python 3.x bytearray type fit into your
taxonomy? At first glance it appears to be mutable and have a fair
swag of functionality.
 
N

Neil Hodgson

casebash:
I have searched this list and found out that Python doesn't have a
mutable string class (it had an inefficient one, but this was removed
in 3.0). Are there any libraries outside the core that offer this?

I wrote a gap buffer implementation for Python 2.5 allowing
character, unicode character and integer elements.

http://code.google.com/p/gapbuffer/

Its not seen much use or any maintenance so is unlikely to work with
Python 3.x.

Neil
 
S

Steven D'Aprano

A mutable string would not (AFAICT) be usefully implementable as a
subclass of the built-in string types. So even if such a type existed,
it would not be useable with all the functionality that works with
strings.

If applications ignore duck-typing and do isinstance(value, str), it's
arguably the application and not the value that is broken.

Besides, with the new __isinstance__ method, surely such a mutable string
could claim to be an instance of string without needing to inherit from
string?

What is it you're trying to do that makes you search for a mutable
string type? It's likely that a better approach can be found.

When dealing with very large strings, it is wasteful to have to duplicate
the entire string just to mutate a single character.

However, when dealing with very large strings, it's arguably better to
use the rope data structure instead.


http://en.wikipedia.org/wiki/Rope_(computer_science)
 
T

Thomas Guettler

casebash said:
Hi,

I have searched this list and found out that Python doesn't have a
mutable string class (it had an inefficient one, but this was removed
in 3.0). Are there any libraries outside the core that offer this?

Hi,

you could use a list of characters. It would not be difficult to
implement this as a class with all fancy methods like startswith() ...

Thomas
 
G

greg

Ben said:
My point was rather meant to imply that
subclassing the built-in (immutable) string types was the best way to
usefully get all their functionality

However, it would be difficult to do that without changing
all C code that deals with strings, including that in extension
modules.

That's because the existing string type stores the characters
in the string object itself. A mutable variant would have to
contain a pointer to a resizable memory block, and therefore
couldn't be used as a drop-in replacement by existing C
code that expects a string.
 
M

Mark Lawrence

Neil said:
casebash:


I wrote a gap buffer implementation for Python 2.5 allowing
character, unicode character and integer elements.

http://code.google.com/p/gapbuffer/

Its not seen much use or any maintenance so is unlikely to work with
Python 3.x.

Neil

I tried this as a learning exercise and found slicing doesn't work
correctly.

import gapbuffer
print gapbuffer.GapBuffer(range(10))[:]
GapBuffer('i')]

If my sleuthing is correct the problem is with these lines

ilow *= self->itemSize;
ihigh *= self->itemSize;

in GapBuffer_slice being computed before ilow and ihigh are compared to
anything.

Python 2.6.2 32 bit Windows.
 
N

Neil Hodgson

Mark Lawrence:
If my sleuthing is correct the problem is with these lines

ilow *= self->itemSize;
ihigh *= self->itemSize;

in GapBuffer_slice being computed before ilow and ihigh are compared to
anything.

This particular bug was because ihigh is the maximum 32 bit integer
2147483647 so multiplying it by the integer item size (4) caused
overflow. Adding an extra check fixes this:
if (ihigh > self->lengthBody / self->itemSize)
ihigh = self->lengthBody / self->itemSize;

Committed a new version 1.02 and new downloads are available from
Google code.
http://code.google.com/p/gapbuffer/downloads/list

Neil
 
J

Jack Diederich

Hi,

I have searched this list and found out that Python doesn't have a
mutable string class (it had an inefficient one, but this was removed
in 3.0). Are there any libraries outside the core that offer this?

It depends on how mutable you want your strings. There have been
several patches proposed on python-dev over the years that do things
like lazy concatenation and ropes. They were always shot down because
the implementation or the semantics were very complicated.

-Jack
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top