Re: Mutable Strings - Any libraries that offer this?

2009-07-26 Thread Mark Lawrence

Neil Hodgson wrote:

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


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.

--
Kindest regards.

Mark Lawrence.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-26 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-26 Thread Jack Diederich
On Mon, Jul 20, 2009 at 5:57 AM, casebashwalkr...@gmail.com wrote:
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-22 Thread casebash
Thanks all for your advice. I'm not actually going to use the mutable
string right at the moment, but I thought I might in the future and I
was just curious if it existed. I suppose a list of characters is
close enough for most purposes.

On Jul 22, 10:28 am, greg g...@cosc.canterbury.ac.nz wrote:
 Ben Finney wrote:
  My point was rather meant to imply that
  subclassing the built-in (immutable)stringtypes 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 existingstringtype stores the characters
 in thestringobject itself. Amutablevariant 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 astring.

 --
 Greg

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-21 Thread Thomas Guettler
casebash schrieb:
 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

-- 
Thomas Guettler, http://www.thomas-guettler.de/
E-Mail: guettli (*) thomas-guettler + de
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-21 Thread Ben Finney
John Machin sjmac...@lexicon.net writes:

 OK, I'll bite: where does the Python 3.x bytearray type

I wasn't previously aware of it, thanks for bringing it to my attention
URL:http://docs.python.org/3.1/library/stdtypes.html#bytes-methods.

 fit into your taxonomy? At first glance it appears to be mutable and
 have a fair swag of functionality.

Yes. I'd classify it as a mutable array of bytes :-) but, as such, not
to be used as a text string.

-- 
 \“Science doesn't work by vote and it doesn't work by |
  `\authority.” —Richard Dawkins, _Big Mistake_ (The Guardian, |
_o__)  2006-12-27) |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-21 Thread Ben Finney
Steven D'Aprano st...@remove-this-cybersource.com.au writes:

 On Mon, 20 Jul 2009 21:08:22 +1000, Ben Finney wrote:
  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.

Agreed, I wouldn't advise that. 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, rather than re-implementing most
of it.

-- 
 \ “I went to a restaurant that serves ‘breakfast at any time’. So |
  `\I ordered French Toast during the Renaissance.” —Steven Wright |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-21 Thread Scott David Daniels

Steven D'Aprano wrote:

On Mon, 20 Jul 2009 21:08:22 +1000, Ben Finney wrote:

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.


The general problem is that whether strings are mutable or not is an
early language design decision, and few languages provide both.
Mutable strings need lots of data copying to be safe passing args to
unknown functions; immutable strings need lots of copying for ncremental
changes.  The rope is a great idea for some cases.

I'd argue Python works better with immutable strings, because Python is
too slow at per-character operations to be running up and down strings
a character at a time, changing here and there.  So it becomes more
natural to deal with strings as chunks to pass around, and it is nice
not to have to copy the strings when doing that passing around.

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-21 Thread greg

Ben Finney wrote:

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.

--
Greg
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-20 Thread Carl Banks
On Jul 20, 2:57 am, casebash walkr...@gmail.com wrote:
 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-20 Thread Ben Finney
casebash walkr...@gmail.com writes:

 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?

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.

-- 
 \ “As scarce as truth is, the supply has always been in excess of |
  `\   the demand.” —Josh Billings |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-20 Thread John Machin
On Jul 20, 9:08 pm, Ben Finney ben+pyt...@benfinney.id.au wrote:
 casebash walkr...@gmail.com writes:
  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?

 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-20 Thread 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
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Mutable Strings - Any libraries that offer this?

2009-07-20 Thread Steven D'Aprano
On Mon, 20 Jul 2009 21:08:22 +1000, Ben Finney wrote:

 casebash walkr...@gmail.com writes:
 
 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?
 
 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)



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list