Editable strings anyone?

John Machin sjmachin at lexicon.net
Sun Feb 17 16:21:52 EST 2002


Paul Rubin <phr-n2002a at nightsong.com> wrote in message news:<7x4rkgw79y.fsf at ruckus.brouhaha.com>...
> martin at v.loewis.de (Martin v. Loewis) writes:
> > > I'm wondering how difficult it would be to add an editable (mutable)
> > > string class to Python 2.2 implemented in C under the new type/class
> > > unification scheme.
> > 
> > There is already an efficient mutable vector type, the array type,
> > which can be used to implement both mutable byte strings and mutable
> > character strings.

The array module is one of the included batteries that needs a
publicity agent. It is good value for:

(a) vectors of all-same-type numerics where the power of Numpy is not
required. Existing code using lists can often be simply tweaked to use
arrays when the vector is created -- myvector = array.array('l', [0,
1, 42]) instead of myvector = [0, 1, 42] -- and everything else just
works. Like how's this for hot-pluggable poly-whatsit:

>>> import array, bisect
>>> v = array.array('l', [10, 20, 30])
>>> v
array('l', [10, 20, 30])
>>> bisect.insort_left(v, 15)
>>> v
array('l', [10, 15, 20, 30])
>>>

(b) mutable strings are a godsend if you have to create a file of the
type that seems to be favoured by government departments:
fixed-column-size records (often several hundreds of bytes wide).

> 
> Very interesting.  I was about to answer that:
> 
>   1) There are functions to insert and delete single values into the array
>      but not multi-element slices.  
> 
>   2) I don't think the existing string functions work on byte arrays.
>      In particular, I don't think you can search for a regular expression in one.
> 
> But it turns out that both operations are supported!  I discovered
> this by looking in the arraymodule.c source to see how the buffer
> interface was implemented, and then experimenting.  The docs for the
> array module gave no clue at all that these operations were supported.
> 

Second sentence of the documentation gives a bit of a clue on your (1)
above:
"""
Arrays are sequence types and behave very much like lists, except that
the type of objects stored in them is constrained.
"""

Functions in string module may have worked on 'c' arrays in 1.5.2 but
now string.find(strg, query) is implemented as a string method --
strg.find(query)

It's a pleasant surprise that the re module supports searching etc on
'c' arrays.

> 
> Anyway, it looks like the array module may do what I need.

You'll find one nuisance when working with 'c' arrays; it doesn't
support coercing an ordinary string to an array -- you have to do it
yourself. For example:

x = array.array('c', 'abc')
x = x + 'def'

doesn't work. You have to do 

x = x  + array.array('c', 'def')

>  So I guess I'll submit a doc bug saying the above items should be described.

Certainly the re docs should be a tad more explicit about what a
"string" can be, or what behaviour(s) it must provide.



More information about the Python-list mailing list