• Trouble with defaults and timeout decorator

    From Jason Friedman@21:1/5 to All on Sat Jun 24 10:18:29 2023
    I'm writing a database connectivity module to be used by other modules and leveraging the jaydebeapi module.
    From what I can tell jaydebeapi contains no built-in timeout capability, so then I turned to https://pypi.org/project/timeout-decorator/.
    My goal is to have a default timeout of, say, 10 seconds, which can be overridden by the caller.


    import jaydebeapi
    from timeout_decorator import timeout

    class Database:
    database_connection = None
    database_name, user_name, password, host, port = stuff
    timeout = None

    def __init__(self, timeout=10):
    self.timeout = timeout

    @timeout(self.timeout)
    def get_connection(self):
    if not self.database_connection:
    self.database_connection = jaydebeapi.connect(some_args)
    return self.database_connection


    The trouble occurs on line 12 with:
    NameError: name 'self' is not defined

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Piergiorgio Sartor@21:1/5 to Jason Friedman on Sat Jun 24 19:36:55 2023
    On 24/06/2023 18.18, Jason Friedman wrote:
    I'm writing a database connectivity module to be used by other modules and leveraging the jaydebeapi module.
    From what I can tell jaydebeapi contains no built-in timeout capability, so
    then I turned to https://pypi.org/project/timeout-decorator/.
    My goal is to have a default timeout of, say, 10 seconds, which can be overridden by the caller.


    import jaydebeapi
    from timeout_decorator import timeout

    class Database:
    database_connection = None
    database_name, user_name, password, host, port = stuff
    timeout = None

    def __init__(self, timeout=10):
    self.timeout = timeout

    @timeout(self.timeout)
    def get_connection(self):
    if not self.database_connection:
    self.database_connection = jaydebeapi.connect(some_args)
    return self.database_connection


    The trouble occurs on line 12 with:
    NameError: name 'self' is not defined

    A quick search would return that "self"
    is not available in the class body, only
    in the class methods.

    There are workarounds, but I guess not
    simple ones, expecially for "timeout".

    bye,

    --

    piergiorgio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From MRAB@21:1/5 to Jason Friedman via Python-list on Sat Jun 24 20:03:34 2023
    On 2023-06-24 17:18, Jason Friedman via Python-list wrote:
    I'm writing a database connectivity module to be used by other modules and leveraging the jaydebeapi module.
    From what I can tell jaydebeapi contains no built-in timeout capability, so then I turned to https://pypi.org/project/timeout-decorator/.
    My goal is to have a default timeout of, say, 10 seconds, which can be overridden by the caller.


    import jaydebeapi
    from timeout_decorator import timeout

    class Database:
    database_connection = None
    database_name, user_name, password, host, port = stuff
    timeout = None

    def __init__(self, timeout=10):
    self.timeout = timeout

    @timeout(self.timeout)
    def get_connection(self):
    if not self.database_connection:
    self.database_connection = jaydebeapi.connect(some_args)
    return self.database_connection


    The trouble occurs on line 12 with:
    NameError: name 'self' is not defined

    The decorator is applied when the class is defined, but 'self' exists
    only in 'Database.__init__' and 'Database.get_connection' when they are
    called.

    Have you tried applying the decorator "manually" in 'Database.__init__'?

    def __init__(self, timeout=10):
    self.timeout = timeout
    self.get_connection = timeout(self.timeout)(self.get_connection)

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