OT: Python % string processing

This might be a little off the wxWidgets topic, but this is something I
need to generate the strings that I display in my wxWidgets GUI, so ...

What I would like to do is to be able to use the % string formatting but
have 'conditional separators' in the text. I.E. the separators would
only appear if there was text on both sides of them.

For example, consider formatting a person's name. The name consists of
first, middle and last parts. Assume the data is supplied in a
dictionary:

name = {'first': 'Mark', 'middle' : 'E.', 'last': 'Erbaugh'}

I can create an string of the name using:

'%(first)s %(middle)s %(last)s' % name ==> 'Mark E. Erbaugh'

Using the same data, I can create a name in the last, first format:

'%(last)s, %(first)s %(middle)s' % name ==> 'Erbaugh, Mark E.'

Suppose I have a person with no middle name. The resulting string would
have two spaces between the first and last name. Similarly, if the
person only has one name and it was stored in the 'last' field, it would
print with two leading spaces. I'd like the routine to be able to get
rid of these extraneous spaces.

Another use for this would be in formatting of mailing addresses. Some
addresses may have two address lines, some only the first.

addr = {'line1' : '123 XYZ Street', 'line2' : 'Apt 12'}

'%(line1)s\n%(line2)s' % addr

If line2 is blank it would be nice to eliminate the separating '\n', so
there would be no blank line in the address.

Mark

This might help (look at coalesce() and none_if()):

  http://cvs.savannah.gnu.org/viewvc/gnumed/gnumed/client/pycommon/gmTools.py?root=gnumed&view=markup

The test cases further below give some idea of how to use it.

Again, any chance I can have a look at your code (or is it
proprietary ?)

Karsten

···

On Thu, Dec 06, 2007 at 08:59:25AM -0500, Mark Erbaugh wrote:

This might be a little off the wxWidgets topic, but this is something I
need to generate the strings that I display in my wxWidgets GUI, so ...

What I would like to do is to be able to use the % string formatting but
have 'conditional separators' in the text. I.E. the separators would
only appear if there was text on both sides of them.

--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

Mark Erbaugh escribió:

This might be a little off the wxWidgets topic, but this is something I
need to generate the strings that I display in my wxWidgets GUI, so ...

What I would like to do is to be able to use the % string formatting but
have 'conditional separators' in the text. I.E. the separators would
only appear if there was text on both sides of them.

For example, consider formatting a person's name. The name consists of
first, middle and last parts. Assume the data is supplied in a
dictionary:

name = {'first': 'Mark', 'middle' : 'E.', 'last': 'Erbaugh'}

I can create an string of the name using:

'%(first)s %(middle)s %(last)s' % name ==> 'Mark E. Erbaugh'

Using the same data, I can create a name in the last, first format:

'%(last)s, %(first)s %(middle)s' % name ==> 'Erbaugh, Mark E.'

Suppose I have a person with no middle name. The resulting string would
have two spaces between the first and last name. Similarly, if the
person only has one name and it was stored in the 'last' field, it would
print with two leading spaces. I'd like the routine to be able to get
rid of these extraneous spaces.

Another use for this would be in formatting of mailing addresses. Some
addresses may have two address lines, some only the first.

addr = {'line1' : '123 XYZ Street', 'line2' : 'Apt 12'}

'%(line1)s\n%(line2)s' % addr

If line2 is blank it would be nice to eliminate the separating '\n', so
there would be no blank line in the address.

Mark
  
A simple approach for this is to do:
result = ' '.join(result.split())

If you render your data into HtmlWindow, perhaps is of interest
using Roman Rolinsky's Custom HTML filter for SSI-like directives <http://news.gmane.org/find-root.php?message_id=<1182369608.4714.135.camel%40hartman.femagsoft.com>&gt;: http://thread.gmane.org/gmane.comp.python.wxpython/49051
Haven't used it myself.

-- jonhattAn :wink:

···

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org

> This might help (look at coalesce() and none_if()):
>
> http://cvs.savannah.gnu.org/viewvc/gnumed/gnumed/client/pycommon/gmTools.py?root=gnumed&view=markup
>
> The test cases further below give some idea of how to use it.
Thanks for the posting. I remember coalesce and nullif from my SQL
Server days <g>.

Oh, PostgreSQL has them, too :wink:

There really isn't any code (yet).

