From: John Posner on
On Tue, 05 Jan 2010 15:08:04 -0500, Dave McCormick <mackrackit(a)gmail.com>
wrote:

>>
>> It's certainly a mistake to use the expression "str(rList).split()".
>> Using str() to convert the list "rList" into a string creates a mess
>> that includes square-bracket characters. Did this actually work for you?

> It sort of worked. With one color file it seemed fine but after I
> posted I added another color file and things fell apart.
> Now with the above fixes it works with three colors from three files.
> When the list are printed to the shell the list look like this:
> redList ['red', 'dog', 'apple', '#']
> blueList ['blue', 'ball', 'berry']
> greenList ['green', 'grass', 'do']
>
> But another problem is noticed. It does not matter if the list is built
> in code or from a file.
> If dog is entered, "do" will be green with the "g" being red. Back to
> the drawing board.....

It sounds like the program is doing exactly what you TOLD it to do (which
might not be what you WANT it to do):

1. In an earlier pass on the text, color the string "dog" red.
2. In a later pass, color the string "do" green.

You need to decide what you WANT to happen if one word to be colored is a
substring of another word to be colored differently. Or maybe you want to
outlaw such situations. After making that decision, you can start to think
about how to write the appropriate code.

Best,
John
From: Dave McCormick on


John Posner wrote:
> On Tue, 05 Jan 2010 15:08:04 -0500, Dave McCormick
> <mackrackit(a)gmail.com> wrote:
>
> It sounds like the program is doing exactly what you TOLD it to do
> (which might not be what you WANT it to do):
>
> 1. In an earlier pass on the text, color the string "dog" red.
> 2. In a later pass, color the string "do" green.
>
> You need to decide what you WANT to happen if one word to be colored
> is a substring of another word to be colored differently. Or maybe you
> want to outlaw such situations. After making that decision, you can
> start to think about how to write the appropriate code.
>
> Best,
> John
Darn thing doing what I told it to do... Guess that means I did
something right :)
But it is not what I am wanting.
I first thought to make it look for a space but that would not work when
a single character like "#" is to be colored if there is a "string" of
them. Or if all of the characters between quotes are to be colored.

I always did like puzzles!

Dave
From: Neil Cerutti on
On 2010-01-05, John Posner <jjposner(a)optimum.net> wrote:
> 2. It's probably not the best idea to use a single variable
> (you use "file") to do double-duty: to hold the name of a
> file, and to hold the open-file object returned by the open()
> function. It's perfectly legal, but it hides information that
> might be useful when you're debugging a program. This is
> better:
>
> fname = 'red.txt'
> inpf = open(fname, "r")

Alternatively:

>>> infile = open("red.txt", "r")
>>> infile.name
'red.txt'

--
Neil Cerutti
From: John Posner on
On Tue, 05 Jan 2010 16:54:44 -0500, Dave McCormick <mackrackit(a)gmail.com>
wrote:

> But it is not what I am wanting. I first thought to make it look for a
> space but that would not work when a single character like "#" is to be
> colored if there is a "string" of them. Or if all of the characters
> between quotes are to be colored.

Regular expressions are good at handling searches like:

* all the characters between quotes
* the two-character string "do", but only if it's a complete word

-John
From: MRAB on
Dave McCormick wrote:
>
>
> On Wed, Jan 6, 2010 at 9:18 AM, John Posner <jjposner(a)optimum.net
> <mailto:jjposner(a)optimum.net>> wrote:
>
> On Tue, 05 Jan 2010 16:54:44 -0500, Dave McCormick
> <mackrackit(a)gmail.com <mailto:mackrackit(a)gmail.com>> wrote:
>
> But it is not what I am wanting. I first thought to make it look
> for a space but that would not work when a single character like
> "#" is to be colored if there is a "string" of them. Or if all
> of the characters between quotes are to be colored.
>
>
> Regular expressions are good at handling searches like:
>
> * all the characters between quotes
> * the two-character string "do", but only if it's a complete word
>
> -John
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
> I need another hint...
>
> Been doing some reading and playing and it looks like
> r'\bxxx\b'
> is what I need. But I can not figure out how to pass a variable between
> \b___\b
> If the word in question is between the "\b \b" and in the list then it
> works like I want it to.
> The below does not work.
>
> greenList_regexp = "|".join(greenList)
> for matchobj in re.finditer(r'\bgreenList_regexp\b', complete_text):
> start,end = matchobj.span()
>
The regex r'\bgreenList_regexp\b' will match the string
'greenList_regexp' if it's a whole word.

What you mean is "any of these words, provided that they're whole
words". You'll need to group the alternatives within "(?:...)", like
this:

r'\b(?:' + greenList_regexp + ')\b'