• Python Dialogs

    From Stefan Ram@21:1/5 to All on Thu May 2 14:23:19 2024
    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Loris Bennett@21:1/5 to Stefan Ram on Thu May 2 16:34:38 2024
    ram@zedat.fu-berlin.de (Stefan Ram) writes:

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to All on Fri May 3 02:02:57 2024
    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'

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Alan Bawden@21:1/5 to Lawrence D'Oliveiro on Fri May 3 03:16:35 2024
    Lawrence D'Oliveiro <ldo@nz.invalid> writes:

    > 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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Peter J. Holzer@21:1/5 to Loris Bennett via Python-list on Sat May 4 22:00:02 2024
    On 2024-05-02 16:34:38 +0200, Loris Bennett via Python-list wrote:
    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    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.

    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@hjp.at | -- Charles Stross, "Creative writing
    __/ | http://www.hjp.at/ | challenge!"

    -----BEGIN PGP SIGNATURE-----

    iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmY2k7wACgkQ8g5IURL+ KF08vw/+MtqPoIJyOQG4K2BugmC6pXxtW4Leee8oILXd+uXzwi59ZCOAkjXm68nX kjkW+fGvv9ywZUAark4NyjFus/57HYxRTR+RQR0f0NXh0nkc+2fKj7ETIxSLbgsQ om6VFl1JVBa/fnj5KgEo5DqFV4C35pn9VWCSQbaZe2CA5k8ZkYTqEzoxMgx8rYZK dXsI2bCwhNDURioZfxDyf2QvFkB2jtXnK93jxjXCSFOKTF3On8HJQlQUqAek5uxb PhYgFcNiqOjmcCK4RTFMpGzn5WUt4pRxVgGhS1YLi72N8SMuMG7lu3gnm0DD8sqU JB14Ze0kewfRmfkAEs81P9/TFdoclsbBJQck8okbj9LY1gnKnirXUC9tdq5Pg4xo jGUU0QffVc6LFtFCUNBP5nc6eySu+rpkzH3++h+evdAPIwteF78b7H193aLHMRai Dl0silG/pgUO4zKa7qLFiEzCH7v8nOyNArkpVw8kQ190krkmPqjklI8pyqkGqInN o+I5e0rpw5cDMtCzbx8ZFZvtVMBWQJyqZ9fN6xPnRMr4ZpDeZFKu7Y6mTm5QKafG P8ypQRU3ypWihYWFYzofltBGvzzTAQAOibu5Cw7J+e/2gGCh/7+0gcYihn3NV37k 13KtzesJgHzJJZE6LVGLYpudTUzY0qMy56sCRxL
  • From jak@21:1/5 to All on Mon May 6 12:47:43 2024
    Loris Bennett ha scritto:
    ram@zedat.fu-berlin.de (Stefan Ram) writes:

    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to jak on Mon May 6 11:43:01 2024
    jak <nospam@please.ty> wrote or quoted:
    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!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Mon May 6 13:07:55 2024
    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    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 ]

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From jak@21:1/5 to All on Mon May 6 16:15:41 2024
    Stefan Ram ha scritto:
    ram@zedat.fu-berlin.de (Stefan Ram) wrote or quoted:
    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');

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Lawrence D'Oliveiro@21:1/5 to Stefan Ram on Mon May 6 21:42:52 2024
    On 6 May 2024 11:43:01 GMT, Stefan Ram wrote:

    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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)