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
-2
votes
2 answers

How to use with statement in lambda expression for Python

I am using Python 3.5.2 (the one used for MS SQL Server Machine Learning Services). I would like to have a lambda expression that uses with so not to repeat an expensive calculation in a tuple. A minimal example is lambda x : with x**2 as x2,…
Edmund
  • 488
  • 4
  • 21
-2
votes
2 answers

automatically closing a file in python3?

Am I right in thinking that this will automatically close a file? def get_file(): with open("file.csv", "rb") as f: yield f f = get_file() do_stuff(f) If not, how do i write a function that returns a file object, whilst making sure…
nz_21
  • 6,140
  • 7
  • 34
  • 80
-2
votes
1 answer

SQL INSERT statement including a WITH and SELECT

I am looking to run an INSERT into a table using a WITH CLAUSE before the SELECT. I tried putting the WITH before the INSERT and before the SELECT but I am SQL is not liking the formatting.
-2
votes
1 answer

What's the use of the "with" statement in Python?

While programming in Python, sometimes I need to launch functions and object methods, something like this: obj1.launch_method1(); // object method do_something(); // general function obj1.launch_method2(); // object method Using the with…
Dominique
  • 16,450
  • 15
  • 56
  • 112
-2
votes
1 answer

Code equivalence in C# and Visual Basic and VB code structure

I am fairly new to C# and VB coding. I need to create .Net framework describing chemical reaction as an extension so that I can reuse it in HYSYS. There are tutorials from 2004 which has a tutorial implementation in VB. I was trying to convert the…
-2
votes
1 answer

For loop - running 1 loop to completion then running next loop python

this script needs to run all the way through RI_page_urls.csv, then run through all the resulting urls from RI_License_urls.csv and grab the business info. it's pulling all the url's from RI_page_urls.csv, but then only running and printing the…
RobK
  • 123
  • 1
  • 10
-2
votes
2 answers

in python 3.4, readline works fine alone, but dies in a 'with open' loop. Why?

infile = 'xyz.txt' f = open(infile) line = f.readline() # these lines are all read fine print("line=",line) line = f.readline() print("line=",line) line = f.readline() print("line=",line) pause() f.close() with open(infile) as f: line =…
user1067305
  • 3,233
  • 7
  • 24
  • 29
-2
votes
1 answer

What does python with statement do when opening files?

I think that with open('file.txt','r') as f: pass closes file f, but, how can I prove it? My colleague thinks it will flush the file if it is open for writing.
silvermangb
  • 355
  • 2
  • 11
-2
votes
2 answers

Calculate means of columns in a sequence for each row

I'm new to R. I need to calculate means of variable for a regular interval for each individual. I have this simple data frame. df = data.frame(id=c("A","B","C","D"), x1=c(3,5,7,2), x2= c(5,3,7,3), x3=c(5,6,4,4), x4=c(5,3,7,3), …
sriya
  • 179
  • 1
  • 2
  • 7
-2
votes
1 answer

Modding a (user made) function in vba called LINTERP

TL;DR: There are two functions below. They perform the same task, but the top one is supposed to accept a starting point for a range and a count instead of the bottom one which needs a fixed range. The problem is that the top function does not work…
Stian
  • 187
  • 4
  • 12
-2
votes
1 answer

recursive common table expression with a view

This is what Im trying to do: Create view vDetailsCommunications as WITH Tickets AS ( SELECT CallLog.CallID , CallLog.RecvdDate , Detail.ReqEffDate , Asgnmnt.DateAcknow , Asgnmnt.DateResolv ,…
-2
votes
1 answer

Use a WITH in an SQL function?

I'm trying to make a function but I do not know how to get the WITH in it. Here is my code. CREATE FUNCTION CubicVolume (@StartDate date, @EndDate date) RETURNS @TableDays TABLE (Days int) AS BEGIN INSERT @TableDays WITH Dates AS ( …
-2
votes
2 answers

How to use `Me` in vb.net With...End With block

With this code "VB.Net 2005" Dim dep as new Department With dep.AddNewEmployee() .FirstName = "Mr. A" .LastName = "B" If TypeOf {dep.AddNewEmployee()'s instance} is Serializable then 'Do something End If end With in…
Bear0x3f
  • 142
  • 1
  • 16
-2
votes
3 answers

Inability to delete file that was opened (and closed?) using 'with'

How this file can be closed. Any idea? with open('output.txt','w', encoding='UTF-8') as output: output.writelines(str(i)+'\n' for i in range(5))
indiag
  • 233
  • 1
  • 4
  • 10
-3
votes
2 answers

How to replace `with` statement in strict mode

This code works optimally and is easily comprehensible: function evalInScope(js, contextAsScope) { //# Return the results of the in-line anonymous function we .call with the passed context return function() { with(this) { …
kungfooman
  • 4,473
  • 1
  • 44
  • 33
1 2 3
74
75