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?
...
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...
What's the performance consequence using the 'With' keyword in vb.net instead of using reusing the instance name over and over?
...
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 =...
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)
{
...
$(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"...
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
...
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?
...
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"?
}
...
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...
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 ...
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...
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...
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...
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 ...
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...
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 ...
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...
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...
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...