argparse modify

30 views
Skip to first unread message

נתי שטרן

unread,
Jun 23, 2022, 10:52:55 AMJun 23
to
how to solve this (argparse)


traceback:
Traceback (most recent call last):
File "u:\oracle\RTR.py", line 10, in <module>
class sre_constants():
File "u:\oracle\RTR.py", line 77, in sre_constants
MAXREPEAT = _NamedIntConstant(32,name=str(32))
TypeError: 'name' is an invalid keyword argument for int()

code:
class sre_constants():
#
# Secret Labs' Regular Expression Engine
#
# various symbols used by the regular expression engine.
# run this script to update the _sre include files!
#
# Copyright (c) 1998-2001 by Secret Labs AB. All rights reserved.
#
# See the sre.py file for information on usage and redistribution.
#

"""Internal support module for sre"""

# update when constants are added or removed

MAGIC = 20171005

from _sre import MAXREPEAT, MAXGROUPS

# SRE standard exception (access as sre.error)
# should this really be here?

class error(Exception):
"""Exception raised for invalid regular expressions.

Attributes:

msg: The unformatted error message
pattern: The regular expression pattern
pos: The index in the pattern where compilation failed (may be
None)
lineno: The line corresponding to pos (may be None)
colno: The column corresponding to pos (may be None)
"""

__module__ = 're'

def __init__(self, msg, pattern=None, pos=None):
self.msg = msg
self.pattern = pattern
self.pos = pos
if pattern is not None and pos is not None:
msg = '%s at position %d' % (msg, pos)
if isinstance(pattern, str):
newline = '\n'
else:
newline = b'\n'
self.lineno = pattern.count(newline, 0, pos) + 1
self.colno = pos - pattern.rfind(newline, 0, pos)
if newline in pattern:
msg = '%s (line %d, column %d)' % (msg, self.lineno,
self.colno)
else:
self.lineno = self.colno = None
super().__init__(msg)

class _NamedIntConstant(int):
def __init__(cls, value):
self = super(_NamedIntConstant, cls).__init__(cls, value)
self.name = name
return self

def __repr__(self):
return self.name


__reduce__ = None

MAXREPEAT = _NamedIntConstant(32,name=str(32))


def _makecodes(names):
names = names.strip().split()
items = [_NamedIntConstant(i, name) for i, name in enumerate(names)]
globals().update({item.name: item for item in items})
return items





--
<https://netanel.ml>

Dieter Maurer

unread,
Jun 23, 2022, 12:57:55 PMJun 23
to
נתי שטרן wrote at 2022-6-23 15:31 +0300:
>how to solve this (argparse)
>
>
>traceback:
>Traceback (most recent call last):
> File "u:\oracle\RTR.py", line 10, in <module>
> class sre_constants():
> File "u:\oracle\RTR.py", line 77, in sre_constants
> MAXREPEAT = _NamedIntConstant(32,name=str(32))
>TypeError: 'name' is an invalid keyword argument for int()

This does not look like an `argparse` problem:
the traceback comes from `oracle/RTR.py`.

Dennis Lee Bieber

unread,
Jun 23, 2022, 6:33:00 PMJun 23
to
On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer" <die...@handshake.de>
declaimed the following:

>??? ???? wrote at 2022-6-23 15:31 +0300:
>>how to solve this (argparse)

>> MAXREPEAT = _NamedIntConstant(32,name=str(32))
>>TypeError: 'name' is an invalid keyword argument for int()
>
>This does not look like an `argparse` problem:
>the traceback comes from `oracle/RTR.py`.

And the listed code looks quite suspicious to me...

>> class _NamedIntConstant(int):
>> def __init__(cls, value):
>> self = super(_NamedIntConstant, cls).__init__(cls, value)
>> self.name = name
>> return self

There does not appear to be any provision for keyword arguments at all.
The only use of "name" is to an undefined object.


--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

Mats Wichmann

unread,
Jun 23, 2022, 7:02:18 PMJun 23
to
On 6/23/22 16:32, Dennis Lee Bieber wrote:
> On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer" <die...@handshake.de>
> declaimed the following:
>
>> ??? ???? wrote at 2022-6-23 15:31 +0300:
>>> how to solve this (argparse)
>
>>> MAXREPEAT = _NamedIntConstant(32,name=str(32))
>>> TypeError: 'name' is an invalid keyword argument for int()
>>
>> This does not look like an `argparse` problem:
>> the traceback comes from `oracle/RTR.py`.
>
> And the listed code looks quite suspicious to me...
>
>>> class _NamedIntConstant(int):
>>> def __init__(cls, value):
>>> self = super(_NamedIntConstant, cls).__init__(cls, value)
>>> self.name = name
>>> return self
>
> There does not appear to be any provision for keyword arguments at all.
> The only use of "name" is to an undefined object.

Indeed... a more typical version of this might be something like:

class _NamedIntConstant(int):
def __init__(self, name, **kwargs):
self.name = name
super().__init__(**kwargs)

