Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Irrationality is the square root of all evil" -- Douglas Hofstadter


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

SubjectAuthor
o Re: Passing info to function used in re.subMRAB

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

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

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=23950&group=comp.lang.python#23950

  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: pyt...@mrabarnett.plus.com (MRAB)
Newsgroups: comp.lang.python
Subject: Re: Passing info to function used in re.sub
Date: Sun, 3 Sep 2023 18:13:10 +0100
Lines: 51
Message-ID: <mailman.269.1693761379.23016.python-list@python.org>
References: <6943DF5C-BBD7-40FB-872E-87F111D52059@mostrom.pp.se>
<a8c7157b-bbb0-0e60-d2b2-4259e971e49c@mrabarnett.plus.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de nM17bXNFvC1f0kuawVO9CgD/p4mw50J/qUJhs64YSZbA==
Cancel-Lock: sha1:dPlZKHPvNrASH1Rai4XzG63B55Y= sha256:h6+35A1Su4yetXcPV25nsEDnmCBmCT8LMppHQAaES7M=
Return-Path: <python@mrabarnett.plus.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=pass
reason="2048-bit key; unprotected key"
header.d=plus.com header.i=@plus.com header.b=ZBpG/C+G;
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.018
X-Spam-Evidence: '*H*': 0.96; '*S*': 0.00; 'this:': 0.03; 'def': 0.04;
'from:addr:python': 0.09; 'received:192.168.1.64': 0.09;
'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16;
'lambda': 0.16; 'message-id:@mrabarnett.plus.com': 0.16;
'received:plus.net': 0.16; 'subject:used': 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;
'function': 0.27; 'header:User-Agent:1': 0.30; 'assume': 0.32;
'python-list': 0.32; 'received:192.168.1': 0.32; 'but': 0.32;
"i'm": 0.33; 'there': 0.33; 'header:In-Reply-To:1': 0.34;
'currently': 0.37; 'using': 0.37; 'received:192.168': 0.37;
'file': 0.38; 'way': 0.38; 'could': 0.38; 'text': 0.39; 'this,':
0.39; 'use': 0.39; 'define': 0.40; 'something': 0.40; 'want':
0.40; 'received:212': 0.62; 'here': 0.62; 'pass': 0.64; 'look':
0.65; 'named': 0.65; 'right': 0.68; 'global': 0.73; 'anonymous':
0.76
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=plus.com; s=042019;
t=1693761192; bh=zvofjJlyZngUdhHhtLwQ8v6rijmB8I9OzrhcsYey3Lg=;
h=Date:Subject:To:References:From:In-Reply-To;
b=ZBpG/C+G+8wqyvUvtgX7JKxWXxZDb9HHtcf1nVSScQDtDFn0g3ris4Po7RNRe6FSH
MnXKTQghz4zSzVupg+T56FdhLsOJ+iW+k5UzKHhPOXeRZAP2IfvpNntlB5bnwG8DJp
WcLuiY74oQdp0GGJy2jf0uD4KmpskWy+ljGaqSeCLbzE6Z3ifdAuDlEY9cKp9OtF80
/sTW80KSsLQcH7GjBZLjM1sRXNRNSrRX+VMX9YH8p9PYS7IODCBw41Jootw+KvG5Jz
GWwAhb/4KajDuDEyq37rVMxPuNYhnIKlfxx0Yel88VWryol9Am5LmgYdBsMkD7EV4e
nh70VydO2XVdw==
X-Clacks-Overhead: "GNU Terry Pratchett"
X-CM-Score: 0.00
X-CNFS-Analysis: v=2.4 cv=DPmcXgBb c=1 sm=1 tr=0 ts=64f4bea8
a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17
a=IkcTkHD0fZMA:10 a=AqMAq9RFtlYWk7O3nJ8A:9 a=QEXdDO2ut3YA:10
X-AUTH: mrabarnett@:2500
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.15.0
Content-Language: en-GB
In-Reply-To: <6943DF5C-BBD7-40FB-872E-87F111D52059@mostrom.pp.se>
X-CMAE-Envelope: MS4xfDfeNFkbtspwxBN2q7zoWXVKatAjpTGsNhUfNznS6UXr7BRNsl8AsrVLoify91kfutfsEl9YJePJfzB3WgOMaDzOfsbeBb1c18c99pOqveVnoHTHKIWC
6fmyssRFwuGe3UQ572njREWeJrmFVT3lSgcR8bXh2HQ7Pqq4mWMBqnqlDWRFGDhEOB6SMkIJrtJr1hNBrL749c+zZmAP24XD94o=
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: <a8c7157b-bbb0-0e60-d2b2-4259e971e49c@mrabarnett.plus.com>
X-Mailman-Original-References: <6943DF5C-BBD7-40FB-872E-87F111D52059@mostrom.pp.se>
 by: MRAB - Sun, 3 Sep 2023 17:13 UTC

On 2023-09-03 17:10, Jan Erik Moström via Python-list wrote:
> I'm looking for some advice for how to write this in a clean way
>
> 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?
>
You could use pass an anonymous function (a lambda) to re.sub:

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

global_var1 = "bla bla"
global_var2 = "pff"

new_text = re.sub(im_pattern, lambda m, var1=global_var1,
var2=global_var2: fix_stuff(m, var1, var2), md_text)

Or, if you prefer a named function, define one just before the re.sub:

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

global_var1 = "bla bla"
global_var2 = "pff"

def fix_it(m, var1=global_var1, var2=global_var2):
return fix_stuff(m, var1, var2)

new_text = re.sub(im_pattern, fix_it, md_text)

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor