Questions tagged [with-statement]

A number of languages have With statements. The Python with statement creates a new context, with associated context manager. When the context (a code block) is exited again, the context manager is notified. Please use "common-table-expression" for the SQL WITH construction.

A number of languages have With statements. Python's with statement creates a runtime context, defined by a . 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.

Please use for the SQL WITH statement

1119 questions
0
votes
2 answers

How to use "with" and "tapply" to calculate a new variable based on multiple factors

I'm trying to obtain the mean "ctrlmeans" of the telephone handle time "Handle" of a single group "Actrl" based on another a variable "Period". I then want to create a new variable "Difference" by subtracting that mean from the "Handle" of each…
user3594490
  • 1,949
  • 2
  • 20
  • 26
0
votes
1 answer

using with as and nesting sql

So I have a Q that is like this: with t1 as ( a.col1 as 'c1', a.col2 as 'c2', b.col1 as 'c3', b.col2 as 'c4' from table1 a left join table2 b on a.col1 = b.col1 ) select c.c1, c.c2, c.c3, c.c4 from t1 c and I want to make this whole thing a…
user3486773
  • 1,174
  • 3
  • 25
  • 50
0
votes
1 answer

Syntax error with fout

I'm having an issue with my script. The error I'm getting below is File "./filter.py", line 12 with open('test.txt') as f: ^ SyntaxError: invalid syntax The code that I'm using below is this. with open('test.txt') as f: for row in f: …
Shifty
  • 3
  • 2
0
votes
2 answers

How to get the "result" from a contextmanager using 'with'

I found a demo of a file locking class (here: https://groups.google.com/forum/#!topic/pug-pe/mQr7KX-cenU), but I don't quite understand the mechanics of using it. @contextmanager def FileLock(lock_file): if os.path.exists(lock_file): …
David Doria
  • 9,873
  • 17
  • 85
  • 147
0
votes
2 answers

Neo4j: WITH and HAVING in the same query?

I am trying to understand if I can perform a query with Neo4j that contains both WITH and HAVING clauses. I have this so far: MATCH (n)-[r:RELATIONSHIP*1..3]->(m) SET m:LABEL WITH m MATCH (m:LABEL)-[r2:RELATIONSHIP]->(q:OTHERLABEL) WHERE…
bobo32
  • 992
  • 2
  • 9
  • 21
0
votes
0 answers

How to use WITH in an inner query

I have used common table expression in an inner query but it is throwing error Msg 319 near WITH. It asks me to use ; before it, but when I use the ; before WITH, it will throw syntax error. Can anybody help? I am using sql server. insert…
Reeya Oberoi
  • 813
  • 5
  • 19
  • 42
0
votes
1 answer

removing non-matching rows based on order in SQL within a WITH statement

I've got a many-to-many setup where there are items and item names(based on languageID) I want to retrieve all names for a set id, where the name is replace with an alternate name (same itemID, but different languageID) when name is NULL. I've set…
Daniel
  • 34,125
  • 17
  • 102
  • 150
0
votes
3 answers

With statement destructor to catch init exceptions

I use a with statement with the following class. def __init__(self): ... def __enter__(self): return self def __exit__(self, type, value, traceback): print "EXIT Shutting the SDK down" ret = self.sdkobject.ShutDown() …
Marmstrong
  • 1,686
  • 7
  • 30
  • 54
0
votes
1 answer

Catching separatly IOerror when opening multiple files using with

I know that I can open multiple file using the with statement. My problem is that I can't catch errors separately. My function has a argument and an optional argument. If the "regular" is not given, it should throw an IOError, but if the optional…
user2024449
0
votes
1 answer

Can Context Manager and Object Oriented be used effectively into a GAE Request Handler class

Can I use context manager inside a Request Handler to manage the flow of my variables from the URL to the template? I started from this example here I was thinking to use it to create a class instance at the top of the handler, to run all the code I…
tuned
  • 1,085
  • 10
  • 14
0
votes
1 answer

Get file objects inside a scope of "with" block

For example there's with statement: with open("ACCELEROMETER", 'w') as ACCELEROMETER,\ open('GPS', 'w') as GPS,\ open('LIGHT', 'w') as LIGHT,\ open('LOCATION', 'w') as LOCATION,\ open('MIC', 'w') as MIC,\ open('SCREEN', 'w') as…
rok
  • 9,403
  • 17
  • 70
  • 126
0
votes
1 answer

Python: with statement and execption handling

I give following code snippet, As at the end of the code I am getting blank output file in with context when exception is raised The file is closed and again overridden in next iteration with open('output', 'w') as f: try: for i in…
Nikhil Rupanawar
  • 4,061
  • 10
  • 35
  • 51
0
votes
3 answers

what is the best way to select the first query that returns a result in DB2

I want to implement this type of logic, using DB2: Create Procedure sp_MyProcedure ( IN Var1 Decimal(6, 0), IN Var2 Decimal(6, 0) ) Language SQL Dynamic Result Sets 1 Begin If Exists(Select * from MyTable where CustomerNbr = Var1) Then …
rogdawg
  • 687
  • 3
  • 11
  • 33
0
votes
1 answer

WITH behavior - PostgreSQL

This question is maybe noobish but i have not found direct answer for it. How does with behave? If i have WITH tmp AS (...) and then i use tmp 2 times in single query will tmp behave like macro in C or will it finish first time and rest in cache…
Rouz
  • 1,247
  • 2
  • 15
  • 37
0
votes
1 answer

Oracle "with .. as (select ...)" not work in database version lower than 11203

I ran following code in 11203 Oracle database, it worked fine without errors: WITH xtimes (xdate) AS ( SELECT to_date('22-OCT-13 21:12:23','DD-MON-RR HH24:MI:SS') xdate FROM dual UNION ALL SELECT xdate+(15/1440) FROM xtimes WHERE…
Amos
  • 23
  • 5