Assuming: that the "value" in your init method signature was supposed to
be 'name' since that's what you use later - and would explain your
exception!

In Python init methods are initializers, not creators (despite that many
materials call them constructors) - when you get to it, "self" already
exists, so you don't want to assign to it. And you don't return it -
again, it already exists, all you're doing here is setting it up, and
part of that you delegated to the parent class's initializer.

Also note that while it's claimed to be fine These Days, inheriting from
a base type like this is sometimes tricky, sometimes broken... be
somewhat aware.

Chris Angelico

unread,
Jun 23, 2022, 7:11:14 PMJun 23
to
On Fri, 24 Jun 2022 at 09:03, Mats Wichmann <ma...@wichmann.us> wrote:
> Also note that while it's claimed to be fine These Days, inheriting from
> a base type like this is sometimes tricky, sometimes broken... be
> somewhat aware.

Depends on your definition of "broken". If you want to make a custom
integer type, you'll probably find that arithmetic operations on it
just return vanilla ints again, but in this case, it seems to be more
akin to IntEnum than to an arithmetic type. so it should be safe.

But that said: why not just use IntEnum? It looks like the purpose of
it is simply to be a name for a number, and that's something that an
enum does well (even if it's not strictly "enumerated" in that sense).

ChrisA

נתי שטרן

unread,
Jun 24, 2022, 1:28:45 AMJun 24
to
I copied code from argparse library and modified it

בתאריך יום חמישי, 23 ביוני 2022, מאת Dieter Maurer <die...@handshake.de>:

> נתי שטרן wrote at 2022-6-23 15:31 +0300:
> >how to solve this (argparse)
> >
> >
> >traceback:
> >Traceback (most recent call last):
> > File "u:\oracle\RTR.py", line 10, in <module>
> > class sre_constants():
> > File "u:\oracle\RTR.py", line 77, in sre_constants
> > MAXREPEAT = _NamedIntConstant(32,name=str(32))
> >TypeError: 'name' is an invalid keyword argument for int()
>
> This does not look like an `argparse` problem:
> the traceback comes from `oracle/RTR.py`.
>


--
<https://netanel.ml>

Dieter Maurer

unread,
Jun 24, 2022, 2:00:09 AMJun 24
to
נתי שטרן wrote at 2022-6-24 08:28 +0300:
>I copied code from argparse library and modified it
>
>בתאריך יום חמישי, 23 ביוני 2022, מאת Dieter Maurer <die...@handshake.de>:
>
>> נתי שטרן wrote at 2022-6-23 15:31 +0300:
>> >how to solve this (argparse)
>> >
>> >
>> >traceback:
>> >Traceback (most recent call last):
>> > File "u:\oracle\RTR.py", line 10, in <module>
>> > class sre_constants():
>> > File "u:\oracle\RTR.py", line 77, in sre_constants
>> > MAXREPEAT = _NamedIntConstant(32,name=str(32))
>> >TypeError: 'name' is an invalid keyword argument for int()

The exception information tells you:
` _NamedIntConstant(32,name=str(32))` raises a `TypeError`:
`_NamedIntConstant` does not know the keyword parameter `name`.
Thus, something is wrong with the `_NamedIntConstant` definition.

נתי שטרן

unread,
Jun 24, 2022, 2:07:46 AMJun 24
to
------------ הודעה שהועברה ----------
מאת: *Dieter Maurer* <die...@handshake.de>
תאריך: יום שישי, 24 ביוני 2022
נושא: argparse modify
אל: נתישטרן <nsh...@gmail.com>
עותק: "python-li...@python.org" <python-li...@python.org>, "
pytho...@python.org" <pytho...@python.org>
Do you have an idea for fixing it?

--
<https://netanel.ml>

נתי שטרן

unread,
Jun 24, 2022, 3:46:00 AMJun 24
to
Can you help me for solve it

בתאריך יום שישי, 24 ביוני 2022, מאת Dennis Lee Bieber <wlf...@ix.netcom.com
>:

> On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer" <die...@handshake.de>
> declaimed the following:
>
> >??? ???? wrote at 2022-6-23 15:31 +0300:
> >>how to solve this (argparse)
>
> >> MAXREPEAT = _NamedIntConstant(32,name=str(32))
> >>TypeError: 'name' is an invalid keyword argument for int()
> >
> >This does not look like an `argparse` problem:
> >the traceback comes from `oracle/RTR.py`.
>
> And the listed code looks quite suspicious to me...
>
> >> class _NamedIntConstant(int):
> >> def __init__(cls, value):
> >> self = super(_NamedIntConstant, cls).__init__(cls, value)
> >> self.name = name
> >> return self
>
> There does not appear to be any provision for keyword arguments at
> all.
> The only use of "name" is to an undefined object.
>
>
> --
> Wulfraed Dennis Lee Bieber AF6VN
> wlf...@ix.netcom.com http://wlfraed.microdiversity.
> freeddns.org/
> --
> https://mail.python.org/mailman/listinfo/python-list
>


--
<https://netanel.ml>

Roel Schroeven

unread,
Jun 24, 2022, 7:30:25 AMJun 24
to
Op 24/06/2022 om 0:32 schreef Dennis Lee Bieber:
> On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer"<die...@handshake.de>
> declaimed the following:
>
> >??? ???? wrote at 2022-6-23 15:31 +0300:
> >>how to solve this (argparse)
>
> >> MAXREPEAT = _NamedIntConstant(32,name=str(32))
> >>TypeError: 'name' is an invalid keyword argument for int()
> >
> >This does not look like an `argparse` problem:
> >the traceback comes from `oracle/RTR.py`.
>
> And the listed code looks quite suspicious to me...
>
> >> class _NamedIntConstant(int):
> >> def __init__(cls, value):
> >> self = super(_NamedIntConstant, cls).__init__(cls, value)
> >> self.name = name
> >> return self
>
> There does not appear to be any provision for keyword arguments at all.
> The only use of "name" is to an undefined object.
>
The code seems to be a copy of Lib/re/_constants.py (or
Lib/sre_constants.py before commit
1be3260a90f16aae334d993aecf7b70426f98013), but in _constants.py that
class uses __new__ instead of __init__:

    class _NamedIntConstant(int):
        def __new__(cls, value, name):
            self = super(_NamedIntConstant, cls).__new__(cls, value)
            self.name = name
            return self

        def __repr__(self):
            return self.name

        __reduce__ = None

(unless still older versions did use __init__)

It's used there as a kind of enum. I guess that code was originally
written before Python had enum.Enum. _makecodes() uses it so create
named int objects from its arguments, with automatically generated
consecutive int values, places them in the global namespace (feels like
a code smell to me) and also returns them.

    def _makecodes(*names):
        items = [_NamedIntConstant(i, name) for i, name in
enumerate(names)]
        globals().update({item.name: item for item in items})
        return items

    # operators
    OPCODES = _makecodes(
        # failure=0 success=1 (just because it looks better that way :-)
        'FAILURE', 'SUCCESS',

        'ANY', 'ANY_ALL',
        'ASSERT', 'ASSERT_NOT',
        'AT',
        # ...
        )

נתי שטרן, are you trying to use that semi-enum functionality? Most
likely you're better of using enum.Enum instead.

--
"You can fool some of the people all the time, and all of the people some
of the time, but you cannot fool all of the people all of the time."
-- Abraham Lincoln
"You can fool too many of the people too much of the time."
-- James Thurber

נתי שטרן

unread,
Jun 24, 2022, 8:16:49 AMJun 24
to
Thanks a lot.
I will read documentation of enum module

בתאריך יום ו׳, 24 ביוני 2022, 14:33, מאת Roel Schroeven ‏<
ro...@roelschroeven.net>:

> Op 24/06/2022 om 0:32 schreef Dennis Lee Bieber:
> > On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer"<die...@handshake.de>
> > declaimed the following:
> >
> > >??? ???? wrote at 2022-6-23 15:31 +0300:
> > >>how to solve this (argparse)
> >
> > >> MAXREPEAT = _NamedIntConstant(32,name=str(32))
> > >>TypeError: 'name' is an invalid keyword argument for int()
> > >
> > >This does not look like an `argparse` problem:
> > >the traceback comes from `oracle/RTR.py`.
> >
> > And the listed code looks quite suspicious to me...
> >
> > >> class _NamedIntConstant(int):
> > >> def __init__(cls, value):
> > >> self = super(_NamedIntConstant, cls).__init__(cls, value)
> > >> self.name = name
> > >> return self
> >
> > There does not appear to be any provision for keyword arguments at
> all.
> > The only use of "name" is to an undefined object.
> >
> The code seems to be a copy of Lib/re/_constants.py (or
> Lib/sre_constants.py before commit
> 1be3260a90f16aae334d993aecf7b70426f98013), but in _constants.py that
> class uses __new__ instead of __init__:
>
> class _NamedIntConstant(int):
> def __new__(cls, value, name):
> self = super(_NamedIntConstant, cls).__new__(cls, value)
> self.name = name
> return self
>
> --
> https://mail.python.org/mailman/listinfo/python-list
>

Dennis Lee Bieber

unread,
Jun 24, 2022, 2:14:04 PMJun 24
to
On Thu, 23 Jun 2022 17:01:42 -0600, Mats Wichmann <ma...@wichmann.us>
declaimed the following:

>Assuming: that the "value" in your init method signature was supposed to
>be 'name' since that's what you use later - and would explain your
>exception!
>

Since it is a "named int", I'd expect value to the integer value of
this "constant" (and there should probably be a generic setter that... bars
changing value or name later). If a "name" keyword argument is not
supplied, using the str() of the value for the name might be valid.
Reply all
Reply to author
Forward
0 new messages