• Context without manager

    From Piergiorgio Sartor@21:1/5 to All on Sat Nov 25 22:15:58 2023
    Hi all,

    I apologize in advance for the "foggy"
    question, but I've myself unclear ideas.

    Anyway...

    Python has "context manager".
    For example, the "open()" class can be
    simply used as follow:

    with open(...) as fp:
    fp.do_something()

    On the other hand, it is also possible to do:

    fp = open()
    fp.do_something()
    fp.close()

    Now, if we want to use "open()" in a class,
    it would be possible to apply the second
    variant, with "self.fp = open()" in "__init__(...)",
    "self.fp.close()" maybe in "__del__(...)" and having
    few methods doing this and that with the "self.fp".

    Apparently, the "with" context manager is not usable
    in classes, at least not with __init__() & co.

    It seems there are classes ("gradio.Blocks()", for
    example) which are *only* usable with context manager.

    I found more...

    One way to do the same as in "open()" is:

    def __init__(...):
    fp = open(...)
    fp.__enter__()
    ...
    def __del__(...):
    fp.__exit__()
    fp.close()

    This works, but it seems quite ugly.
    I could not find any other way, in case the
    class do only support context manager.

    Question: is there any other way to use a
    context manager only object within a class,
    with methods accessing the object?

    Or any other solution to the same situation?

    Thanks a lot in advance.

    P.S.: currently gmail posts are deleted, due
    to excessive spam, so I'll not see any reply
    coming from this family of addresses.

    bye,

    --

    piergiorgio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dieter Maurer@21:1/5 to Piergiorgio Sartor on Sun Nov 26 18:50:38 2023
    Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100:
    ...
    Apparently, the "with" context manager is not usable
    in classes, at least not with __init__() & co.

    You can use `with` in classes -- with any context manager.
    However, you would usually not use `with` with a file you have opened
    in `__init__`.

    If a class defines `__enter__` and `__exit__` (i.e.
    the "cntext manager protocol"), then its instances
    can be used with the `with` statement.

    The important use case for a context manager is the
    situation:
    set up a context (--> method `__enter__`)
    perform some operations in this context (--> body of `with` statement)
    tear down the context (--> method `__exit__`).
    If you do not have this case (e.g. usually if you open the file
    in a class's `__init__`), you do not use a context manager.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Piergiorgio Sartor@21:1/5 to Dieter Maurer on Sun Nov 26 19:58:58 2023
    On 26/11/2023 18.50, Dieter Maurer wrote:
    Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100:
    ...
    Apparently, the "with" context manager is not usable
    in classes, at least not with __init__() & co.

    You can use `with` in classes -- with any context manager.
    However, you would usually not use `with` with a file you have opened
    in `__init__`.

    If a class defines `__enter__` and `__exit__` (i.e.
    the "cntext manager protocol"), then its instances
    can be used with the `with` statement.

    The important use case for a context manager is the
    situation:
    set up a context (--> method `__enter__`)
    perform some operations in this context (--> body of `with` statement)
    tear down the context (--> method `__exit__`).
    If you do not have this case (e.g. usually if you open the file
    in a class's `__init__`), you do not use a context manager.

    Very clear, but what if the class is *not* "open()",
    but something else _requiring_ using "with"?
    How to do this in a "__init__()" of a class?

    In other words, what if "open()" could *only* be used
    with "with" and not just by assigning "fp = open()"?

    The problem is I've some SDK of some device which
    provides context manager *only* classes.

    I *cannot* do:

    device = device_open(...)
    device.do_something()
    device.close()

    I *must* do:

    with device_open() as device:
    device.do_something()

    Nevertheless, I _need_ to have a class
    where the device is opened in the __init__()
    and used in some methods.

    Any ideas?

    bye,

    --

    piergiorgio

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to piergiorgio.sartor.this.should.not. on Sun Nov 26 19:15:53 2023
    Piergiorgio Sartor <piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de> writes:
    The problem is I've some SDK of some device which
    provides context manager *only* classes.

    Everybody please excuse my being off topic. But it really
    make is hard for me to read messages when contractions are
    used in a way that seems wrong to me. I noticed this now for
    the third time in a post by Piergiorgio.

    I (not being a native speaker myself) think, when "have" is
    the main verb, it is not contracted. It's contracted when
    it's an auxiliary verb. So:

    I've seen this before.
    I've got another an.

    , but (note that no other verb follows "have" directly):

    I have been there.
    I have all of them.

    I wrote all of the above just by my own judgement, but now let
    me try to find something in the Web so support this: Web:

    |As far as I'm aware, verbs are usually only contracted when they are: |auxiliary, e.g. 'are' in they're leaving, and
    |unstressed (they can be attached to a stressed word but remain
    | unstressed, themselves), and |informal/casual (or formal when quoted verbatim).
    ...
    |in en-US, where they favour using 'have' as a main verb, which
    |does not get contracted:
    |"I have money"
    ...
    quoted from the World-Wide Web.

    The other two cases were:

    I've myself unclear ideas

    and

    I'll not see any reply

    .

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Stefan Ram on Sun Nov 26 20:03:55 2023
    ram@zedat.fu-berlin.de (Stefan Ram) writes:
    Everybody please excuse my being off topic. But it really
    make is hard for me to read messages when contractions are

    ... makes it hard ...

    I've got another an.

    I've got another one.

    , but (note that no other verb follows "have" directly):
    I have been there.
    I have all of them.

    Above, "have" is followed by another verb in "have been",
    so it should be eligible for a contraction there!

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Grant Edwards@21:1/5 to Dieter Maurer via Python-list on Sun Nov 26 19:43:53 2023
    On 2023-11-26, Dieter Maurer via Python-list <python-list@python.org> wrote:

    If you do not have this case (e.g. usually if you open the file
    in a class's `__init__`), you do not use a context manager.

    He knows that. The OP wrote that he wants to use <something> that can
    _only_ be used by a context manager, but he wants that usage to be
    spread over various methods of a class he's writing. So he's asking
    how to fool that <something> into working when he's not using a
    context manager.

    --
    Grnat

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Grant Edwards@21:1/5 to Grant Edwards via Python-list on Sun Nov 26 20:03:20 2023
    On 2023-11-27, Grant Edwards via Python-list <python-list@python.org> wrote:
    On 2023-11-26, Dieter Maurer via Python-list <python-list@python.org> wrote:

    If you do not have this case (e.g. usually if you open the file
    in a class's `__init__`), you do not use a context manager.

    He knows that. The OP wrote that he wants to use <something> that can
    _only_ be used by a context manager, but he wants that usage to be
    spread over various methods of a class he's writing. So he's asking
    how to fool that <something> into working when he's not using a
    context manager.

    I should probably have written "how to fool that <something> into
    working when he's not using a 'with' statement"

    --
    Grant

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Ewing@21:1/5 to Grant Edwards on Mon Nov 27 19:43:14 2023
    On 27/11/23 5:03 pm, Grant Edwards wrote:
    I should probably have written "how to fool that <something> into
    working when he's not using a 'with' statement"

    It should be possible to run the context protocol yourself.
    Something like (warning, untested):

    class MyDeviceWrapper:

    def __init__(self):
    self.cm = device_open()
    self.device = self.cm.__enter__()

    # Other methods here for doing things with
    # self.device

    def close(self):
    self.cm.__exit__(None, None, None)

    --
    Greg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Greg Ewing@21:1/5 to Stefan Ram on Mon Nov 27 19:34:04 2023
    On 27/11/23 9:03 am, Stefan Ram wrote:
    Above, "have" is followed by another verb in "have been",
    so it should be eligible for a contraction there!

    Yes, "been" is the past participle of 'to be", so "I've been" is
    fine.

    --
    Greg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Stefan Ram@21:1/5 to Greg Ewing on Mon Nov 27 10:43:40 2023
    Greg Ewing <greg.ewing@canterbury.ac.nz> writes:
    On 27/11/23 9:03 am, Stefan Ram wrote:
    Above, "have" is followed by another verb in "have been",
    so it should be eligible for a contraction there!
    Yes, "been" is the past participle of 'to be", so "I've been" is
    fine.

    In the meantime, I had asked about this topic in a newsgroup
    dedicated to English and was told that my view would apply
    to American English, but that more contractions were allowed
    in British English (and earlier in American English).

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Damon@21:1/5 to All on Mon Nov 27 12:51:27 2023
    Read the Fine context manager documentation.
    What “with with_expression as var” does is effectively:

    ob = with_expression
    var = ob.__enter__()

    And then at the end of the with, does a
    ob.__exit__()

    (With some parameters to __exit__, that could just be None, None, None for the simplest case).

    Note, YOUR program must now make sure that the __exit__ function is called, and handle any exceptions that got thrown, and that ob and var are put somewhere you can access them at that later time.


    On Nov 27, 2023, at 12:24 PM, Piergiorgio Sartor via Python-list <python-list@python.org> wrote:

    On 26/11/2023 18.50, Dieter Maurer wrote:
    Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100:
    ...
    Apparently, the "with" context manager is not usable
    in classes, at least not with __init__() & co.
    You can use `with` in classes -- with any context manager.
    However, you would usually not use `with` with a file you have opened
    in `__init__`.
    If a class defines `__enter__` and `__exit__` (i.e.
    the "cntext manager protocol"), then its instances
    can be used with the `with` statement.
    The important use case for a context manager is the
    situation:
    set up a context (--> method `__enter__`)
    perform some operations in this context (--> body of `with` statement)
    tear down the context (--> method `__exit__`).
    If you do not have this case (e.g. usually if you open the file
    in a class's `__init__`), you do not use a context manager.

    Very clear, but what if the class is *not* "open()",
    but something else _requiring_ using "with"?
    How to do this in a "__init__()" of a class?

    In other words, what if "open()" could *only* be used
    with "with" and not just by assigning "fp = open()"?

    The problem is I've some SDK of some device which
    provides context manager *only* classes.

    I *cannot* do:

    device = device_open(...)
    device.do_something()
    device.close()

    I *must* do:

    with device_open() as device:
    device.do_something()

    Nevertheless, I _need_ to have a class
    where the device is opened in the __init__()
    and used in some methods.

    Any ideas?

    bye,

    --

    piergiorgio

    --
    https://mail.python.org/mailman/listinfo/python-list

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Damon@21:1/5 to All on Mon Nov 27 12:50:42 2023
    Read the Fine context manager documentation.
    What “with with_expression as var” does is effectively:

    ob = with_expression
    var = ob.__enter__()

    And then at the end of the with, does a
    ob.__exit__()

    (With some parameters to __exit__, that could just be None, None, None for the simplest case).

    Note, YOUR program must now make sure that the __exit__ function is called, and handle any exceptions that got thrown, and that ob and var are put somewhere you can access them at that later time.


    On Nov 27, 2023, at 12:24 PM, Piergiorgio Sartor via Python-list <python-list@python.org> wrote:

    On 26/11/2023 18.50, Dieter Maurer wrote:
    Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100:
    ...
    Apparently, the "with" context manager is not usable
    in classes, at least not with __init__() & co.
    You can use `with` in classes -- with any context manager.
    However, you would usually not use `with` with a file you have opened
    in `__init__`.
    If a class defines `__enter__` and `__exit__` (i.e.
    the "cntext manager protocol"), then its instances
    can be used with the `with` statement.
    The important use case for a context manager is the
    situation:
    set up a context (--> method `__enter__`)
    perform some operations in this context (--> body of `with` statement)
    tear down the context (--> method `__exit__`).
    If you do not have this case (e.g. usually if you open the file
    in a class's `__init__`), you do not use a context manager.

    Very clear, but what if the class is *not* "open()",
    but something else _requiring_ using "with"?
    How to do this in a "__init__()" of a class?

    In other words, what if "open()" could *only* be used
    with "with" and not just by assigning "fp = open()"?

    The problem is I've some SDK of some device which
    provides context manager *only* classes.

    I *cannot* do:

    device = device_open(...)
    device.do_something()
    device.close()

    I *must* do:

    with device_open() as device:
    device.do_something()

    Nevertheless, I _need_ to have a class
    where the device is opened in the __init__()
    and used in some methods.

    Any ideas?

    bye,

    --

    piergiorgio

    --
    https://mail.python.org/mailman/listinfo/python-list

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From David Raymond@21:1/5 to All on Mon Nov 27 17:58:40 2023
    I *must* do:

    with device_open() as device:
    device.do_something()

    Nevertheless, I _need_ to have a class
    where the device is opened in the __init__()
    and used in some methods.

    Any ideas?

    Perhaps take a look at contextlib.ExitStack and see if you can do something with it.

    (Below is not tested)

    import contextlib
    class myClass:
    def __init__(self):
    self._exitStack = contextlib.ExitStack()
    device = self._exitStack.enter_context(device_open(...))
    def __del__(self):
    self._exitStack.close()

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