Re: correct way to catch exception with Python 'with' statement

2016-12-02 Thread Grant Edwards
On 2016-12-02, Marko Rauhamaa wrote: > Grant Edwards : >> In general CISC processors like x86, AMD64, 68K have read-modify-write >> instructions that allow you to increment a memory location or >> set/clear a bit in memory with a single instruction: >> >> INC.W [R0]# increment memory word

Re: correct way to catch exception with Python 'with' statement

2016-12-02 Thread Marko Rauhamaa
Grant Edwards : > In general CISC processors like x86, AMD64, 68K have read-modify-write > instructions that allow you to increment a memory location or > set/clear a bit in memory with a single instruction: > > INC.W [R0]# increment memory word whose addr is in register R0 The x86 instru

Re: correct way to catch exception with Python 'with' statement

2016-12-02 Thread Michael Torrie
On 12/01/2016 08:39 PM, Ned Batchelder wrote: > On Thursday, December 1, 2016 at 7:26:18 PM UTC-5, DFS wrote: >> How is it possible that the 'if' portion runs, then 44/100,000ths of a >> second later my process yields to another process which deletes the >> file, then my process continues. > > A

Re: correct way to catch exception with Python 'with' statement

2016-12-02 Thread Grant Edwards
On 2016-12-02, Steve D'Aprano wrote: > I'm not an expert on the low-level hardware details, so I welcome > correction, but I think that you can probably expect that the OS can > interrupt code execution between any two CPU instructions. Yep, mostly. Some CPUs have "lock" features that allow two

Re: correct way to catch exception with Python 'with' statement

2016-12-01 Thread Steve D'Aprano
On Fri, 2 Dec 2016 11:26 am, DFS wrote: >> For most programs, yes, it probably will never be a problem to check >> for existence, and then assume that the file still exists.  But put that >> code on a server, and run it a couple of million times, with dozens of >> other processes also manipulating

Re: correct way to catch exception with Python 'with' statement

2016-12-01 Thread Steve D'Aprano
On Fri, 2 Dec 2016 11:26 am, DFS wrote: > On 12/01/2016 06:48 PM, Ned Batchelder wrote: >> On Thursday, December 1, 2016 at 2:31:11 PM UTC-5, DFS wrote: >>> After a simple test below, I submit that the above scenario would never >>> occur. Ever. The time gap between checking for the file's exist

Re: correct way to catch exception with Python 'with' statement

2016-12-01 Thread Ned Batchelder
On Thursday, December 1, 2016 at 7:26:18 PM UTC-5, DFS wrote: > On 12/01/2016 06:48 PM, Ned Batchelder wrote: > > On Thursday, December 1, 2016 at 2:31:11 PM UTC-5, DFS wrote: > >> After a simple test below, I submit that the above scenario would never > >> occur. Ever. The time gap between check

Re: correct way to catch exception with Python 'with' statement

2016-12-01 Thread Ned Batchelder
On Thursday, December 1, 2016 at 2:31:11 PM UTC-5, DFS wrote: > After a simple test below, I submit that the above scenario would never > occur. Ever. The time gap between checking for the file's existence > and then trying to open it is far too short for another process to sneak > in and dele

Re: correct way to catch exception with Python 'with' statement

2016-11-30 Thread Steve D'Aprano
On Wed, 30 Nov 2016 05:35 pm, DFS wrote: > On 11/29/2016 10:20 PM, Steven D'Aprano wrote: >> On Wednesday 30 November 2016 10:59, woo...@gmail.com wrote: >> >>> If you want to do something only if the file exists (or does not), use >>> os.path.isfile(filename) >> >> No, don't do that. Just because

Re: correct way to catch exception with Python 'with' statement

2016-11-30 Thread Marko Rauhamaa
;>>finally: >>>f.close() >> >> What's the problem with spelling the above >> >> try: >> f = open(...) >> except FileNotFoundError: >> ... >> with f: >> ... > > Nothing. Well, in general, the

Re: correct way to catch exception with Python 'with' statement

2016-11-29 Thread Steven D'Aprano
On Wednesday 30 November 2016 10:59, woo...@gmail.com wrote: > If you want to do something only if the file exists (or does not), use > os.path.isfile(filename) No, don't do that. Just because the file exists, doesn't mean that you have permission to read or write to it. Worse, the code is vuln

