Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"You know, we've won awards for this crap." -- David Letterman


devel / comp.lang.python / Re: Match.groupdict: Meaning of default argument?

SubjectAuthor
* Match.groupdict: Meaning of default argument?Loris Bennett
+* Re: Match.groupdict: Meaning of default argument?Stefan Ram
|`* Re: Match.groupdict: Meaning of default argument?Loris Bennett
| `- Re: Match.groupdict: Meaning of default argument?Loris Bennett
`* Re: Match.groupdict: Meaning of default argument?Julio Di Egidio
 `* Re: Match.groupdict: Meaning of default argument?Loris Bennett
  `* Re: Match.groupdict: Meaning of default argument?Julio Di Egidio
   `* Re: Match.groupdict: Meaning of default argument?Julio Di Egidio
    `- Re: Match.groupdict: Meaning of default argument?Julio Di Egidio

1
Match.groupdict: Meaning of default argument?

<877d78z5us.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.be...@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Match.groupdict: Meaning of default argument?
Date: Fri, 29 Apr 2022 09:49:47 +0200
Organization: Freie Universitaet Berlin
Lines: 36
Message-ID: <877d78z5us.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de YVl60Q1LAno79n8nv7gubAx7Qjv8E/ny0JGIgORFEM5PYr
Cancel-Lock: sha1:xHAO3d8r/4rkpEFkjMxE02hnveM=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Fri, 29 Apr 2022 07:49 UTC

Hi,

If I do

import re
pattern = re.compile(r'(?P<days>\d*)(-?)(?P<hours>\d\d):(?P<minutes>\d\d):(?P<seconds>\d\d)')
s = '104-02:47:06'
match = pattern.search(s)
match_dict = match.groupdict('0')

I get

match_dict
{'days': '104', 'hours': '02', 'minutes': '47', 'seconds': '06'}

However, if the string has no initial part (corresponding to the number of
days), e.g.

s = '02:47:06'
match = pattern.search(s)
match_dict = match.groupdict('0')

I get

match_dict
{'days': '', 'hours': '02', 'minutes': '47', 'seconds': '06'}

I thought that 'days' would default to '0'.

What am I doing wrong?

Cheers,

Loris
--
This signature is currently under construction.

Re: Match.groupdict: Meaning of default argument?

<participate-20220429161639@ram.dialup.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: ram...@zedat.fu-berlin.de (Stefan Ram)
Newsgroups: comp.lang.python
Subject: Re: Match.groupdict: Meaning of default argument?
Date: 29 Apr 2022 15:18:48 GMT
Organization: Stefan Ram
Lines: 12
Expires: 1 Apr 2023 11:59:58 GMT
Message-ID: <participate-20220429161639@ram.dialup.fu-berlin.de>
References: <877d78z5us.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-Trace: news.uni-berlin.de ni3Mmn0Pb4Xd4easmRWWRg5g33tsxB6bOJ4VwhR7AXbPNl
X-Copyright: (C) Copyright 2022 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, en-US, it, fr-FR
 by: Stefan Ram - Fri, 29 Apr 2022 15:18 UTC

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>I thought that 'days' would default to '0'.

It will get the value '0' if (?P<days>\d*) does
/not/ participate in the match.

In your case, it /does/ participate in the match,
\d* matching the empty string.

Try (?P<days>\d+)?.

Re: Match.groupdict: Meaning of default argument?

<22f9a92c-3117-4f6a-820e-b7f3c44a655cn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:a05:6214:519b:b0:456:48f2:a1b1 with SMTP id kl27-20020a056214519b00b0045648f2a1b1mr2187563qvb.4.1651298128469;
Fri, 29 Apr 2022 22:55:28 -0700 (PDT)
X-Received: by 2002:a05:620a:1a06:b0:69f:b536:af3c with SMTP id
bk6-20020a05620a1a0600b0069fb536af3cmr1772118qkb.772.1651298128338; Fri, 29
Apr 2022 22:55:28 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Fri, 29 Apr 2022 22:55:28 -0700 (PDT)
In-Reply-To: <877d78z5us.fsf@hornfels.zedat.fu-berlin.de>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.99.90; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.99.90
References: <877d78z5us.fsf@hornfels.zedat.fu-berlin.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <22f9a92c-3117-4f6a-820e-b7f3c44a655cn@googlegroups.com>
Subject: Re: Match.groupdict: Meaning of default argument?
From: jul...@diegidio.name (Julio Di Egidio)
Injection-Date: Sat, 30 Apr 2022 05:55:28 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 34
 by: Julio Di Egidio - Sat, 30 Apr 2022 05:55 UTC

On Friday, 29 April 2022 at 09:50:08 UTC+2, Loris Bennett wrote:
> Hi,
>
> If I do
>
> import re
> pattern = re.compile(r'(?P<days>\d*)(-?)(?P<hours>\d\d):(?P<minutes>\d\d):(?P<seconds>\d\d)')
> s = '104-02:47:06'
> match = pattern.search(s)
> match_dict = match.groupdict('0')
>
> I get
>
> match_dict
> {'days': '104', 'hours': '02', 'minutes': '47', 'seconds': '06'}
>
> However, if the string has no initial part (corresponding to the number of
> days), e.g.
>
> s = '02:47:06'
> match = pattern.search(s)
> match_dict = match.groupdict('0')
>
> I get
>
> match_dict
> {'days': '', 'hours': '02', 'minutes': '47', 'seconds': '06'}
>
> I thought that 'days' would default to '0'.
>
> What am I doing wrong?

You tell, but it's quite obvious that you (just) run a regex on a string and captures are going to be strings: indeed, '02' is not a number either...

Julio

Re: Match.groupdict: Meaning of default argument?

<8735hqsv6x.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.be...@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: Match.groupdict: Meaning of default argument?
Date: Tue, 03 May 2022 13:37:58 +0200
Organization: Freie Universitaet Berlin
Lines: 21
Message-ID: <8735hqsv6x.fsf@hornfels.zedat.fu-berlin.de>
References: <877d78z5us.fsf@hornfels.zedat.fu-berlin.de>
<participate-20220429161639@ram.dialup.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de 3MxRPvojDbO9z++vZlilLQ9WxluMScio6GnxemlliQC4J3
Cancel-Lock: sha1:q8e8D4lhn8LOsn1VsOhrtJIUS2c=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Tue, 3 May 2022 11:37 UTC

ram@zedat.fu-berlin.de (Stefan Ram) writes:

> "Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>>I thought that 'days' would default to '0'.
>
> It will get the value '0' if (?P<days>\d*) does
> /not/ participate in the match.
>
> In your case, it /does/ participate in the match,
> \d* matching the empty string.
>
> Try (?P<days>\d+)?.

Ah, thanks. I was misunderstanding the meaning of 'participate'.

Cheers,

Loris

--
This signature is currently under construction.

Re: Match.groupdict: Meaning of default argument?

<87v8umrg2s.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.be...@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: Match.groupdict: Meaning of default argument?
Date: Tue, 03 May 2022 13:49:47 +0200
Organization: Freie Universitaet Berlin
Lines: 39
Message-ID: <87v8umrg2s.fsf@hornfels.zedat.fu-berlin.de>
References: <877d78z5us.fsf@hornfels.zedat.fu-berlin.de>
<participate-20220429161639@ram.dialup.fu-berlin.de>
<8735hqsv6x.fsf@hornfels.zedat.fu-berlin.de>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de CWBE8F4W+2cDe8foSrSXlAX/3YygoQCN+DoSoyiCu16MY1
Cancel-Lock: sha1:0s2lV/Uk8OtFqWlZZ2DofhyV2Xc=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Tue, 3 May 2022 11:49 UTC

"Loris Bennett" <loris.bennett@fu-berlin.de> writes:

> ram@zedat.fu-berlin.de (Stefan Ram) writes:
>
>> "Loris Bennett" <loris.bennett@fu-berlin.de> writes:
>>>I thought that 'days' would default to '0'.
>>
>> It will get the value '0' if (?P<days>\d*) does
>> /not/ participate in the match.
>>
>> In your case, it /does/ participate in the match,
>> \d* matching the empty string.
>>
>> Try (?P<days>\d+)?.
>
> Ah, thanks. I was misunderstanding the meaning of 'participate'.

What I actually need is

((?P<days>\d+)(-?))?(?P<hours>\d\d):(?P<minutes>\d\d):(?P<seconds>\d\d)

so that I can match both

99-11:22:33

and

11:22:33

and have 'days' be '0' in the later case.
Thanks for pointing me in the right direction.

Cheers,

Loris

--
This signature is currently under construction.

Re: Match.groupdict: Meaning of default argument?

<87k0b2n0bf.fsf@hornfels.zedat.fu-berlin.de>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: loris.be...@fu-berlin.de (Loris Bennett)
Newsgroups: comp.lang.python
Subject: Re: Match.groupdict: Meaning of default argument?
Date: Tue, 03 May 2022 16:43:48 +0200
Organization: Freie Universitaet Berlin
Lines: 49
Message-ID: <87k0b2n0bf.fsf@hornfels.zedat.fu-berlin.de>
References: <877d78z5us.fsf@hornfels.zedat.fu-berlin.de>
<22f9a92c-3117-4f6a-820e-b7f3c44a655cn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain
X-Trace: news.uni-berlin.de Ta5/yk9JccmPXKpLM9z59wBQa02K6qQR7kugjy4XQXZDQM
Cancel-Lock: sha1:DOkYYtouwXz3M0I6+9QQX0AlJDI=
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
 by: Loris Bennett - Tue, 3 May 2022 14:43 UTC

Julio Di Egidio <julio@diegidio.name> writes:

> On Friday, 29 April 2022 at 09:50:08 UTC+2, Loris Bennett wrote:
>> Hi,
>>
>> If I do
>>
>> import re
>> pattern = re.compile(r'(?P<days>\d*)(-?)(?P<hours>\d\d):(?P<minutes>\d\d):(?P<seconds>\d\d)')
>> s = '104-02:47:06'
>> match = pattern.search(s)
>> match_dict = match.groupdict('0')
>>
>> I get
>>
>> match_dict
>> {'days': '104', 'hours': '02', 'minutes': '47', 'seconds': '06'}
>>
>> However, if the string has no initial part (corresponding to the number of
>> days), e.g.
>>
>> s = '02:47:06'
>> match = pattern.search(s)
>> match_dict = match.groupdict('0')
>>
>> I get
>>
>> match_dict
>> {'days': '', 'hours': '02', 'minutes': '47', 'seconds': '06'}
>>
>> I thought that 'days' would default to '0'.
>>
>> What am I doing wrong?
>
> You tell, but it's quite obvious that you (just) run a regex on a string and captures are going to be strings: indeed, '02' is not a number either...
>
> Julio

I am not sure what you are trying to tell me. I wasn't expecting
anything other than strings. The problem was, as Stefan helped me to
understand, that I misunderstood what 'participating in the match'
means.

Cheers,

Loris

--
This signature is currently under construction.

Re: Match.groupdict: Meaning of default argument?

<27fbb06f-a6a0-4c98-aca4-c58410425e75n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:a05:620a:454e:b0:69f:b9fd:f05d with SMTP id u14-20020a05620a454e00b0069fb9fdf05dmr13052559qkp.633.1651598299114;
Tue, 03 May 2022 10:18:19 -0700 (PDT)
X-Received: by 2002:ac8:598a:0:b0:2f3:637b:e4a with SMTP id
e10-20020ac8598a000000b002f3637b0e4amr15478097qte.389.1651598298930; Tue, 03
May 2022 10:18:18 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Tue, 3 May 2022 10:18:18 -0700 (PDT)
In-Reply-To: <87k0b2n0bf.fsf@hornfels.zedat.fu-berlin.de>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.97.126; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.97.126
References: <877d78z5us.fsf@hornfels.zedat.fu-berlin.de> <22f9a92c-3117-4f6a-820e-b7f3c44a655cn@googlegroups.com>
<87k0b2n0bf.fsf@hornfels.zedat.fu-berlin.de>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <27fbb06f-a6a0-4c98-aca4-c58410425e75n@googlegroups.com>
Subject: Re: Match.groupdict: Meaning of default argument?
From: jul...@diegidio.name (Julio Di Egidio)
Injection-Date: Tue, 03 May 2022 17:18:19 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 50
 by: Julio Di Egidio - Tue, 3 May 2022 17:18 UTC

On Tuesday, 3 May 2022 at 16:44:10 UTC+2, Loris Bennett wrote:
> Julio Di Egidio <ju...@diegidio.name> writes:
> > On Friday, 29 April 2022 at 09:50:08 UTC+2, Loris Bennett wrote:
> >> Hi,
> >>
> >> If I do
> >>
> >> import re
> >> pattern = re.compile(r'(?P<days>\d*)(-?)(?P<hours>\d\d):(?P<minutes>\d\d):(?P<seconds>\d\d)')
> >> s = '104-02:47:06'
> >> match = pattern.search(s)
> >> match_dict = match.groupdict('0')
> >>
> >> I get
> >>
> >> match_dict
> >> {'days': '104', 'hours': '02', 'minutes': '47', 'seconds': '06'}
> >>
> >> However, if the string has no initial part (corresponding to the number of
> >> days), e.g.
> >>
> >> s = '02:47:06'
> >> match = pattern.search(s)
> >> match_dict = match.groupdict('0')
> >>
> >> I get
> >>
> >> match_dict
> >> {'days': '', 'hours': '02', 'minutes': '47', 'seconds': '06'}
> >>
> >> I thought that 'days' would default to '0'.
> >>
> >> What am I doing wrong?
> >
> > You tell, but it's quite obvious that you (just) run a regex on a string and captures are going to be strings: indeed, '02' is not a number either....
>
> I am not sure what you are trying to tell me. I wasn't expecting
> anything other than strings. The problem was, as Stefan helped me to
> understand, that I misunderstood what 'participating in the match'
> means.

Stefan's "fix" makes it fail if there is not at least one digit in that position. And not only I don't se how that is in fact a fix, nor I still can guess where the string '0' is supposed to originate from in case we do not have the days part in the input. Maybe I am overlooking something...?

Julio

Re: Match.groupdict: Meaning of default argument?

<6d798569-e395-4249-8cbc-7f1c80dde35en@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:a05:6214:e6a:b0:446:154a:7e02 with SMTP id jz10-20020a0562140e6a00b00446154a7e02mr14507792qvb.82.1651598643675;
Tue, 03 May 2022 10:24:03 -0700 (PDT)
X-Received: by 2002:ad4:5347:0:b0:42c:4711:5eb5 with SMTP id
v7-20020ad45347000000b0042c47115eb5mr14344347qvs.101.1651598643536; Tue, 03
May 2022 10:24:03 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Tue, 3 May 2022 10:24:03 -0700 (PDT)
In-Reply-To: <27fbb06f-a6a0-4c98-aca4-c58410425e75n@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.97.126; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.97.126
References: <877d78z5us.fsf@hornfels.zedat.fu-berlin.de> <22f9a92c-3117-4f6a-820e-b7f3c44a655cn@googlegroups.com>
<87k0b2n0bf.fsf@hornfels.zedat.fu-berlin.de> <27fbb06f-a6a0-4c98-aca4-c58410425e75n@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6d798569-e395-4249-8cbc-7f1c80dde35en@googlegroups.com>
Subject: Re: Match.groupdict: Meaning of default argument?
From: jul...@diegidio.name (Julio Di Egidio)
Injection-Date: Tue, 03 May 2022 17:24:03 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 58
 by: Julio Di Egidio - Tue, 3 May 2022 17:24 UTC

On Tuesday, 3 May 2022 at 19:18:30 UTC+2, Julio Di Egidio wrote:
> On Tuesday, 3 May 2022 at 16:44:10 UTC+2, Loris Bennett wrote:
> > Julio Di Egidio <ju...@diegidio.name> writes:
> > > On Friday, 29 April 2022 at 09:50:08 UTC+2, Loris Bennett wrote:
> > >> Hi,
> > >>
> > >> If I do
> > >>
> > >> import re
> > >> pattern = re.compile(r'(?P<days>\d*)(-?)(?P<hours>\d\d):(?P<minutes>\d\d):(?P<seconds>\d\d)')
> > >> s = '104-02:47:06'
> > >> match = pattern.search(s)
> > >> match_dict = match.groupdict('0')
> > >>
> > >> I get
> > >>
> > >> match_dict
> > >> {'days': '104', 'hours': '02', 'minutes': '47', 'seconds': '06'}
> > >>
> > >> However, if the string has no initial part (corresponding to the number of
> > >> days), e.g.
> > >>
> > >> s = '02:47:06'
> > >> match = pattern.search(s)
> > >> match_dict = match.groupdict('0')
> > >>
> > >> I get
> > >>
> > >> match_dict
> > >> {'days': '', 'hours': '02', 'minutes': '47', 'seconds': '06'}
> > >>
> > >> I thought that 'days' would default to '0'.
> > >>
> > >> What am I doing wrong?
> > >
> > > You tell, but it's quite obvious that you (just) run a regex on a string and captures are going to be strings: indeed, '02' is not a number either...
> >
> > I am not sure what you are trying to tell me. I wasn't expecting
> > anything other than strings. The problem was, as Stefan helped me to
> > understand, that I misunderstood what 'participating in the match'
> > means.
> Stefan's "fix" makes it fail if there is not at least one digit in that position. And not only I don't se how that is in fact a fix, nor I still can guess where the string '0' is supposed to originate from in case we do not have the days part in the input. Maybe I am overlooking something...?

Yes, I am missing the meaning of the parameter in the call to groupdict.
<https://docs.python.org/2.7/library/re.html#re.MatchObject.groupdict>

And then Stefan's solution makes sense, as it's the default for the *non-optional* parts that are missing...

Right? Sorry for the confusion.

Julio

Re: Match.groupdict: Meaning of default argument?

<4a4ad4a8-227c-4bc7-9b25-d0724ea16052n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.python
X-Received: by 2002:ac8:5b51:0:b0:2f3:8654:53b2 with SMTP id n17-20020ac85b51000000b002f3865453b2mr15817023qtw.300.1651598745741;
Tue, 03 May 2022 10:25:45 -0700 (PDT)
X-Received: by 2002:a05:620a:29cc:b0:69f:c297:a9ff with SMTP id
s12-20020a05620a29cc00b0069fc297a9ffmr12895144qkp.259.1651598745570; Tue, 03
May 2022 10:25:45 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.python
Date: Tue, 3 May 2022 10:25:45 -0700 (PDT)
In-Reply-To: <6d798569-e395-4249-8cbc-7f1c80dde35en@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=93.41.97.126; posting-account=F3H0JAgAAADcYVukktnHx7hFG5stjWse
NNTP-Posting-Host: 93.41.97.126
References: <877d78z5us.fsf@hornfels.zedat.fu-berlin.de> <22f9a92c-3117-4f6a-820e-b7f3c44a655cn@googlegroups.com>
<87k0b2n0bf.fsf@hornfels.zedat.fu-berlin.de> <27fbb06f-a6a0-4c98-aca4-c58410425e75n@googlegroups.com>
<6d798569-e395-4249-8cbc-7f1c80dde35en@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4a4ad4a8-227c-4bc7-9b25-d0724ea16052n@googlegroups.com>
Subject: Re: Match.groupdict: Meaning of default argument?
From: jul...@diegidio.name (Julio Di Egidio)
Injection-Date: Tue, 03 May 2022 17:25:45 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 63
 by: Julio Di Egidio - Tue, 3 May 2022 17:25 UTC

On Tuesday, 3 May 2022 at 19:24:14 UTC+2, Julio Di Egidio wrote:
> On Tuesday, 3 May 2022 at 19:18:30 UTC+2, Julio Di Egidio wrote:
> > On Tuesday, 3 May 2022 at 16:44:10 UTC+2, Loris Bennett wrote:
> > > Julio Di Egidio <ju...@diegidio.name> writes:
> > > > On Friday, 29 April 2022 at 09:50:08 UTC+2, Loris Bennett wrote:
> > > >> Hi,
> > > >>
> > > >> If I do
> > > >>
> > > >> import re
> > > >> pattern = re.compile(r'(?P<days>\d*)(-?)(?P<hours>\d\d):(?P<minutes>\d\d):(?P<seconds>\d\d)')
> > > >> s = '104-02:47:06'
> > > >> match = pattern.search(s)
> > > >> match_dict = match.groupdict('0')
> > > >>
> > > >> I get
> > > >>
> > > >> match_dict
> > > >> {'days': '104', 'hours': '02', 'minutes': '47', 'seconds': '06'}
> > > >>
> > > >> However, if the string has no initial part (corresponding to the number of
> > > >> days), e.g.
> > > >>
> > > >> s = '02:47:06'
> > > >> match = pattern.search(s)
> > > >> match_dict = match.groupdict('0')
> > > >>
> > > >> I get
> > > >>
> > > >> match_dict
> > > >> {'days': '', 'hours': '02', 'minutes': '47', 'seconds': '06'}
> > > >>
> > > >> I thought that 'days' would default to '0'.
> > > >>
> > > >> What am I doing wrong?
> > > >
> > > > You tell, but it's quite obvious that you (just) run a regex on a string and captures are going to be strings: indeed, '02' is not a number either...
> > >
> > > I am not sure what you are trying to tell me. I wasn't expecting
> > > anything other than strings. The problem was, as Stefan helped me to
> > > understand, that I misunderstood what 'participating in the match'
> > > means.
> > Stefan's "fix" makes it fail if there is not at least one digit in that position. And not only I don't se how that is in fact a fix, nor I still can guess where the string '0' is supposed to originate from in case we do not have the days part in the input. Maybe I am overlooking something...?
> Yes, I am missing the meaning of the parameter in the call to groupdict.
> <https://docs.python.org/2.7/library/re.html#re.MatchObject.groupdict>
>
> And then Stefan's solution makes sense, as it's the default for the *non-optional* parts that are missing...
>
> Right? Sorry for the confusion.

P.S. I don't see Stefan's posts, and I doubt he sees mine...

Julio

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor