Discussion:
Python Dialogs
(too old to reply)
Stefan Ram
2024-05-02 14:23:19 UTC
Permalink
Me (indented by 2) and the chatbot (flush left). Lines lengths > 72!

Assume you have an expression "s.replace('a','b').replace('c','d').
replace('e','f').replace('g','h')". Its value is a string which
is the value of s, but with "a" replaced by "b", "c" replaced by
"d", "e" replaced by "f" and "g" replaced by "h". How to modify
this expression, so that "a", "c", "e", and "g", respectively,
are replaced only if they are words (but not parts of words)?

import re

s.replace(r"\ba\b", "b").replace(r"\bc\b", "d").replace(r"\be\b", "f").replace(r"\bg\b", "h")

No. "replace" will not use regular expressions, even if you write
"import re".

import re

s = re.sub(r"\ba\b", "b", s)
s = re.sub(r"\bc\b", "d", s)
s = re.sub(r"\be\b", "f", s)
s = re.sub(r"\bg\b", "h", s)

Now you give me a sequence of four statements, but I need an
expression!

s = re.sub(r"\ba\b", "b", re.sub(r"\bc\b", "d", re.sub(r"\be\b", "f", re.sub(r"\bg\b", "h", s))))

Yeah, that's an expression, but it's kind of nested I guess.
Can you write another expression with less nesting?

s = re.sub(r"\ba\b|\bc\b|\be\b|\bg\b", lambda x: {"a": "b", "c": "d", "e": "f", "g": "h"}[x.group()], s)

Nice idea! Only drawback is that to add another word, you gotta
modify in two places.

replacements = {"a": "b", "c": "d", "e": "f", "g": "h"}
s = re.sub(r"\b(" + "|".join(replacements.keys()) + r")\b", lambda x: replacements[x.group()], s)

PS: This is for the script I use to send posts. So if you do not
see "trying to" (5 letters: "t-r-y-n-a") here, it kind of worked!
Loris Bennett
2024-05-02 14:34:38 UTC
Permalink
Post by Stefan Ram
Me (indented by 2) and the chatbot (flush left). Lines lengths > 72!
Is there a name for this kind of indentation, i.e. the stuff you are
writing not being flush left? It is sort of contrary to
what I think of as "normal" indentation. You seem to use it in all your
postings, too, which hurts my brain, but I guess that's my problem :-)

[snip (40 lines)]
--
This signature is currently under constuction.
Peter J. Holzer
2024-05-04 20:00:02 UTC
Permalink
Post by Loris Bennett
Post by Stefan Ram
Me (indented by 2) and the chatbot (flush left). Lines lengths > 72!
Is there a name for this kind of indentation, i.e. the stuff you are
writing not being flush left?
Ramism.
Post by Loris Bennett
It is sort of contrary to what I think of as "normal" indentation.
Stefan is well known for doing everything contrary to normal convention.

hp
--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | ***@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"
jak
2024-05-06 10:47:43 UTC
Permalink
Post by Loris Bennett
Post by Stefan Ram
Me (indented by 2) and the chatbot (flush left). Lines lengths > 72!
Is there a name for this kind of indentation, i.e. the stuff you are
writing not being flush left? It is sort of contrary to
what I think of as "normal" indentation. You seem to use it in all your
postings, too, which hurts my brain, but I guess that's my problem :-)
[snip (40 lines)]
It's not just your problem. When the texts are particularly long and
complex, I happen to use Google-Translator. It makes bad translations if
the lines are interrupted by the newline, so I wrote a tool dedicated to
Stefan and to all those who indent like him. This tool takes the text
from the clipboard, removes all the superfluous spaces, combines all the
lines in one and puts the result back into the clipboard. :-D
Stefan Ram
2024-05-06 11:43:01 UTC
Permalink
Post by jak
complex, I happen to use Google-Translator. It makes bad translations if
the lines are interrupted by the newline, so I wrote a tool dedicated to
Stefan and to all those who indent like him. This tool takes the text
from the clipboard, removes all the superfluous spaces, combines all the
lines in one and puts the result back into the clipboard. :-D
The game-changer is using "Content-Type: text/plain" instead
of "Content-Type: text/plain; format=flowed", because some
translation services are gonna interpret line breaks as
paragraph endings! But extra spaces like this should
just get ignored by most translation services.

We usually set off quotes, like code snippets, by indenting them.
Well, that won't fly in Python because indentation actually affects
the code's meaning. That's why in Python newsgroups, we got to do the
opposite - indent the surrounding text to make the quotes stand out!
Stefan Ram
2024-05-06 13:07:55 UTC
Permalink
Post by Stefan Ram
translation services are gonna interpret line breaks as
I just beefed up my posting program to replace "gonna".

Now I won't come across like some street thug, but rather
as a respectable member of human society!

# replace complete words
replacements =\
{ rb'kind of': b'kind of',
rb'trying to': b'trying to',
rb'got to': b'got to',
rb'gonna': b'going to',
}
lines =\
[ re.sub( rb"\b(" + b"|".join( replacements.keys() ) + rb")\b",
lambda x: replacements[ x.group() ], line )
if len( line )and line[ 0 ]not in b'>|' else line for line in lines ]
jak
2024-05-06 14:15:41 UTC
Permalink
Post by Stefan Ram
Post by Stefan Ram
translation services are gonna interpret line breaks as
I just beefed up my posting program to replace "gonna".
Now I won't come across like some street thug, but rather
as a respectable member of human society!
# replace complete words
replacements =\
{ rb'kind of': b'kind of',
rb'trying to': b'trying to',
rb'got to': b'got to',
rb'gonna': b'going to',
}
lines =\
[ re.sub( rb"\b(" + b"|".join( replacements.keys() ) + rb")\b",
lambda x: replacements[ x.group() ], line )
if len( line )and line[ 0 ]not in b'>|' else line for line in lines ]
I present to you the brutality to which your posts are subjected:

for(d = s = pCLipb; *s != TEXT('\0'); s++)
{
if(d == pCLipb)
{
if(*s != TEXT(' ') && *s != TEXT('\n') && *s != TEXT('\r'))
*d++ = *s;
}
else if(*s == TEXT(' ') || *s == TEXT('\n') || *s == TEXT('\r'))
{
if(d[-1] != TEXT(' '))
*d++ = TEXT(' ');
}
else
*d++ = *s;
}
*d = TEXT('\0');
Lawrence D'Oliveiro
2024-05-06 21:42:52 UTC
Permalink
Post by Stefan Ram
Well, that won't fly in Python because indentation actually affects
the code's meaning.
This is another reason why I use “#end” comments. Even if the indentation
gets screwed up, it is still possible to reconstruct what it means.
Otherwise, the code would just become irretrievable gibberish.

Chris Angelico
2024-05-06 17:48:19 UTC
Permalink
Post by jak
Post by Loris Bennett
Post by Stefan Ram
Me (indented by 2) and the chatbot (flush left). Lines lengths > 72!
Is there a name for this kind of indentation, i.e. the stuff you are
writing not being flush left? It is sort of contrary to
what I think of as "normal" indentation. You seem to use it in all your
postings, too, which hurts my brain, but I guess that's my problem :-)
[snip (40 lines)]
It's not just your problem. When the texts are particularly long and
complex, I happen to use Google-Translator. It makes bad translations if
the lines are interrupted by the newline, so I wrote a tool dedicated to
Stefan and to all those who indent like him. This tool takes the text
from the clipboard, removes all the superfluous spaces, combines all the
lines in one and puts the result back into the clipboard. :-D
Fun fact: His posts are completely irrelevant to people who follow the
mailing list. Due to a dispute over permissions, his posts are blocked
at the gateway. So all of us folks who use the mailing list never need
to see the wonky indentation.

ChrisA
Lawrence D'Oliveiro
2024-05-03 02:02:57 UTC
Permalink
Post by Stefan Ram
Assume you have an expression "s.replace('a','b').replace('c','d').
replace('e','f').replace('g','h')". Its value is a string which
is the value of s, but with "a" replaced by "b", "c" replaced by
"d", "e" replaced by "f" and "g" replaced by "h". How to modify
this expression, so that "a", "c", "e", and "g", respectively,
are replaced only if they are words (but not parts of words)?
import re

replacements = (("a", "b"), ("c", "d"), ("e", "f"), ("g", "h"))

text = "this be a test g eg"

"".join \
(
repl.get(s, s)
for repl in (dict(replacements),)
for s in
re.split("\\b(" + "|".join(re.escape(s[0]) for s in replacements) + ")\\b", text)
)

result:

'this be b test h eg'
Alan Bawden
2024-05-03 07:16:35 UTC
Permalink
Post by Stefan Ram
Assume you have an expression "s.replace('a','b').replace('c','d').
replace('e','f').replace('g','h')". Its value is a string which
is the value of s, but with "a" replaced by "b", "c" replaced by
"d", "e" replaced by "f" and "g" replaced by "h". How to modify
this expression, so that "a", "c", "e", and "g", respectively,
are replaced only if they are words (but not parts of words)?
import re

replacements = (("a", "b"), ("c", "d"), ("e", "f"), ("g", "h"))

text = "this be a test g eg"

"".join \
(
repl.get(s, s)
for repl in (dict(replacements),)
for s in
re.split("\\b(" + "|".join(re.escape(s[0]) for s in replacements) + ")\\b", text)
)

How about just:

repl = {
"a" : "b",
"c" : "d",
"e" : "f",
"g" : "h",
}

"".join(repl.get(s, s) for s in re.split(r"\b", text))

- Alan
Loading...