Questions tagged [contextmanager]

A python context manager manages the context of a with statement. A context manager defines enter and exit hooks that get called as the code block under the with statement is entered and exited, respectively.

Python’s creates a runtime context, defined by a context manager. As the code block under the with statement is entered, a __enter__ hook is called on the context manager, and as it is exited (by any means, including exceptions and return statements), the __exit__ hook is called.

Python provides several standard context managers. File objects, for example, can be opened as a context manager, and on exit the file is automatically closed.

Context managers were defined in PEP 343.

613 questions
-1
votes
1 answer

How to modify the argument taken from a contextmanager within the with block?

I am trying to modify on the fly some of the parameters used by the exit function of a context manager. I am trying to bind the parameter to a variable known in the with block from contextlib import contextmanager import tempfile,…
Llopeth
  • 406
  • 5
  • 11
-1
votes
1 answer

Using a "with" contextmanager with pysqlite

I'm trying to use the "with" contextmanager using pysqlite: >>> with conn.cursor() as db: res = db.execute("SELECT * FROM Publishers LIMIT 5;").fetchall() ... ... Traceback (most recent call last): File "", line 1, in…
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
-1
votes
1 answer

Why is @contextmanager used only for self-contained functions?

I am reading the Python Cookbook where the following is mentioned: @contextmanager is really only used for writing self-contained context-management functions. If you have some object (e.g. a file, network connection, or lock) that needs to…
debashish
  • 1,374
  • 1
  • 19
  • 29
-1
votes
1 answer

In python is there a way to make a function/class that behaves like a function and a context manager?

In python is there a way to make a function/class that behaves like a function and a context manager? Note: that I need the function/class to return an object that doesn't have a __exit__ method and I cant change that object (that's why I am…
moshevi
  • 4,999
  • 5
  • 33
  • 50
-1
votes
1 answer

decompose "with" statements to various functions

I wrote a generic framework that help me to bench-mark code critical sections. Here is an explanation of the framework and in the end is the problem I am facing and few ideas I have for solutions. Basically, I am looking for more elegant…
Lior Cohen
  • 5,570
  • 2
  • 14
  • 30
-1
votes
2 answers

Why does my context manager __exit__ function run before the computation isn't finished?

The exit function of my custom context manager seemingly runs before the computation is done. My context manager is meant to simplify writing concurrent/parallel code. Here is my context manager code: import time from multiprocessing.dummy import…
jamis
  • 247
  • 3
  • 10
-1
votes
2 answers

How to omit code block based on arguments to context manager?

Lets's see this example: with mycontextmanager(arg1='value', arg2=False): print 'Executed' Is there a way to not execute code block (print 'Executed') within a context manager based on argument, eg: when arg2 is not False?
canni
  • 5,737
  • 9
  • 46
  • 68
-1
votes
2 answers

PEP343 'with' context manager and django

I am doing some application testing with django frame work , i have a case where i test if inactive users can login , and i do like so self.testuser.is_active = False //DO testing self.testuser.is_active = True //Proceed my question is , by…
AmOs
  • 129
  • 1
  • 13
-2
votes
2 answers

Python conditional fails to reach the else statement when the if statement is not satisfied

try: symbol = input("Specify symbol: ").upper() dt_string = input("Specify datetime in isoformat (e.g.'2021-05-27T03:30:00+00:00'): ") try: dt = datetime.fromisoformat(dt_string) except…
-2
votes
1 answer

Keep context manager open after an if ... else statement

I have the following scheme, with different conditional with statements: if not remote: _open = open os.chdir(localpath) else: sftp = pysftp.Connection(host, username=user, password=sftppwd) with sftp: sftp.chdir(remotepath) …
Basj
  • 41,386
  • 99
  • 383
  • 673
-2
votes
2 answers

How to make a python class a context manager

I define my class like this: class Simple(): def __init__(self): self.string = "Hello World" def __enter__(self): pass def __exit__(self): pass and call it like this: with Simple() as simple_test: …
ckemere
  • 59
  • 6
-2
votes
1 answer

One-line syntax to read and return file on Python using with

I need to read a file and return result: this is the syntax I use return json.loads(with open(file, 'r') as f: f.read()) I know that we cannot write with open in one line, so I look for the correct syntax to fix that.
HISI
  • 4,557
  • 4
  • 35
  • 51
-2
votes
1 answer

Returning a value from a SQLAlchemy context manager

I am trying to return the value of my SELECT query from my context manager. However, nothing is returned as a response. How can I return the results of my select query from my context manager/session? @contextmanager def aperio_session_scope(): …
RubyJ
  • 193
  • 2
  • 16
1 2 3
40
41