with-statement

How to check if an object is created with `with` statement?

故事扮演 提交于 2019-12-01 04:03:47
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? All answers so far do not provide what (I think) OP wants directly . (I think) OP wants something like this: >>> with X() as x: ... # ok >>> x = X() # ERROR Traceback (most recent call last): File "run.py", line 18, in <module> x = X() File "run.py", line 9, in __init__ raise Exception("Should only be used with `with`") Exception: Should only be used with `with` This is what I come up with, it may not be very

Oracle — WITH CLAUSE => MERGE? (Syntax error, )

£可爱£侵袭症+ 提交于 2019-12-01 02:58:29
I'm trying to get the WITH clause to work with merge in Oracle, but for some reason I can't get it working. I'm sure it is something obvious, but I just haven't seen it. -- behold, the wonders of fake data WITH X AS ( SELECT 'moo' AS COW, 'woof' AS CAT, (SELECT MAX( DECIBELS ) FROM ANIMALIA WHERE COW = 'moo' ) AS DECIBELS FROM DUAL ) MERGE INTO ANIMALIA D USING X WHEN MATCHED THEN UPDATE SET D.COW = X.COW; EDIT I actually found out how to manage this (before I submitted the question), but I think that since it took me quite some time to find the answer, hopefully leaving this question up will

Python 2.5.2- what was instead of 'with' statement

我的梦境 提交于 2019-12-01 02:11:32
问题 I wrote my code for python 2.7 but the server has 2.5. How do i rewrite the next code so it will run in python 2.5.2: gzipHandler = gzip.open(gzipFile) try: with open(txtFile, 'w') as out: for line in gzipHandler: out.write(line) except: pass Right now, when i try to run my script I get this error: Warning: 'with' will become a reserved keyword in Python 2.6 Traceback (most recent call last): File "Main.py", line 7, in from Extractor import Extractor File "/data/client/scripts/Extractor.py",

How sql with-recursive statement interpreted?

女生的网名这么多〃 提交于 2019-11-30 21:22:19
I would like to ask get some help about understanding how "with recursive" works. More precisely WHY the anchor query (the non-recursive term) isn't replicated into the sub call of the CTE. I tried my best to understand alone but I'm not sure. First of all let's take the example of PostgreSQL which is the simplest one I found (make the sum of 1 to 100) : WITH RECURSIVE t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 100) SELECT sum(n) FROM t; My code walkthrough ( I used links below) : Evaluate the non-recursive term. For UNION [...]. Include all remaining rows in the result of the

How “with” is better than try/catch to open a file in Python?

天大地大妈咪最大 提交于 2019-11-30 11:44:46
问题 I got that the with statement help you to turn this: try: f = open(my_file) do_stuff_that_fails() except: pass finally: f.close() Into: with open(my_file) as f: do_stuff_that_fails() But how is that better? You still got to handle the case with the file not being able to be opened (like prompting the user to tell him he doesn't have permissions), so in reality you'd have: try: with open(my_file) as f: do_stuff_that_fails() except (IOError, OSError, Failure) as e: do_stuff_when_it_doesnt_work(

Re-assign exception from within a python __exit__ block

大兔子大兔子 提交于 2019-11-30 08:48:36
From within an __exit__ block in a custom cursor class I want to catch an exception so I can in turn throw a more specific exception. What is the proper way to do this? class Cursor: def __enter__(self): ... def __exit__(self, ex_type, ex_val, tb): if ex_type == VagueThirdPartyError: # get new more specific error based on error code in ex_val and # return that one in its place. return False # ? else: return False Raising the specific exception within the __exit__ block seems like a hack, but maybe I'm over thinking it. The proper procedure is to raise the new exception inside of the __exit__

Using “with” statement for CSV files in Python

自作多情 提交于 2019-11-30 06:58:49
问题 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 reader But csv.reader doesn't provide the __enter__ and __exit__ methods, so this doesn't work. I can however do it in two steps: import csv with open("myfile.csv") as f: reader = csv.reader(f) # do things with reader Is this second way the ideal way to do it? Why wouldn't they make csv.reader

How sql with-recursive statement interpreted?

有些话、适合烂在心里 提交于 2019-11-30 05:36:38
问题 I would like to ask get some help about understanding how "with recursive" works. More precisely WHY the anchor query (the non-recursive term) isn't replicated into the sub call of the CTE. I tried my best to understand alone but I'm not sure. First of all let's take the example of PostgreSQL which is the simplest one I found (make the sum of 1 to 100) : WITH RECURSIVE t(n) AS ( VALUES (1) UNION ALL SELECT n+1 FROM t WHERE n < 100) SELECT sum(n) FROM t; My code walkthrough ( I used links

Is Python *with* statement exactly equivalent to a try - (except) - finally block?

天大地大妈咪最大 提交于 2019-11-30 04:48:33
I know this was widely discussed, but I still can't find an answer to confirm this: is the with statement identical to calling the same code in a try - (except) -finally block, where whatever one defines in the __exit__ function of the context manager is placed in the finally block? For example -- are these 2 code snippets doing exactly the same thing? import sys from contextlib import contextmanager @contextmanager def open_input(fpath): fd = open(fpath) if fpath else sys.stdin try: yield fd finally: fd.close() with open_input("/path/to/file"): print "starting to read from file..." the same

What does “with” do in JavaScript?

社会主义新天地 提交于 2019-11-30 03:46:17
I saw JavaScript code which begins with with . That's a bit confusing. What does it do and how can it be used correctly? with (sObj) return options[selectedIndex].value; Justin Niessner It adds to the scope of the statements contained in the block: return sObj.options[selectedIndex].value; can become: with (sObj) return options[selectedIndex].value; In your case, it doens't do a whole lot...but consider the following: var a, x, y; var r = 10; a = Math.PI * r * r; x = r * Math.cos(PI); y = r * Math.sin(PI /2); Becomes: var a, x, y; var r = 10; with (Math) { a = PI * r * r; x = r * cos(PI); y =