Python Forum
what works with the with statement?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
what works with the with statement?
#1
is there a list of everything that works with the with statement? or, alternatively, everything that does not work with the with statement? i could not find anything at all, one way or the other, regarding how a particular function behaved, so i had to set up a test script.
import os
with os.open('/dev/tty',os.O_WRONLY) as fd:
    write(fd,b'foo\n')
write(fd,b'bar\n')
i don't want to have to do this with everything i might use the with statement with.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
(Jan-02-2022, 11:19 PM)Skaperen Wrote: is there a list of everything that works with the with statement? or, alternatively, everything that does not work with the with statement? i could not find anything at all, one way or the other, regarding how a particular function behaved, so i had to set up a test script.
What you can search for is "Context Manager". I think that you'll get more information that way. I can't tell you what does or does not qualify as a context manager but if you want to have some fun fooling around with the idea, you can roll your own. Try this:
from time import time

class Timer : # This is your context manager :)
	def __enter__ (self) :
		self.start = time ()
		return self

	def __exit__ (self, *args) :
		end = time ()
		self.interval = end - self.start


with Timer () as tracker:
	for count in range (99999) :
		print ('Still working...')

print(f'The loop took {tracker.interval:.3f} seconds.')
Reply
#3
If an object has an __enter__() method and an __exit__() method, you can normally use it with the with statement.
Reply
#4
First attempt to get filename where __enter__ occours:

grep -Rl __enter__ .pyenv/versions/3.11.0a3 | egrep ".py$" | xargs -n1 basename  | egrep -v "^test|^_" | sort -u
To gather more information, you have to use some functions from inspect.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
It is more difficult to track objects returned by functions from the contextlib module, such as functions decorated by the contextmanager decorator. They are functions that return contexts.
Reply
#6
i made my zopen() class work with with statements (tested). but i was, at first, surprised that os.open() did not work with with. thinking about it, since it returns int, that makes sense.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
You could wrap it in a context manager
import contextlib
import os

@contextlib.contextmanager
def osopen(*args, **kwargs):
    fd = os.open(*args, **kwargs)
    try:
        yield fd
    finally:
        os.close(fd)

s = b'hello world'
with osopen('foo.txt', os.O_WRONLY|os.O_CREAT, 0o666) as fd:
    while s:
        n = os.write(fd, s)
        s = s[n:]
snippsat and Skaperen like this post
Reply
#8
that justifies me digging into more documentation to learn what magic decorators really do.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020