with-statement

Python 'with' command

你。 提交于 2019-12-10 09:19:20
问题 Is this code with open(myfile) as f: data = f.read() process(data) equivalent to this one try: f = open(myfile) data = f.read() process(f) finally: f.close() or the following one? f = open(myfile) try: data = f.read() process(f) finally: f.close() This article: http://effbot.org/zone/python-with-statement.htm suggests (if I understand it correctly) that the latter is true. However, the former would make more sense to me. If I am wrong, what am I missing? 回答1: It's equivalent to the latter one

“with” macro in C

早过忘川 提交于 2019-12-09 13:17:35
问题 I was looking for a macro that will resemble the with-construct. The usage should be something like: with (lock(&x), unlock(&x)) { ... } It might be useful for some other purposes. I came up with this macro: #define __with(_onenter, _onexit, v) \ for (int __with_uniq##v=1; __with_uniq##v > 0; )\ for (_onenter; __with_uniq##v > 0; _onexit) \ while (__with_uniq##v-- > 0) #define _with(x, y, z) __with(x, y, z) #define with(_onenter, _onexit) _with(_onenter, _onexit, __COUNTER__) It has 3 nested

python 'with' statement, should I use contextlib.closing?

我与影子孤独终老i 提交于 2019-12-09 05:09:06
问题 from contextlib import closing def init_db(): with closing(connect_db()) as db: with app.open_resource('schema.sql') as f: db.cursor().executescript(f.read()) db.commit() This is from flask tutorial Step 3(http://flask.pocoo.org/docs/tutorial/dbinit/#tutorial-dbinit). And I'm little curious about the line 4 of that. Must I import and use that 'contextlib.closing()' method? When I've learned about with statement, many articles said that it closes file automatically after process like below.

“with”, context manager, python: What's going on in simple terms?

天涯浪子 提交于 2019-12-08 12:12:22
问题 Novice Python coder here coming from a Java background. I'm still puzzled by this: with open(...) as f: do_something(f) even after Googling and reading some of the answers here (I just couldn't get my head around them). My understanding is that there is this thing called a context manager that is some sort of wrapper that contains a reference to a file that is created. Regarding as f: the 'as' above is like the 'as' below import numpy as np It's just an alias. 'f' doesn't refer to a file, but

Using Python's 'with open()' To Write a Log, How Can I Write Exceptions to my Log?

半腔热情 提交于 2019-12-08 03:49:34
问题 I'm setting up a log file for a python script being run in a command window so that there's a record of all the input and output from the script. I'm using: with open("file.txt") as file: so that it will still save all the text it has written if an exception occurs. However I was wondering if there might be a way to get it to log the exception as well? Basically to write one last message and then close the file as the exit function? There seemed to be only a little documentation on with open(

Where is a Python built-in object's __enter__() and __exit__() defined?

旧巷老猫 提交于 2019-12-07 12:11:02
问题 I've read that the object's __ enter__() and __ exit__() methods are called every time 'with' is used. I understand that for user-defined objects, you can define those methods yourself, but I don't understand how this works for built-in objects/functions like 'open' or even the testcases. This code works as expected and I assume it closes the file with __ exit__(): with open('output.txt', 'w') as f: f.write('Hi there!') or with self.assertRaises(ValueError): remove_driver(self.driver) # self

Calculate a series of weighted means in R for groups with different weightings

可紊 提交于 2019-12-07 10:27:00
问题 I have the following dataset (simple version of my actual data), 'data', and would like to calculate weighted means for variables x1 and x2, using weightings w1 and w2 respectively, split up into two groups (groups determined by the variable n). data <- data.frame(n = c(1,1,1,2,2,2), x1 = c(4,5,4,7,5,5), x2 = c(7,10,9,NaN,11,12), w1 = c(0,1,1,1,1,1), w2 = c(1,1,1,0,0,1)) I'm trying to do it using with() but get an error when I run this: with(data, aggregate(x = list(x1=x1, x2=x2), by = list(n

Other builtin or practical examples of python `with` statement usage?

旧巷老猫 提交于 2019-12-07 08:15:17
问题 Does anyone have a real world example outside of python's file object implementation of an __enter__ and __exit__ use case? Preferably your own, since what I'm trying to achieve is a better way to conceptualize the cases where it would be used. I've already read this. And, here's a link to the python documentation. 回答1: There are many uses. Just in the standard library we have: sqlite3 ; using the connection as a context manager translates to committing or aborting the transaction. unittest ;

Why is the with() construct not included in C#, when it is really cool in VB.NET?

懵懂的女人 提交于 2019-12-06 21:33:56
问题 I am C# developer. I really love the curly brace because I came from C, C++ and Java background. However, I also like the other programming languages of the .NET Family such as VB.NET. Switching back and forth between C# and VB.NET is not really that big of deal if you have been programming for a while in .NET. That is very common approach in the company where I work. As C# guy, I really like the XML literal and with keywords provided by the VB.NET compiler. I wish Microsoft had included

line-by-line file processing, for-loop vs with

夙愿已清 提交于 2019-12-06 18:11:07
问题 I am trying to understand the trade offs/differences between these to ways of opening files for line-by-line processing with open('data.txt') as inf: for line in inf: #etc vs for line in open('data.txt'): # etc I understand that using with ensures the file is closed when the "with-block" (suite?) is exited (or an exception is countered). So I have been using with ever since I learned about it here. Re for -loop: From searching around the net and SO, it seems that whether the file is closed