with-statement Questions
7
Solved
Just learning about with statements especially from this article
question is, can I pass an argument to __enter__?
I have code like this:
class clippy_runner:
def __enter__(self):
self.engine = E...
Lepley asked 24/2, 2011 at 19:34
6
Solved
I am trying to understand the with statement. I understand that it is supposed to replace the try/except block.
Now suppose I do something like this:
try:
name = "rubicon" / 2 # to raise...
Electromagnet asked 12/9, 2010 at 4:40
11
Solved
How do I test the following code with unittest.mock:
def testme(filepath):
with open(filepath) as f:
return f.read()
Frannie asked 17/8, 2009 at 19:26
4
Solved
I am trying to implement something similar to the Python with statement in C++. As I plan to use it mainly with Qt-OpenGL the methods are called bind and release (in Python __enter__, __exit__).
Co...
Pictograph asked 15/7, 2012 at 11:31
2
Solved
Classes have a defineable function __exit__ that allows implementation of a context manager.
It takes the required arguments:
def __exit__(self, exc_type, exc_val, exc_tb):
but I cannot find a ...
Opportina asked 11/11, 2019 at 20:11
2
Solved
Is there a way to use multiple WITH statements in Athena/Presto?
WITH "revenue" AS (
SELECT
"cik",
"accession",
"year",
"quarter",
"for...
Haplosis asked 15/1, 2022 at 23:22
11
Solved
I came across the Python with statement for the first time today. I've been using Python lightly for several months and didn't even know of its existence! Given its somewhat obscure status, I thoug...
Densmore asked 10/6, 2010 at 7:35
5
Trying to properly delete a Python object. I'm creating an object and then supposedly deleting it with a 'with' statement. But when I do a print out after the 'with' statement is closed.... the obj...
Toffee asked 15/3, 2016 at 4:28
4
Solved
I was using this answer in order to run parallel commands with multiprocessing in Python on a Linux box.
My code did something like:
import multiprocessing
import logging
def cycle(offset):
# ...
Coupling asked 14/8, 2017 at 0:17
13
Solved
I'd just like to exit out of a with statement under certain conditions:
with open(path) as f:
print 'before condition'
if <condition>: break #syntax error!
print 'after condition'
Of co...
Clavicle asked 25/6, 2012 at 18:30
5
Solved
I have the following code:
class Test:
def __init__(self, name):
self.name = name
def __enter__(self):
print(f'entering {self.name}')
def __exit__(self, exctype, excinst, exctb) -> bool:...
Safar asked 16/9, 2021 at 20:14
3
Solved
Instead of using:
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute(...)
c.close()
would it be possible to use the Pythonic idiom:
with conn.cursor() as c:
c.execut...
Inshore asked 25/11, 2018 at 20:34
1
Solved
So I have this simple example of a with statement.
It works in Python 3.8 and 3.9:
class Foo:
def __enter__(self, *args):
print("enter")
def __exit__(self, *args):
print("exit&q...
Clarisaclarise asked 25/8, 2021 at 14:29
1
I am running code which after sometimes hours, sometimes minutes fails with the error
OSError: [Errno 24] Too many open files
And I have real trouble debugging this. The error itself is always tri...
Coruscate asked 14/8, 2021 at 14:9
19
Solved
with keyword in Pascal can be use to quick access the field of a record.
Anybody knows if C++ has anything similar to that?
Ex:
I have a pointer with many fields and i don't want to type like thi...
Unlive asked 17/2, 2010 at 8:19
2
Solved
I'm familiar with using python's with statement as a means of ensuring finalization of an object in the event of an exception being thrown. This usually looks like
with file.open('myfile.txt') as ...
Confab asked 13/10, 2014 at 14:46
5
Solved
What is the OCaml counterpart to Python's "with"-statement?
with open('test.txt', 'r') as f:
# Do stuff with f
# At this point, f will always be closed, even in case of exceptions
That is: What...
Nostrum asked 11/6, 2018 at 9:3
3
Solved
I came across this as a bit of a surprise while trying to work out another question.
This seemed extremely odd to me, I thought it was worth asking the question. Why doesn't __getattr__ appear to ...
Confutation asked 28/9, 2012 at 2:40
2
Many of R's functions with non-standard evaluation, e.g. with, subset, and transform, contain a warning like this:
For interactive use this is very effective and nice to read. For programming howe...
Alcoholic asked 1/4, 2021 at 15:13
1
Solved
Consider:
notBroken<-within(mtcars, {
gear<-as.factor(gear)
cyl<-as.factor(cyl)})
str(notBroken)
Our output,
> str(notBroken)
'data.frame': 32 obs. of 11 variables:
$ mpg : num 21 2...
Unclasp asked 25/3, 2021 at 0:12
5
Solved
Python 3.4 provides this neat tool to temporarily redirect stdout:
# From https://docs.python.org/3.4/library/contextlib.html#contextlib.redirect_stdout
with redirect_stdout(sys.stderr):
help(pow...
Meyers asked 11/5, 2014 at 20:41
8
Solved
Is it possible to declare more than one variable using a with statement in Python?
Something like:
from __future__ import with_statement
with open("out.txt","wt"), open("in.txt") as file_out, fi...
Maretz asked 21/5, 2009 at 14:51
3
Solved
Suppose we have the following mod.py:
def __enter__():
print("__enter__<")
def __exit__(*exc):
print("__exit__< {0}".format(exc))
class cls:
def __enter__(self):
print("cls.__enter__&l...
Jenks asked 15/11, 2016 at 9:9
7
Solved
I saw this in someone's code. What does it mean?
def __enter__(self):
return self
def __exit__(self, type, value, tb):
self.stream.close()
Here is the complete code.
from __future__ import wi...
Aurum asked 31/12, 2009 at 7:7
5
I'm looking to encapsulate logic for database transactions into a with block; wrapping the code in a transaction and handling various exceptions (locking issues). This is simple enough, however I'd...
Multiplier asked 4/6, 2013 at 13:46
© 2022 - 2024 — McMap. All rights reserved.