Re: correct way to catch exception with Python 'with' statement

2016-11-29 Thread Matt Wheeler
On Tue, 29 Nov 2016 at 23:59 wrote: > If you want to do something only if the file exists (or does not), use > os.path.isfile(filename) > This opens you up to a potential race condition (and has potential security implications, depending on the application), as you're using LBYL[0]. If you want

Re: correct way to catch exception with Python 'with' statement

2016-11-29 Thread woooee
If you want to do something only if the file exists (or does not), use os.path.isfile(filename) -- https://mail.python.org/mailman/listinfo/python-list

Re: correct way to catch exception with Python 'with' statement

2016-11-29 Thread Marko Rauhamaa
Peter Otten <__pete...@web.de>: > Marko Rauhamaa wrote: > >> However, I think the real answer is that you shouldn't mix the "with" >> construct with exception handling. Instead you should write: >> >>try: >>f = open("xyz") >>except FileNotFoundError: >>...[B]... >>try:

Re: correct way to catch exception with Python 'with' statement

2016-11-29 Thread Peter Otten
Marko Rauhamaa wrote: > However, I think the real answer is that you shouldn't mix the "with" > construct with exception handling. Instead you should write: > >try: >f = open("xyz") >except FileNotFoundError: >...[B]... >try: >...[A]... >finally: >f

Re: correct way to catch exception with Python 'with' statement

2016-11-29 Thread Marko Rauhamaa
Steven D'Aprano : > There is no need to catch the exception if you're not going to do > anything with it. Correct. However, the question of the subject line is still a good one. See: try: with open("xyz") as f: ...[A]... except FileNotFoundError: ...[B]... The

Re: correct way to catch exception with Python 'with' statement

2016-11-28 Thread Ganesh Pal
Thanks Steve I got what you were trying to explain , nice learning from this conversation , what I was really doing wrong I had broken down my huge code into a simple program and had missed out returning False. On Tue, Nov 29, 2016 at 11:01 AM, Steven D'Aprano < steve+comp.lang.pyt...@pearwood.

Re: correct way to catch exception with Python 'with' statement

2016-11-28 Thread Steven D'Aprano
On Tuesday 29 November 2016 02:18, Ganesh Pal wrote: > On Mon, Nov 28, 2016 at 1:16 PM, Steven D'Aprano < > steve+comp.lang.pyt...@pearwood.info> wrote: > >> >> >> There is no need to return True. The function either succeeds, or it >> raises an >> exception, so there is no need to return any val

correct way to catch exception with Python 'with' statement

