with-statement Questions

3

I have a long-running process called an Updater, that has updates (to an ETL system) submitted to it. The updates have resource requirements that are managed by adding a context manager to the Upda...
Protocol asked 3/6, 2016 at 5:56

6

Solved

Is it possible to use the with statement directly with CSV files? It seems natural to be able to do something like this: import csv with csv.reader(open("myfile.csv")) as reader: # do things with...
Remarkable asked 13/1, 2009 at 22:36

6

Solved

I am trying to do some shared locking using with statements def someMethod(self, hasLock = False): with self.my_lock: self.somethingElse(hasLock=True) def somethingElse(self, hasLock = False):...
Saenz asked 3/3, 2011 at 19:34

7

Solved

I would like to ensure that the class is only instantiated within a "with" statement. i.e. this one is ok: with X() as x: ... and this is not: x = X() How can I ensure such functionality?
Stercoricolous asked 14/2, 2015 at 15:41

3

Solved

I am unsuccessfully trying to get the magic with-statement methods __enter__ and __exit__ running on class-level: class Spam(): @classmethod def __enter__(cls): return cls @classmethod def ...
Holly asked 4/2, 2015 at 20:5

4

Solved

I'm often in the need of temporarily switching the value of a variable out with something else, do some computation which depends on this variable, and then restore the variable to its original val...
Kish asked 4/1, 2017 at 1:1

8

Solved

Is it possible to ensure the __exit__() method is called even if there is an exception in __enter__()? >>> class TstContx(object): ... def __enter__(self): ... raise Exception('Oops in _...
Cognizant asked 25/10, 2012 at 18:25

9

Solved

I am struggling to make a piece of code that allows to measure time spent within a "with" statement and assigns the time measured (a float) to the variable provided in the "with" statement. import...
Fluoro asked 29/11, 2015 at 19:29

7

Solved

What is a clean way to create a multi-line with in python? I want to open up several files inside a single with, but it's far enough to the right that I want it on multiple lines. Like this: class...
Acclimate asked 24/6, 2015 at 23:49

2

Solved

Im using PL/SQL. I am trying to have a for loop right after I define my temporary tables in the with clause. However, Im getting an error to have a SELECT query first. For instance WITH TMP1 AS (...
Livonia asked 30/5, 2016 at 4:47

9

Solved

I am defining a context manager class and I would like to be able to skip the block of code without raising an exception if certain conditions are met during instantiation. For example, class My_C...
Hydrogeology asked 26/9, 2012 at 3:28

5

Below is an example of my my_create method, and an example of that method in use. @contextmanager def my_create(**attributes): obj = MyObject(**attributes) yield obj obj.save() with my_create...
Cholula asked 2/2, 2018 at 22:28

8

Solved

Instead of this: file = open(f) do_something(file) file.close() it's better to use this: with open(f) as file: do_something(file) What if I have something like this? if f is not None: file = op...
Ophthalmitis asked 28/8, 2012 at 22:7

17

I've heard many programmers, particularly Delphi programmers scorn the use of 'with'. I thought it made programs run faster (only one reference to parent object) and that it was easier to read th...
Twoup asked 16/9, 2008 at 11:35

3

Solved

After reading this : How do I mock an open used in a with statement (using the Mock framework in Python)? I'm able to mock the open function in python using : with patch(open_name, create=True) a...
Ouellette asked 19/2, 2012 at 12:26

3

Solved

New to unittest and Python in general, came across example in a tutorial introduction to unit testing wherein a with statement is used to catch a ValueError. The script being tested (invoice_calc...

3

Solved

Are these two statements equivalent? with A() as a, B() as b: # do something with A() as a: with B() as b: # do something I ask because both a and b alter global variables (tensorflow here) ...
Elsey asked 31/3, 2017 at 1:33

10

Solved

Is there a way to begin a block of code with a with statement, but conditionally? Something like: if needs_with(): with get_stuff() as gs: # do nearly the same large block of stuff, # involving...

6

Solved

I've been trying to find RAII in Python. Resource Allocation Is Initialization is a pattern in C++ whereby an object is initialized as it is created. If it fails, then it throws an exception. In th...
Aborigine asked 21/2, 2011 at 20:50

1

cx_Oracle contains __enter__ and __exit__ on Connection objects, but not on Cursor objects. Thus, I use this everywhere to wrap cursors : class CursorWrapper(object): def __init__(self, connection...
Frech asked 11/12, 2015 at 16:15

4

Solved

I have some legacy code with a legacy function that takes a filename as an argument and processes the file contents. A working facsimile of the code is below. What I want to do is not have to writ...
Marquittamarr asked 9/8, 2012 at 22:2

4

Solved

The following is listed as example in pymysql: conn = pymysql.connect(...) with conn.cursor() as cursor: cursor.execute(...) ... conn.close() Can I use the following instead, or will this leav...
Drachm asked 3/7, 2015 at 22:2

6

Solved

It turns out that "with" is a funny word to search for on the internet. Does anyone knows what the deal is with nesting with statements in python? I've been tracking down a very slippery bug in a ...
Pare asked 2/1, 2010 at 2:18

3

I am using a Python 3 sequence like this: lock = threading.Lock() res = lock.acquire(timeout=10) if res: # do something .... lock.release() else: # do something else ... I would prefer to use...
Synonym asked 24/5, 2013 at 17:0

4

I always open and write into files using with statement: with open('file_path', 'w') as handle: print >>handle, my_stuff However, there is one instance where I need to be able to be more ...
Carbonyl asked 8/3, 2014 at 3:9

© 2022 - 2024 — McMap. All rights reserved.