Scope of the variable in & ldquo; with & quot; declaration?

I am reading only firstline from python using : with open(file_path, 'r') as f: my_count = f.readline() print(my_count) I am bit confused over scope of variable my_count. Although prints work fine, would it be better to do something like my_count = 0

How to open more than 19 files in parallel (Python)?

I have a project that needs to read data, then write in more than 23 CSV files in parallel depending on each line. For example, if the line is about temperature, we should write to temperature.csv, if about humidity, >>to humid.CSV , etc. I tried th

reopening of sys.stdout after it closes with with statement

I'm having trouble with a printing information that is input from a yaml file using PyYAML. I'm trying to reduce line-count, without affecting functionality. In some runs the output has to be appended to a file, in others to stdout. At first I used t

Hide post-function calls with an instruction

I have a function f that returns two parameters. After this function, I use the first parameter. I need the second one to glue things together with another function g. So my code would look like: a, b = f() # do stuff with a g(b) This is very repetit

Is it a good idea to use the statement with context?

If I have a rendering function in my game for example, that is called at a pretty high rate (determined by requestAnimationFrame), like this for example: function render() { ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.beginPath(); ctx.arc(p

Why the context manager is slow

A coworker pointed out to me, that with statement can be slow. So I measured and indeed it takes 20 times longer to get value from a contextmanager function than from a generator in Python 2.7 and even 200 times longer in PyPy 2.6. Why is it so? Is i

SQL Server: With in Select in NOT IN

I Have problem with this code. I have tree in my data base and i need to select all nodes except The Node and his children, grandchilder etc. I done WITH query - it works. I can select all big family of The Node. But when i try to do SELECT ... WHERE

Python Compare two CSV files line by line

I currently have a version of a script that compares two csv files by reading both into a list/set one after the other. However, the csvs are currently too large for memory, so I would like to iterate line by line and print out lines which are differ

Context Manager for optional redirected I / O

I'm often faced with the situation that depending on some command line argument, input might either come from a file or standard input. The same goes for output. I really like how context managers in python 3 work, and therefore try to make all my op

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 r2.time<

Is it possible to refer to Rg in With Rg

Is it possible to refer to Rg in With Rg? I have a long with statement and I would like to pass the range specified in the With statement as a parameter. Is this possible? With rg.OffSet(0, -1).Resize(ColumnSize:=1) 'set conditional format Call SetCo

Python 'with' instruction, should I use contextlib.closing?

from contextlib import closing def init_db(): with closing(connect_db()) as db: with app.open_resource('schema.sql') as f: db.cursor().executescript( db.commit() This is from flask tutorial Step 3(

Can I acquire Multiprocessing Lock in a with statement?

Horrible things can happen if a process fails to unlock a multiprocessing lock. To minimize the chance of that happening, I want to acquire the lock in a with block. Is there any built in way I can do this or do I need to roll my own?Yes, you can jus

Delete consecutive duplicates in R

How to remove consecutive duplicate entries in R? I think with may be used but can't think how to use it. Illustrating one example: read.table(text = " a t1 b t2 b t3 b t4 c t5 c t6 b t7 d t8") Sample Data: D events time a t1 b t2 b t3 b t4 c t5

Python 'with' statement and its use in a class

I have a TestCase which uses sockets for testing purposes. As you should always close a socket even in the case of some error, I created a context-manager class abstracting a socket. A test fixture would look like the following, where MyClassToTest i

Update the manager table tree by recurrence

I have a table (see image) Employees (manager is manager of another and so on) with id, parentid, salary, totalsalary. The last one needs to be updated so that every employee had Sum of its Descendants salary. I have already written script, which get

Use WITH SQL Query with PHP

I have tried to rewrite the below query, so that it will work in a PHP page, but I have not been able to find any documentation on how the ";WITH" statement can be used in PHP, or how to make the same effect with a valid PHP Query.. The Query wo

PostgreSQL with RECURSIVE performance

I have a simple question. Somehow I was unable to find a definitive answer. How much is WITH RECURSIVE syntax optimized in PostgreSQL? By that I mean: is it merely a syntactic sugar for a series of non recursive queries, OR is it more of a single sta