Ah, OK. So I am wondering whether you might be interested in
having a look at GNUmed (http://wiki.gnumed.de) which is an
EMR (initially targetted at GPs - which isn't exactly the
same as ER, however) written with
Python/wxPython/PostgreSQL. It has classes like cPerson,
cPatient, cAddress, cAllergy, ... which make immediate sense
to a medical person.

I would like this to not have extra spaces if a needed piece of the
address was blank ('')

Which can be achieved with gmTools.coalesce() (it's one of
the things I use it for).

Karsten

···

On Thu, Dec 06, 2007 at 09:35:30AM -0500, Mark Erbaugh wrote:
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

Mark Erbaugh escribió:
> This might be a little off the wxWidgets topic, but this is something I
> need to generate the strings that I display in my wxWidgets GUI, so ...
>
> What I would like to do is to be able to use the % string formatting but
> have 'conditional separators' in the text. I.E. the separators would
> only appear if there was text on both sides of them.
>
> For example, consider formatting a person's name. The name consists of
> first, middle and last parts. Assume the data is supplied in a
> dictionary:
>
> name = {'first': 'Mark', 'middle' : 'E.', 'last': 'Erbaugh'}
>
> I can create an string of the name using:
>
> '%(first)s %(middle)s %(last)s' % name ==> 'Mark E. Erbaugh'
>
> Using the same data, I can create a name in the last, first format:
>
> '%(last)s, %(first)s %(middle)s' % name ==> 'Erbaugh, Mark E.'
>
> Suppose I have a person with no middle name. The resulting string would
> have two spaces between the first and last name. Similarly, if the
> person only has one name and it was stored in the 'last' field, it would
> print with two leading spaces. I'd like the routine to be able to get
> rid of these extraneous spaces.
>
> Another use for this would be in formatting of mailing addresses. Some
> addresses may have two address lines, some only the first.
>
> addr = {'line1' : '123 XYZ Street', 'line2' : 'Apt 12'}
>
> '%(line1)s\n%(line2)s' % addr
>
> If line2 is blank it would be nice to eliminate the separating '\n', so
> there would be no blank line in the address.
>
> Mark
>

A simple approach for this is to do:
result = ' '.join(result.split())

That's a possibility, but it could eliminate desired spaces (i.e. New
York would become NewYork). Also, it wouldn't eliminate other
extraneous separators such as commas.

If you render your data into HtmlWindow, perhaps is of interest
using Roman Rolinsky's Custom HTML filter for SSI-like directives
<http://news.gmane.org/find-root.php?message_id=<1182369608.4714.135.camel%40hartman.femagsoft.com>&gt;:
http://thread.gmane.org/gmane.comp.python.wxpython/49051
Haven't used it myself.

Right now, I'm not thinking about HTML for this application.

···

On Thu, 2007-12-06 at 14:39 +0000, jonhattan wrote:

> Mark Erbaugh escribió:
> > This might be a little off the wxWidgets topic, but this is something I
> > need to generate the strings that I display in my wxWidgets GUI, so ...
> >
> > What I would like to do is to be able to use the % string formatting but
> > have 'conditional separators' in the text. I.E. the separators would
> > only appear if there was text on both sides of them.
> >
> > For example, consider formatting a person's name. The name consists of
> > first, middle and last parts. Assume the data is supplied in a
> > dictionary:
> >
> > name = {'first': 'Mark', 'middle' : 'E.', 'last': 'Erbaugh'}
> >
> > I can create an string of the name using:
> >
> > '%(first)s %(middle)s %(last)s' % name ==> 'Mark E. Erbaugh'
> >
> > Using the same data, I can create a name in the last, first format:
> >
> > '%(last)s, %(first)s %(middle)s' % name ==> 'Erbaugh, Mark E.'
> >
> > Suppose I have a person with no middle name. The resulting string would
> > have two spaces between the first and last name. Similarly, if the
> > person only has one name and it was stored in the 'last' field, it would
> > print with two leading spaces. I'd like the routine to be able to get
> > rid of these extraneous spaces.
> >
> > Another use for this would be in formatting of mailing addresses. Some
> > addresses may have two address lines, some only the first.
> >
> > addr = {'line1' : '123 XYZ Street', 'line2' : 'Apt 12'}
> >
> > '%(line1)s\n%(line2)s' % addr
> >
> > If line2 is blank it would be nice to eliminate the separating '\n', so
> > there would be no blank line in the address.
> >
> > Mark
> >
>
> A simple approach for this is to do:
> result = ' '.join(result.split())

That's a possibility, but it could eliminate desired spaces (i.e. New
York would become NewYork). Also, it wouldn't eliminate other
extraneous separators such as commas.

The idea is right, but the code example is not.
'\n'.join(addr.values()) will give the results you want. The dict and
interpolation formatting here is a dead end if you're looking to break
out separators - create a sequence of the fields you're separating,
and use join to link them together. If you might have missing or blank
fields in the middle, use filter or a list comp to remove them before
joining.

For example:

'*'.join(['', '', 'a', 'b'])

'**a*b'

'*'.join(filter(None, ['', '', 'a', 'b']))

'a*b'

···

On Dec 6, 2007 11:38 AM, Mark Erbaugh <mark@microenh.com> wrote:

On Thu, 2007-12-06 at 14:39 +0000, jonhattan wrote: