[ANN] istring interpolated strings release 1.0

Steve Holden sholden at holdenweb.com
Fri Jan 25 09:04:09 EST 2002


"Steven D. Arnold" <stevena at neosynapse.net> wrote in message
news:mailman.1011937057.30382.python-list at python.org...
> Neosynapse is pleased to announce the release of the istring
> interpolated string class.  This class allows Python programmers to
> interpolate variables directly in strings, providing a level of
> readability and convenience that can be more difficult to attain with
> the `%' notation.  However, since istrings descend directly from
> full-fledged Python strings, you still keep all the flexibility of `%'
> notation should you wish to use it, and you get all the other features
> you expect from a Python string, including slicing and usability in a
> regular expression.
>
> As a very simple example, istring allows you to do things like this:
>
> >>> j = 3
> >>> istring("The value is $j")
> "The value is 3"
>
> The istring class understands and works with lists, dictionaries,
> functions and class methods.
>
> istring may be downloaded at:
>
>     http://www.neosynapse.net/interpolated_string.htm
>
>From that page:
'''
Ask yourself which query is clearer:

(1) db.query("""SELECT    foo, bar
                FROM      %s
                WHERE     rsrc_id = %s
                AND       name = '%s'" % (table, rsrc_id, name))
Or:

(2) qry = istring("""SELECT   foo, bar
                     FROM     $table
                     WHERE    rsrc_id = $rsrc_id
                     AND      name = $.name""")
    db.query(qry)
'''

As far as I'm concerned, (1) is rather clearer, since it isn't apparent to
me that in (2) the

    AND    name = $.name

portion will become'

    AND name = 'namevalue'

Unless, of course, that's a typo. The example goes on to say:
'''
However, since istring descends from the regular Python string class, you
can use % notation intermixed with istring interpolation:

>>> m = "abc"
>>> n = "def"
>>> test_dict = {'foo': 'bar', 'yak': 'lmnop'}
>>> istring("value=%(foo)s$m,%(yak)s$n" % test_dict)
"value=barabc,lmnopdef"
'''

Unfortunately this example needs rewriting, as you are constructing an
istring from the result of a formatting operation rather than formatting on
an istring.

> The istring class was developed with SQL in mind, but it may be used
> for a wide variety of purposes.

Also bear in mind that, which it is necessary to use string interpolation
for certain portions of statements (such as the table in your example
above), it is actually _much_ more efficient to use the DB API's parameter
substitution mechanisms to parameterize query values.
> [...]
Despite these minor misgivings, congratulations on putting something usable
and worthwhile out for Python users.

regards
 Steve
--
Consulting, training, speaking: http://www.holdenweb.com/
Python Web Programming: http://pydish.holdenweb.com/pwp/








More information about the Python-list mailing list