with-statement

with statement - backport for Python 2.5

I'd like to use with statement in Python 2.5 in some production code. It was backported, should I expect any problems (e.g. with availability/compatibility on other machines/etc)? Is this code from __future__ import with_statement compatible with Python 2.6? ...

Nesting 'WITH' statements in Python

Turns out 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 script I've been writing and I suspect that it's because I'm doing this: with open(file1) as fsock1: with open(file2, 'a') as fsock2: fstri...

VB.NET 'With' statement performance?

What's the performance consequence using the 'With' keyword in vb.net instead of using reusing the instance name over and over? ...

Python: defining new functions on the fly using "with"

I want to convert the following code: ... urls = [many urls] links = [] funcs = [] for url in urls: func = getFunc(url, links) funcs.append(func) ... def getFunc(url, links): def func(): page = open(url) link = searchForLink(page) links.append(link) return func into the much more convenient code: urls =...

Does C++ have "with" keyword like Pascal?

with keyword in Pascal can be use to quick access the field of a record. Anybody knows if C++ has anything similar to that? Ex: I have a pointer with many fields and i don't want to type like this: if (pointer->field1) && (pointer->field2) && ... (pointer->fieldn) what I really want is something like this in C++: with (pointer) { ...

What is wrong with this Jquery function, help

$(function() { $("table.section thead").click(function() { if ($(this).next("table.section tbody").style.display == "block"){ $(this).next("table.section tbody").slideUp("slow"); } if ($(this).next("table.section tbody").style.display == "none"){ $(this).next("table.section tbody").slideDown("slow"...

VB.NET Use With keyword on existing object?

Is it somehow possible to use the 'With' keyword on an existing object? I would like to do the following using LINQ to objects and can't seem to find a way: From m as Product in Me _ Select m With {.Match = m.Name.IndexOf(query)} _ Where m.Name.IndexOf(query) > 0 ...

What's the advantage of using 'with .. as' statement in Python?

with open("hello.txt", "wb") as f: f.write("Hello Python!\n") seems to be the same as f = open("hello.txt", "wb") f.write("Hello Python!\n") f.close() What's the advantage of using open .. as instead of f = ? Is it just syntactic sugar? Just saving one line of code? ...

"with" scope and properties with square bracket notation

Is it possible to access object properties that can only be accessed with the square bracket notation when inside a "with" statement. Example: var o = { "bad-property": 1, "another:bad:property": 2, "goodProperty": 3 }; with(o) { console.log(goodProperty); // works awesome console.log(???) // how to access "bad:property"? } ...

Javascript Sandbox

I want to have developers write some custom apps for a site in Javascript but I want to sandbox it so they can't do anything naughty like redirect the user, set the body display to none etc etc. I have a namespace in Javascript where all the functions they'll ever need exist in there so I was thinking to create a sandbox would be a matte...

What is the python "with" statement designed for?

I came across the Python with statement for the first time today. I've been using Python lightly for several months and didn't even know of its existence! Given its somewhat obscure status, I thought it would be worth asking: What is the Python with statement designed to be used for? What do you use it for? Are their any gotchas ...

python: create a "with" block on several context managers

Suppose you have three objects you acquire via context manager, for instance A lock, a db connection and an ip socket. You can acquire them by: with lock: with db_con: with socket: #do stuff But is there a way to do it in one block? something like with lock,db_con,socket: #do stuff Furthermore, is it possib...

How does the WITH statement store the record set for the select query?

Consider the following SQL (SQL Server 2008) statement: WITH MyResult AS ( SELECT Name, Row_ID AS ORD FROM Person Where Gender = @Gender ) SELECT * FROM MyResult WHERE ORD > 5 Is MyResult stored in a temporary table in the tempdb? Or does it do something else? We are optimising some queries and would lke to better unders...

How come eval doesn't have access to the scoped variables under a with statement?

Why can't you access scoped variables using eval under a with statement? For example: (function (obj) { with (obj) { console.log(a); // prints out obj.a eval("console.log(a)"); // ReferenceError: a is not defined } })({ a: "hello" }) EDIT: As the knowledgeable CMS pointed out, this appears to be a browser bug (brow...

Dealing with context classes in Python 2.4

I'm trying to use the python-daemon module. It supplies the daemon.DaemonContext class to properly daemonize a script. Although I'm primarily targeting Python 2.6+, I'd like to maintain backwards compatibility to version 2.4. Python 2.5 supports importing contexts from future, but Python 2.4 has no such facility. I figured I could just ...

Using python "with" statement with try-except block

Is this the right way to use the python "with" statement in combination with a try-except block?: try: with open("file", "r") as f: line = f.readline() except IOError: <whatever> If it is, then considering the old way of doing things: try: f = open("file", "r") line = f.readline() except IOError: <whatever...

Is there any research on (or better use of) of RAII in GC languages?

Note: Object Lifetime RAII not using/with block scope RAII It seems like its possible using an extra gc category, short lived objects(check gc category somewhat frequently), long lived objects(check gc category less frequently), and resource objects(check gc category very frequently). Or possibly with an extra reference counting gc for ...

Operating on a file's content despite a failure in the 'with' block

I've just written a utility in Python to do something I need (irrelevant, but it's to generate a ctags-compatible tag file for an in-house DSL). Anyway- I'm opening and reading the file in the context of a with statement, and I'm curious, how do people tend to handle failures in that process? My solution is with open(filename, 'rt') a...

Implementing use of 'with object() as f' in custom class in python

I have to open a file-like object in python (it's a serial connection through /dev/) and then close it. This is done several times in several methods of my class. How I WAS doing it was opening the file in the constructor, and then closing it in the destructor. I'm getting weird errors though and I think it has to do with the garbage col...

equivalent of Python's "with" in Ruby

In Python, the with statement is used to make sure that clean-up code always gets called, regardless of exceptions being thrown or function calls returning. For example: with open("temp.txt", "w") as f: f.write("hi") raise ValueError("spitespite") Here, the file is closed, even though an exception was raised. A better explanat...