2016-11-28 Thread g thakuri
- What will be the best way to catch the exception in the above program ? Can we replace both the with statement in the above program with something like below try: for i in range(1000): with open(os.path.join(QA_TEST_DIR,"filena

Re: correct way to catch exception with Python 'with' statement

2016-11-28 Thread Thomas 'PointedEars' Lahn
Ganesh Pal wrote: > I am using Python 2.7 and Linux As a rule of thumb¹, use at least Python 3.3 for new programs. > What will be the best way to catch the exception in the above program ? > Can we replace both the with statement in the above program with > something like be

Re: correct way to catch exception with Python 'with' statement

2016-11-28 Thread Michael Torrie
On 11/28/2016 08:18 AM, Ganesh Pal wrote: > On Mon, Nov 28, 2016 at 1:16 PM, Steven D'Aprano < > steve+comp.lang.pyt...@pearwood.info> wrote: > >> >> >> There is no need to return True. The function either succeeds, or it >> raises an >> exception, so there is no need to return any value at all. >

Re: correct way to catch exception with Python 'with' statement

2016-11-28 Thread Ganesh Pal
On Mon, Nov 28, 2016 at 1:16 PM, Steven D'Aprano < steve+comp.lang.pyt...@pearwood.info> wrote: > > > There is no need to return True. The function either succeeds, or it > raises an > exception, so there is no need to return any value at all. > > I returned True here ,because based on the result

Re: correct way to catch exception with Python 'with' statement

2016-11-27 Thread Steven D'Aprano
at can't be opened: def create_files_append(): """Docstring""" os.makedirs(QA_TEST_DIR) # Create few files and write something for i in range(1000): try: with open(os.path.join(QA_TEST_DIR,"filename%d" %i),'w

correct way to catch exception with Python 'with' statement

2016-11-27 Thread Ganesh Pal
- --- What will be the best way to catch the exception in the above program ? Can we replace both the with statement in the above program with something like below try: for i in range(1000): with open(os.path.join(QA_TEST_DIR,"filename%d

Re: VB/Pascal with statement [was Re: Proposal for new minor syntax]

2015-03-28 Thread Rustom Mody
On Saturday, March 28, 2015 at 9:51:50 PM UTC+5:30, Rustom Mody wrote: > So if the VB model is followed, it is purely a syntactic (ie not type-related) > question whether an identifier is an adorned variable or an attribute of > something else. The preceding dot is the disambiguator. Uh... UN-ado

Re: VB/Pascal with statement [was Re: Proposal for new minor syntax]

2015-03-28 Thread Rustom Mody
> > > In VB one can do: > > > > with self > > .attr1 = field1 > > .attr2 = field2 > > > > (or something like that -- dont exactly remember the syntax) > > > Pascal is another language with a construct like that, and there's a FAQ for

Re: VB/Pascal with statement [was Re: Proposal for new minor syntax]

2015-03-28 Thread Mark Lawrence
t-python-have-a-with-statement-for-attribute-assignments >>> help('FAQ') no Python documentation found for 'FAQ' Less than useless, now what? Did you try googling? help("google") Fails in just the same way. Do I raise a bug report on the issue trac

Re: VB/Pascal with statement [was Re: Proposal for new minor syntax]

2015-03-28 Thread Steven D'Aprano
On Sat, 28 Mar 2015 11:26 pm, Mark Lawrence wrote: > On 28/03/2015 06:26, Steven D'Aprano wrote: >>Pascal is another language with a construct like that, and there's a FAQ >>for it: >> >> https://docs.python.org/2/faq/design.html#why-doesn-t-python-have-a-wit

Re: VB/Pascal with statement [was Re: Proposal for new minor syntax]

2015-03-28 Thread Mark Lawrence
that -- dont exactly remember the syntax) Pascal is another language with a construct like that, and there's a FAQ for it: https://docs.python.org/2/faq/design.html#why-doesn-t-python-have-a-with-statement-for-attribute-assignments c:\cpython\PCbuild>python Python 3.4.3 (v3.4.3:9b73f1c

Re: VB/Pascal with statement [was Re: Proposal for new minor syntax]

2015-03-27 Thread Chris Angelico
one can do: >> >> with self >> .attr1 = field1 >> .attr2 = field2 >> >> (or something like that -- dont exactly remember the syntax) > > > Pascal is another language with a construct like that, and there's a FAQ for > it: > > https://docs.pyth

VB/Pascal with statement [was Re: Proposal for new minor syntax]

2015-03-27 Thread Steven D'Aprano
g like that -- dont exactly remember the syntax) Pascal is another language with a construct like that, and there's a FAQ for it: https://docs.python.org/2/faq/design.html#why-doesn-t-python-have-a-with-statement-for-attribute-assignments -- Steven -- https://mail.python.org/mailman/listinfo/python-list

Re: with statement

2012-04-19 Thread Ethan Furman
Ian Kelly wrote: On Thu, Apr 19, 2012 at 3:33 PM, Terry Reedy wrote: On 4/19/2012 1:15 PM, Kiuhnm wrote: A with statement is not at the module level only if it appears inside a function definition or a class definition. This is true, I believe, of all statements. Am I forgetting something

Re: with statement

2012-04-19 Thread Ian Kelly
On Thu, Apr 19, 2012 at 3:33 PM, Terry Reedy wrote: > On 4/19/2012 1:15 PM, Kiuhnm wrote: >> >> A with statement is not at the module level only if it appears inside a >> function definition or a class definition. > > > This is true, I believe, of all statements.

Re: with statement

2012-04-19 Thread Terry Reedy
On 4/19/2012 1:15 PM, Kiuhnm wrote: A with statement is not at the module level only if it appears inside a function definition or a class definition. This is true, I believe, of all statements. Am I forgetting something? Comprehensions (in Py3) and lambda expressions also introduce new

Re: with statement

2012-04-19 Thread Kiuhnm
On 4/19/2012 20:02, Jacob MacDonald wrote: On Thursday, April 19, 2012 10:15:23 AM UTC-7, Kiuhnm wrote: A with statement is not at the module level only if it appears inside a function definition or a class definition. Am I forgetting something? Kiuhnm That sounds about right to me. However

Re: with statement

2012-04-19 Thread Jacob MacDonald
On Thursday, April 19, 2012 10:15:23 AM UTC-7, Kiuhnm wrote: > A with statement is not at the module level only if it appears inside a > function definition or a class definition. > Am I forgetting something? > > Kiuhnm That sounds about right to me. However, I haven't rea

RE: alternative to with statement?

2012-02-28 Thread Prasad, Ramit
streamlined 'with' syntax- abusing the for loop might just >be confusing. The with statement is also a good fit because the caching >strategy does have to atomically acquire, create and release the appropriate >locks. With this statement the cached CLI wrappers can be called

Re: alternative to with statement?

2012-02-28 Thread Craig Yoshioka
abusing the for loop might just be confusing. The with statement is also a good fit because the caching strategy does have to atomically acquire, create and release the appropriate locks. With this statement the cached CLI wrappers can be called from simultaneously from different scripts and s

Re: alternative to with statement?

2012-02-28 Thread Terry Reedy
On 2/28/2012 5:12 PM, Prasad, Ramit wrote: Craig Yoshioka wrote: I see that there was previously a PEP to allow the with statement to skip the enclosing block... this was shot down, and I'm trying to think of the most elegant alternative. [..] I would have really liked: with cachingco

Re: alternative to with statement?

2012-02-28 Thread Chris Kaynor
On Tue, Feb 28, 2012 at 1:04 PM, Craig Yoshioka wrote: > > I see that there was previously a PEP to allow the with statement to skip the > enclosing block... this was shot down, and I'm trying to think of the most > elegant alternative. > The best I've found is

RE: alternative to with statement?

2012-02-28 Thread Prasad, Ramit
Craig Yoshioka wrote: >I see that there was previously a PEP to allow the with statement to skip the >enclosing block... this was shot down, and I'm trying to think of the most >elegant alternative. [..] >I would have really liked: >with cachingcontext(x): ># creat

alternative to with statement?

2012-02-28 Thread Craig Yoshioka
I see that there was previously a PEP to allow the with statement to skip the enclosing block... this was shot down, and I'm trying to think of the most elegant alternative. The best I've found is to abuse the for notation: for _ in cachingcontext(x): # create cached resources her

Re: AttributeError in "with" statement (3.2.2)

2011-12-17 Thread Terry Reedy
On 12/16/2011 8:26 PM, Steven D'Aprano wrote: On Fri, 16 Dec 2011 17:05:57 -0500, Terry Reedy wrote: It is am important distinction [unbound versus bound] It is not an important distinction, and I am not confusing the two. So we agree on the distinction but disagree on its importance. Let

Re: AttributeError in "with" statement (3.2.2)

2011-12-16 Thread Steven D'Aprano
On Fri, 16 Dec 2011 15:26:30 -0800, Ethan Furman wrote: > Terry Reedy wrote: >> On 12/16/2011 4:22 AM, Steven D'Aprano wrote: [...] > I think you two are in violent agreement as far as how Python is > functioning, and the conflict is in the names given to the various > pieces... I think a glossar

Re: AttributeError in "with" statement (3.2.2)

2011-12-16 Thread Steven D'Aprano
On Fri, 16 Dec 2011 17:05:57 -0500, Terry Reedy wrote: > On 12/16/2011 4:22 AM, Steven D'Aprano wrote: >> On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: [...] >> >> After reading your post, I think I have worked out where our >> disagreement lines: you think that bound methods and instance

Re: AttributeError in "with" statement (3.2.2)

2011-12-16 Thread Ethan Furman
Ethan Furman wrote: Terry Reedy wrote: On 12/16/2011 4:22 AM, Steven D'Aprano wrote: On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: [...] After reading your post, I think I have worked out where our disagreement lies: you think that bound methods and instance methods are not the same

Re: AttributeError in "with" statement (3.2.2)

2011-12-16 Thread Ethan Furman
Terry Reedy wrote: On 12/16/2011 4:22 AM, Steven D'Aprano wrote: On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: [...] After reading your post, I think I have worked out where our disagreement lies: you think that bound methods and instance methods are not the same thing, Do you agree

Re: AttributeError in "with" statement (3.2.2)

2011-12-16 Thread Terry Reedy
On 12/16/2011 4:22 AM, Steven D'Aprano wrote: On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: [...] After reading your post, I think I have worked out where our disagreement lines: you think that bound methods and instance methods are not the same thing, Do you agree that an unbound met

Re: AttributeError in "with" statement (3.2.2)

2011-12-16 Thread Steven D'Aprano
On Thu, 15 Dec 2011 19:39:17 -0500, Terry Reedy wrote: [...] After reading your post, I think I have worked out where our disagreement lines: you think that bound methods and instance methods are not the same thing, and that a function defined inside a class is different from a function outside

Re: AttributeError in "with" statement (3.2.2)

2011-12-15 Thread Steven D'Aprano
On Thu, 15 Dec 2011 05:35:55 -0800, Steve Howell wrote: > For the special methods like __enter__ and __exit__, the tricky part > isn't understanding what would happen once the methods were called; the > tricky part is getting them to be called in the first place, if they > were not declared inside

Re: AttributeError in "with" statement (3.2.2)

2011-12-15 Thread Terry Reedy
On 12/15/2011 12:01 AM, Steven D'Aprano wrote: On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: On 12/14/2011 3:01 AM, Steven D'Aprano wrote: On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: To complement what Eric says below: The with statement is looking for a

Re: AttributeError in "with" statement (3.2.2)

2011-12-15 Thread Gregory Ewing
MRAB wrote: To give an analogy, it is like defining mammals as "hairy animals which give birth to live young", which is correct for all mammals except for monotremes, which are mammals which lay eggs. Or the naked mole-rat. Or cetaceans (whales). The way I understand it, the main characteris

Re: AttributeError in "with" statement (3.2.2)

2011-12-15 Thread Steve Howell
On Dec 14, 9:01 pm, Steven D'Aprano wrote: > [...] > So what are methods? In Python, methods are wrappers around functions > which automatically pass the instance to the inner function object. Under > normal circumstances, you create methods by declaring functions inside a > class, but that's not

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread Steven D'Aprano
On Thu, 15 Dec 2011 05:01:21 +, Steven D'Aprano wrote: >> From the Python glossary: >> "method: A function which is defined inside a class body." >> >> That is actually a bit too narrow, as a function can be added to the >> class after it is defined. But the point then is that it is treated

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread Steven D'Aprano
ry Reedy wrote: >>>> >>>>> To complement what Eric says below: The with statement is looking >>>>> for an instance *method*, which by definition, is a function >>>>> attribute of a *class* (the class of the context manager) that >>&

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread MRAB
On 15/12/2011 05:01, Steven D'Aprano wrote: On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: On 12/14/2011 3:01 AM, Steven D'Aprano wrote: On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: To complement what Eric says below: The with statement is looking for a

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread Steven D'Aprano
On Wed, 14 Dec 2011 18:13:36 -0500, Terry Reedy wrote: > On 12/14/2011 3:01 AM, Steven D'Aprano wrote: >> On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: >> >>> To complement what Eric says below: The with statement is looking for >>> an instan

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread Terry Reedy
On 12/14/2011 3:01 AM, Steven D'Aprano wrote: On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: To complement what Eric says below: The with statement is looking for an instance *method*, which by definition, is a function attribute of a *class* (the class of the context manager)

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread Eric Snow
On Wed, Dec 14, 2011 at 12:14 PM, Lie Ryan wrote: > On 12/15/2011 03:56 AM, Eric Snow wrote: >> >> On Tue, Dec 13, 2011 at 11:05 PM, Eric Snow >>  wrote: >> >> If you want to be more dynamic about it you can do it, but it involves >> black magic.  Chances are really good that being explicit throug

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread Lie Ryan
On 12/15/2011 03:56 AM, Eric Snow wrote: On Tue, Dec 13, 2011 at 11:05 PM, Eric Snow wrote: If you want to be more dynamic about it you can do it, but it involves black magic. Chances are really good that being explicit through your class definition is the right approach. Note that the black

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread Steve Howell
On Dec 14, 12:01 am, Steven D'Aprano wrote: > [...] > > So the normal lookup rules that apply to data attributes, namely > instance, then class, then superclasses, also applies to methods in > Python. In languages that don't allow that sort of thing, like Java, you > need to use convoluted design

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread Eric Snow
ve >> foo.__exit__ = foo.goodbye >> foo.__exit__() # outputs goodbye Steve >> >> with foo: # fails with AttributeError:  __exit__ >>  print("doing stuff") >> >> I am dynamically adding an attribute __exit__ to the variable foo, >> which works fine

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread 88888 Dihedral
On Thursday, December 15, 2011 12:08:32 AM UTC+8, 8 Dihedral wrote: > On Wednesday, December 14, 2011 4:01:24 PM UTC+8, Steven D'Aprano wrote: > > On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > > > > > To complement what Eric says below: The wit

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread 88888 Dihedral
On Wednesday, December 14, 2011 4:01:24 PM UTC+8, Steven D'Aprano wrote: > On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > > > To complement what Eric says below: The with statement is looking for an > > instance *method*, which by definition, is a function at

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread Peter Otten
quot;doing stuff") > > I am dynamically adding an attribute __exit__ to the variable foo, > which works fine when I call it directly, but it fails when I try to > use foo as the expression in the with statement. Here is the full > output: > >> python3 with.coffee &g

Re: AttributeError in "with" statement (3.2.2)

2011-12-14 Thread Steven D'Aprano
On Wed, 14 Dec 2011 01:29:13 -0500, Terry Reedy wrote: > To complement what Eric says below: The with statement is looking for an > instance *method*, which by definition, is a function attribute of a > *class* (the class of the context manager) that takes an instance of the > class

Re: AttributeError in "with" statement (3.2.2)

2011-12-13 Thread Terry Reedy
ce*. with foo: # fails with AttributeError: __exit__ print("doing stuff") I am dynamically adding an attribute __exit__ to the variable foo, which works fine when I call it directly, but it fails when I try to use foo as the expression in the with statement. Here is the full outp

Re: AttributeError in "with" statement (3.2.2)

2011-12-13 Thread Eric Snow
 __exit__ >  print("doing stuff") > > I am dynamically adding an attribute __exit__ to the variable foo, > which works fine when I call it directly, but it fails when I try to > use foo as the expression in the with statement.  Here is the full > output: > >&

AttributeError in "with" statement (3.2.2)

2011-12-13 Thread Steve Howell
ctly, but it fails when I try to use foo as the expression in the with statement. Here is the full output: > python3 with.coffee goodbye Steve goodbye Steve Traceback (most recent call last): File "with.coffee", line 17, in with foo: # fails with AttributeError: Attr

Re: with statement and context managers

2011-08-04 Thread Steven D'Aprano
Thomas Rachel wrote: > Am 03.08.2011 04:15 schrieb Steven D'Aprano: [...] > > but to me that looks badly wrong. Surely the spam context manager > > object will exit after the first iteration, and always raise an > > exception on the second? But I don't quite understand context > > managers eno

Re: with statement and context managers

2011-08-03 Thread Thomas Rachel
Am 03.08.2011 04:15 schrieb Steven D'Aprano: > I'm not greatly experienced with context managers and the with > statement, so I would like to check my logic. > > Somebody (doesn't matter who, or where) stated that they frequently > use this idiom: > > spam =

Re: with statement and context managers

2011-08-02 Thread Nobody
On Wed, 03 Aug 2011 12:15:44 +1000, Steven D'Aprano wrote: > I'm not greatly experienced with context managers and the with statement, so > I would like to check my logic. > > Somebody (doesn't matter who, or where) stated that they frequently use this > idiom: >

Re: with statement and context managers

2011-08-02 Thread Jack Diederich
On Tue, Aug 2, 2011 at 10:15 PM, Steven D'Aprano wrote: > I'm not greatly experienced with context managers and the with statement, so > I would like to check my logic. > > Somebody (doesn't matter who, or where) stated that they frequently use this > idiom: >

with statement and context managers

2011-08-02 Thread Steven D'Aprano
I'm not greatly experienced with context managers and the with statement, so I would like to check my logic. Somebody (doesn't matter who, or where) stated that they frequently use this idiom: spam = MyContextManager(*args) for ham in my_iter: with spam: # do stuff but

Re: [Python-ideas] with statement syntax forces ugly line breaks?

2010-09-11 Thread Thomas Jollans
On Saturday 11 September 2010, it occurred to Lawrence D'Oliveiro to exclaim: > In message , MRAB > > wrote: > > On 08/09/2010 19:07, Georg Brandl wrote: > >> Thus spake the Lord: Thou shalt indent with four spaces. No more, no > >> less. Four shall be the number of spaces thou shalt indent, and t

Re: [Python-ideas] with statement syntax forces ugly line breaks?

2010-09-10 Thread Lawrence D'Oliveiro
In message , MRAB wrote: > On 08/09/2010 19:07, Georg Brandl wrote: > >> Thus spake the Lord: Thou shalt indent with four spaces. No more, no >> less. Four shall be the number of spaces thou shalt indent, and the >> number of thy indenting shall be four. Eight shalt thou not indent, >> nor either

Re: [Python-ideas] with statement syntax forces ugly line breaks?

2010-09-09 Thread MRAB
On 09/09/2010 17:07, Mark Lawrence wrote: On 08/09/2010 20:30, MRAB wrote: On 08/09/2010 19:07, Georg Brandl wrote: Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eig

Re: [Python-ideas] with statement syntax forces ugly line breaks?

2010-09-09 Thread Mark Lawrence
On 08/09/2010 20:30, MRAB wrote: On 08/09/2010 19:07, Georg Brandl wrote: Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent

Re: how to extract an implicit dict expression (with statement return)

2010-09-09 Thread Benjamin Kaplan
On Thu, Sep 9, 2010 at 11:20 AM, Fritz Loseries wrote: > Hi, > > I do not know how to subject my problem in a better way. > > I have the following statement: > >    return [ dict(x1 = elem.x1, x2 = elem.x2, x3 = elem.x3,) >                for elem in method(in_values) >              ] > > How can

how to extract an implicit dict expression (with statement return)

2010-09-09 Thread Fritz Loseries
Hi, I do not know how to subject my problem in a better way. I have the following statement: return [ dict(x1 = elem.x1, x2 = elem.x2, x3 = elem.x3,) for elem in method(in_values) ] How can I transform it to an explicit description: result = ... return

Re: [Python-ideas] with statement syntax forces ugly line breaks?

2010-09-08 Thread MRAB
On 08/09/2010 19:07, Georg Brandl wrote: Thus spake the Lord: Thou shalt indent with four spaces. No more, no less. Four shall be the number of spaces thou shalt indent, and the number of thy indenting shall be four. Eight shalt thou not indent, nor either indent thou two, excepting that thou the

Re: with statement and standard library

2010-02-21 Thread Chris Rebert
On Sun, Feb 21, 2010 at 3:21 PM, John Nagle wrote: > nobrowser wrote: >> >> Hi.  The with statement is certainly nifty.  The trouble is, the >> *only* two documented examples how it can be used with the library >> classes are file objects (which I use all the time)

Re: with statement and standard library

2010-02-21 Thread John Nagle
nobrowser wrote: Hi. The with statement is certainly nifty. The trouble is, the *only* two documented examples how it can be used with the library classes are file objects (which I use all the time) and thread locks which I almost never use. Yet there are many, many classes in the library

Re: with statement and standard library

2010-02-19 Thread Terry Reedy
On 2/19/2010 2:18 AM, nobrowser wrote: Hi. The with statement is certainly nifty. The trouble is, the *only* two documented examples how it can be used with the library classes are file objects (which I use all the time) and thread locks which I almost never use. Yet there are many, many

Re: with statement and standard library

2010-02-19 Thread Robert Kern
On 2010-02-19 01:18 AM, nobrowser wrote: Hi. The with statement is certainly nifty. The trouble is, the *only* two documented examples how it can be used with the library classes are file objects (which I use all the time) and thread locks which I almost never use. Yet there are many, many

Re: with statement and standard library

2010-02-19 Thread alex23
nobrowser wrote: > Yet there are many, many classes in the > library whose use would be more elegant and readable if the with > statement could be employed.  Start with the connection objects in > httplib and you can probably come up with 10 others easily.  Maybe it > is the ca

with statement and standard library

2010-02-18 Thread nobrowser
Hi. The with statement is certainly nifty. The trouble is, the *only* two documented examples how it can be used with the library classes are file objects (which I use all the time) and thread locks which I almost never use. Yet there are many, many classes in the library whose use would be

"file" does not work with the "with" statement!

2009-12-10 Thread Michele Simionato
I have just discovered that the syntax with file(name, 'w') as f: do_something(f) does not close the file at the end of the with statement! On the contrary with open(name, 'w') as f: do_something(f) works fine. The docs say "When opening a file, it’s preferable

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Michele Simionato
On Dec 10, 11:59 am, Christian Heimes wrote: > In Python 2.5 you have to implement your own __enter__ and __exit__ > methods if you subclass from file. The file.__exit__ function doesn't > call f.close(). Yes, that was my problem. -- http://mail.python.org/mailman/listinfo/python-list

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Peter Otten
;) >> test(outf) >> >> Which Python do you use? >> >> Diez > > Python 2.5, but it could be an artifact of the way I am looking if a > file is closed. > I have subclassed the file builtin, added a .close method and it was > not called by the > "with&qu

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Christian Heimes
Michele Simionato wrote: > Python 2.5, but it could be an artifact of the way I am looking if a > file is closed. > I have subclassed the file builtin, added a .close method and it was > not called by the > "with" statement. This during debugging, but now I have found an

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Diez B. Roggisch
;> test(outf) >> >> Which Python do you use? >> >> Diez > > Python 2.5, but it could be an artifact of the way I am looking if a > file is closed. The above ran on python2.5 for me, no hitch. > I have subclassed the file builtin, added a .close method and

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Michele Simionato
gt; > Diez Python 2.5, but it could be an artifact of the way I am looking if a file is closed. I have subclassed the file builtin, added a .close method and it was not called by the "with" statement. This during debugging, but now I have found another reason to explain why I was ru

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Diez B. Roggisch
Michele Simionato wrote: > I have just discovered that the syntax > > with file(name, 'w') as f: >do_something(f) > > does not close the file at the end of the with statement! On the > contrary > > with open(name, 'w') as f: >do_someth

Re: "file" does not work with the "with" statement!

2009-12-10 Thread Peter Otten
Michele Simionato wrote: > I have just discovered that the syntax > > with file(name, 'w') as f: >do_something(f) > > does not close the file at the end of the with statement! On the > contrary > > with open(name, 'w') as f: >do_someth

Re: Line-continuation "Anti-Idiom" and with statement

2009-11-24 Thread Neil Cerutti
On 2009-11-23, Terry Reedy wrote: > Neil Cerutti wrote: >> Unfortunately, the new "nested" with statement (which I also read >> about today) forces me into this anti-idiom. When replacing an >> appearance of contextlib.nested with the 3K with statement, I >&g

Re: Line-continuation "Anti-Idiom" and with statement

2009-11-23 Thread Terry Reedy
cter labeled an > anti-idiom. I already generally avoided it, so I just nodded. > > Unfortunately, the new "nested" with statement (which I also read > about today) forces me into this anti-idiom. When replacing an > appearance of contextlib.nested with the 3K with statement

Re: Line-continuation "Anti-Idiom" and with statement

2009-11-23 Thread Terry Reedy
lly avoided it, so I just nodded. Unfortunately, the new "nested" with statement (which I also read about today) forces me into this anti-idiom. When replacing an appearance of contextlib.nested with the 3K with statement, I ended up with an unexpected syntax error. with (open(roster_pa

Re: Line-continuation "Anti-Idiom" and with statement

2009-11-23 Thread MRAB
idiom. I already generally avoided it, so I just nodded. > > Unfortunately, the new "nested" with statement (which I also read > about today) forces me into this anti-idiom. When replacing an > appearance of contextlib.nested with the 3K with statement, I > ended up

Line-continuation "Anti-Idiom" and with statement

2009-11-23 Thread Neil Cerutti
st nodded. Unfortunately, the new "nested" with statement (which I also read about today) forces me into this anti-idiom. When replacing an appearance of contextlib.nested with the 3K with statement, I ended up with an unexpected syntax error. with (open(roster_path, 'r') as roster

  1   2   >