Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Everyone's head is a cheap movie show." -- Jeff G. Bone


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

SubjectAuthor
* Passing info to function used in re.subJan Erik Moström
`- Re: Passing info to function used in re.subStefan Ram

1
Passing info to function used in re.sub

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

  copy mid

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

  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: lis...@mostrom.pp.se (Jan Erik Moström)
Newsgroups: comp.lang.python
Subject: Passing info to function used in re.sub
Date: Sun, 03 Sep 2023 18:10:29 +0200
Lines: 25
Message-ID: <mailman.268.1693757502.23016.python-list@python.org>
References: <6943DF5C-BBD7-40FB-872E-87F111D52059@mostrom.pp.se>
Mime-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de eyQB7/zo0RDajtKCNZ/aLwuRq0EYY18U/MkkgAshxx2g==
Cancel-Lock: sha1:6xKtO+CW+XsiNmdSw6SSD/KEKq0= sha256:QtLgOj7/QPfIJaMoXEeQmlxYyy9ByRcFGYXQksIcIk4=
Return-Path: <lists@mostrom.pp.se>
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.072
X-Spam-Evidence: '*H*': 0.86; '*S*': 0.00; 'this:': 0.03; 'def': 0.04;
'subject:used': 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; 'assume': 0.32; 'but': 0.32; "i'm": 0.33;
'there': 0.33; 'received:192.168.0': 0.33; 'currently': 0.37;
'using': 0.37; 'received:192.168': 0.37; 'file': 0.38; 'way':
0.38; 'text': 0.39; 'this,': 0.39; 'something': 0.40; 'want':
0.40; 'here': 0.62; 'from:charset:utf-8': 0.64; 'received:195':
0.64; 'look': 0.65; 'right': 0.68; 'global': 0.73
X-Halon-ID: 66c7dcfd-4a74-11ee-b7d6-cf458ee68324
Authorized-sender: lists@mostrom.pp.se
X-Mailer: MailMate (1.14r5937)
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: <6943DF5C-BBD7-40FB-872E-87F111D52059@mostrom.pp.se>
 by: Jan Erik Moström - Sun, 3 Sep 2023 16:10 UTC

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?

= jem

Re: Passing info to function used in re.sub

<advice-20230903173607@ram.dialup.fu-berlin.de>

  copy mid

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

  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: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Passing info to function used in re.sub
Date: 3 Sep 2023 16:53:02 GMT
Organization: Stefan Ram
Lines: 37
Expires: 1 Sep 2024 11:59:58 GMT
Message-ID: <advice-20230903173607@ram.dialup.fu-berlin.de>
References: <mailman.268.1693757502.23016.python-list@python.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de BrYugGJuW2Ge5/xXEkLHXg/waijBwuI5vIm15tbFjcfZ/M
Cancel-Lock: sha1:53bHgnL4kYJHjBpa/CncuCSHvCw= sha256:132AGEgnzybtfQlJojV4WwxkXvweABgHpZJ92PYtEzs=
X-Copyright: (C) Copyright 2023 Stefan Ram. All rights reserved.
Distribution through any means other than regular usenet
channels is forbidden. It is forbidden to publish this
article in the Web, to change URIs of this article into links,
and to transfer the body without this notice, but quotations
of parts in other Usenet posts are allowed.
X-No-Archive: Yes
Archive: no
X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some
services to mirror the article in the web. But the article may
be kept on a Usenet archive server with only NNTP access.
X-No-Html: yes
Content-Language: en-US
Accept-Language: de-DE-1901, en-US, it, fr-FR
 by: Stefan Ram - Sun, 3 Sep 2023 16:53 UTC

=?utf-8?q?Jan_Erik_Mostr=C3=B6m?= <lists@mostrom.pp.se> writes:
>def fix_stuff(m):
> # that what's available in m
> replacement_text = m.group(1) + global_var1 + global_var2
> return replacement_text
....
>new_text = re.sub(im_pattern,fix_stuff,md_text)

You can use a closure:

import re

def make_replace( was_global_1, was_global_2 ):
def replace( matcher ):
return matcher.group( 1 )+ was_global_1 + was_global_2
return replace

replace = make_replace( "a", "b" )

print( re.sub( r'(.)', replace, 'x' ))

. Note how I wrote code one can actually execute.
You could have done this too!

Or, partial application:

import functools
import re

def replace( was_global_1, was_global_2, matcher ):
return matcher.group( 1 )+ was_global_1 + was_global_2

print( re.sub( r'(.)', functools.partial( replace, 'a', 'b' ), 'x' ))

.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor