Rocksolid Light

Welcome to Rocksolid Light

mail  files  register  newsreader  groups  login

Message-ID:  

No line available at 300 baud.


devel / comp.lang.python / Re: Passing info to function used in re.sub

SubjectAuthor
o Passing info to function used in re.subPeter J. Holzer

1
Re: Passing info to function used in re.sub

<mailman.279.1693854010.23016.python-list@python.org>

  copy mid

https://news.novabbs.org/devel/article-flat.php?id=23961&group=comp.lang.python#23961

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!fu-berlin.de!uni-berlin.de!not-for-mail
From: hjp-python@hjp.at (Peter J. Holzer)
Newsgroups: comp.lang.python
Subject: Re: Passing info to function used in re.sub
Date: Mon, 4 Sep 2023 21:00:02 +0200
Lines: 81
Message-ID: <mailman.279.1693854010.23016.python-list@python.org>
References: <6943DF5C-BBD7-40FB-872E-87F111D52059@mostrom.pp.se>
<20230904190002.2vh7mcqi6xkb6rvu@hjp.at>
Mime-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha512;
protocol="application/pgp-signature"; boundary="geliur6iwy73cxcc"
X-Trace: news.uni-berlin.de mWwEyP6JOIv0BewZ5QvTzAfqs2nGs2qOLZiDzI579EyQ==
Cancel-Lock: sha1:QkX+EV75bV4LH6aqDQUmCi949Fc= sha256:ho82IZwx19g5snGC6Rr8OasXrKd2uoFOt052bsenm1s=
Return-Path: <hjp-python@hjp.at>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.001
X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'this:': 0.03; 'def': 0.04;
'content-type:multipart/signed': 0.05; 'content-type:application
/pgp-signature': 0.09; 'filename:fname piece:asc': 0.09;
'filename:fname piece:signature': 0.09;
'filename:fname:signature.asc': 0.09; 'import': 0.15; '"creative':
0.16; '+0200,': 0.16; '__/': 0.16; 'challenge!"': 0.16; 'from:addr
:hjp-python': 0.16; 'from:addr:hjp.at': 0.16; 'from:name:peter j.
holzer': 0.16; 'hjp@hjp.at': 0.16; 'holzer': 0.16; 'outer': 0.16;
'reality.': 0.16; 'stross,': 0.16; 'subject:used': 0.16; 'url-
ip:212.17.106/24': 0.16; 'url-ip:212.17/16': 0.16; 'url:hjp':
0.16; '|_|_)': 0.16; 'wrote:': 0.16; 'uses': 0.19; 'to:addr
:python-list': 0.20; 'written': 0.22; "i've": 0.22; "what's":
0.22; 'code': 0.23; 'creating': 0.27; 'local': 0.27; 'function':
0.27; 'sense': 0.28; 'assume': 0.32; 'python-list': 0.32; 'but':
0.32; 'there': 0.33; 'header:In-Reply-To:1': 0.34; 'currently':
0.37; 'using': 0.37; 'file': 0.38; 'way': 0.38; 'could': 0.38;
'text': 0.39; 'this,': 0.39; 'use': 0.39; 'something': 0.40;
'want': 0.40; 'received:212': 0.62; 'here': 0.62; 'skip:r 20':
0.64; 'look': 0.65; 'received:userid': 0.66; 'right': 0.68; 'url-
ip:212/8': 0.69; 'global': 0.73; 'charset:iso-8859-1': 0.73;
'capture': 0.84; 'received:at': 0.84
Mail-Followup-To: python-list@python.org
Content-Disposition: inline
In-Reply-To: <6943DF5C-BBD7-40FB-872E-87F111D52059@mostrom.pp.se>
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <20230904190002.2vh7mcqi6xkb6rvu@hjp.at>
X-Mailman-Original-References: <6943DF5C-BBD7-40FB-872E-87F111D52059@mostrom.pp.se>
 by: Peter J. Holzer - Mon, 4 Sep 2023 19:00 UTC
Attachments: signature.asc (application/pgp-signature)

On 2023-09-03 18:10:29 +0200, Jan Erik Moström via Python-list wrote:
> I want to replace some text using a regex-pattern, but before creating
> replacement text I need to some file checking/copying etc. My code
> right now look something like this:
>
> def fix_stuff(m):
> # Do various things that involves for info
> # that what's available in m
> replacement_text = m.group(1) + global_var1 + global_var2
> return replacement_text
>
> and the call comes here
>
> global_var1 = "bla bla"
> global_var2 = "pff"
>
> new_text = re.sub(im_pattern,fix_stuff,md_text)
>
>
> The "problem" is that I've currently written some code that works but
> it uses global variables ... and I don't like global variables. I
> assume there is a better way to write this, but how?

If you use fix_stuff only inside one other function, you could make it
local to that function so that it will capture the local variables of
the outer function:

import re

def demo():

local_var1 = "bla bla"
local_var2 = "pff"

def fix_stuff(m):
# Do various things that involves for info
# that what's available in m
replacement_text = m.group(1) + local_var1 + local_var2
return replacement_text

for md_text in ( "aardvark", "barbapapa", "ba ba ba ba barbara ann"):
new_text = re.sub(r"(a+).*?(b+)", fix_stuff, md_text)
print(md_text, new_text)

demo()

hp

--
_ | Peter J. Holzer | Story must make more sense than reality.
|_|_) | |
| | | hjp@hjp.at | -- Charles Stross, "Creative writing
__/ | http://www.hjp.at/ | challenge!"

Attachments: signature.asc (application/pgp-signature)

devel / comp.lang.python / Re: Passing info to function used in re.sub

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor