• argparse modify

    From =?UTF-8?B?16DXqteZINep15jXqNef?=@21:1/5 to All on Thu Jun 23 15:31:35 2022
    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>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to All on Thu Jun 23 18:57:31 2022
    נתי שטרן 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`.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Chris Angelico@21:1/5 to Mats Wichmann on Fri Jun 24 09:10:43 2022
    On Fri, 24 Jun 2022 at 09:03, Mats Wichmann <mats@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dennis Lee Bieber@21:1/5 to All on Thu Jun 23 18:32:34 2022
    On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer" <dieter@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
    wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Mats Wichmann@21:1/5 to Dennis Lee Bieber on Thu Jun 23 17:01:42 2022
    On 6/23/22 16:32, Dennis Lee Bieber wrote:
    On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer" <dieter@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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to All on Fri Jun 24 07:59:39 2022
    נתי שטרן wrote at 2022-6-24 08:28 +0300:
    I copied code from argparse library and modified it

    בתאריך יום חמישי, 23 ביוני 2022, מאת Dieter Maurer <dieter@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.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?B?16DXqteZINep15jXqNef?=@21:1/5 to All on Fri Jun 24 08:28:18 2022
    I copied code from argparse library and modified it

    בתאריך יום חמישי, 23 ביוני 2022, מאת Dieter Maurer <dieter@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>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?B?16DXqteZINep15jXqNef?=@21:1/5 to All on Fri Jun 24 10:45:35 2022
    Can you help me for solve it

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

    On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer" <dieter@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
    wlfraed@ix.netcom.com http://wlfraed.microdiversity.
    freeddns.org/
    --
    https://mail.python.org/mailman/listinfo/python-list



    --
    <https://netanel.ml>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Roel Schroeven@21:1/5 to All on Fri Jun 24 13:29:57 2022
    Op 24/06/2022 om 0:32 schreef Dennis Lee Bieber:
    On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer"<dieter@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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From =?UTF-8?B?16DXqteZINep15jXqNef?=@21:1/5 to All on Fri Jun 24 15:16:14 2022
    Thanks a lot.
    I will read documentation of enum module

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

    Op 24/06/2022 om 0:32 schreef Dennis Lee Bieber:
    On Thu, 23 Jun 2022 18:57:31 +0200, "Dieter Maurer"<dieter@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
    --
    https://mail.python.org/mailman/listinfo/python-list


    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dennis Lee Bieber@21:1/5 to All on Fri Jun 24 14:13:40 2022
    On Thu, 23 Jun 2022 17:01:42 -0600, Mats Wichmann <mats@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.


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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)