RE: Context without manager

2023-11-27 Thread David Raymond via Python-list
> I *must* do: > > with device_open() as device: >device.do_something() > > Nevertheless, I _need_ to have a class > where the device is opened in the __init__() > and used in some methods. > > Any ideas? Perhaps take a look at contextlib.ExitStack and see if you can do something with it.

Re: Context without manager

2023-11-27 Thread Richard Damon via Python-list
Read the Fine context manager documentation. What “with with_expression as var” does is effectively: ob = with_expression var = ob.__enter__() And then at the end of the with, does a ob.__exit__() (With some parameters to __exit__, that could just be None, None, None for the simplest case). N

Re: Context without manager

2023-11-27 Thread Piergiorgio Sartor via Python-list
On 26/11/2023 18.50, Dieter Maurer wrote: Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100: ... Apparently, the "with" context manager is not usable in classes, at least not with __init__() & co. You can use `with` in classes -- with any context manager. However, you would usually not use `w

Re: Context without manager

2023-11-26 Thread Greg Ewing via Python-list
On 27/11/23 5:03 pm, Grant Edwards wrote: I should probably have written "how to fool that into working when he's not using a 'with' statement" It should be possible to run the context protocol yourself. Something like (warning, untested): class MyDeviceWrapper: def __init__(self

Re: Context without manager

2023-11-26 Thread Greg Ewing via Python-list
On 27/11/23 9:03 am, Stefan Ram wrote: Above, "have" is followed by another verb in "have been", so it should be eligible for a contraction there! Yes, "been" is the past participle of 'to be", so "I've been" is fine. -- Greg -- https://mail.python.org/mailman/listinfo/python-list

Re: Context without manager

2023-11-26 Thread Grant Edwards via Python-list
On 2023-11-27, Grant Edwards via Python-list wrote: > On 2023-11-26, Dieter Maurer via Python-list wrote: > >> If you do not have this case (e.g. usually if you open the file >> in a class's `__init__`), you do not use a context manager. > > He knows that. The OP wrote that he wants to use that

Re: Context without manager

2023-11-26 Thread Grant Edwards via Python-list
On 2023-11-26, Dieter Maurer via Python-list wrote: > If you do not have this case (e.g. usually if you open the file > in a class's `__init__`), you do not use a context manager. He knows that. The OP wrote that he wants to use that can _only_ be used by a context manager, but he wants that us

Re: Context without manager

2023-11-26 Thread Dieter Maurer via Python-list
Piergiorgio Sartor wrote at 2023-11-25 22:15 +0100: > ... >Apparently, the "with" context manager is not usable >in classes, at least not with __init__() & co. You can use `with` in classes -- with any context manager. However, you would usually not use `with` with a file you have opened in `__ini

Re: Context manager for database connection

2023-08-23 Thread dn via Python-list
On 24/08/2023 06.11, dn via Python-list wrote: On 24/08/2023 03.41, Jason Friedman via Python-list wrote: with Database() as mydb: conn = mydb.get_connection() cursor = conn.get_cursor() cursor.execute("update table1 set x = 1 where y = 2") cursor.close() cursor = conn.get_cursor() cursor.execut

Re: Context manager for database connection

2023-08-23 Thread dn via Python-list
On 24/08/2023 03.41, Jason Friedman via Python-list wrote: I want to be able to write code like this: with Database() as mydb: conn = mydb.get_connection() cursor = conn.get_cursor() cursor.execute("update table1 set x = 1 where y = 2") cursor.close() cursor = conn.get_cursor() cursor.execute("u

Re: Context manager on method call from class

2018-03-15 Thread Joseph L. Casale
From: Python-list on behalf of Rob Gaddi Sent: Thursday, March 15, 2018 12:47 PM To: python-list@python.org Subject: Re: Context manager on method call from class   > from contextlib import contextmanager. > > Then you just use the @contextmanager decorator on a function, have it

Re: Context manager on method call from class

2018-03-15 Thread Rob Gaddi
On 03/15/2018 11:17 AM, Joseph L. Casale wrote: I have a class which implements a context manager, its __init__ has a signature and the __enter__ returns an instance of the class. Along with several methods which implement functionality on the instance, I have one method which itself must open a

Re: Re: Context manager able to write to the caller's namespace

2018-01-18 Thread Chris Angelico
On Fri, Jan 19, 2018 at 5:00 PM, wrote: > Hello, > > Thank you for your mail. I will answer as fast as possible. If you're going to subscribe to a mailing list, PLEASE disable your autoresponder. Otherwise, messages like this will get marked as Spam, and your legit mail will end up getting tarr

Re: Context manager able to write to the caller's namespace

2018-01-18 Thread Steven D'Aprano
On Fri, 19 Jan 2018 16:49:49 +1100, Chris Angelico wrote: [...] > 1) Context manager was called from global scope, and needs access to > globals() or locals() as returned in the caller A! /facepalm Of course the caller can just pass locals() to the context manager. Why didn't I think o

Re: Context manager able to write to the caller's namespace

2018-01-18 Thread Chris Angelico
On Fri, Jan 19, 2018 at 3:48 PM, Steven D'Aprano wrote: > I want to define a context manager in one module: > > # a.py > def CM: > def __enter__(self): > return self > def __exit__(self, *args): > pass > > > Then call it from another module: > > # b.py > import a > with a.C

Re: Context

2017-02-03 Thread William Ray Wing
> On Feb 3, 2017, at 8:10 AM, Antonio wrote: > > From: Antonio > Sent: Friday, February 3, 2017 1:02 PM > To: python-list@python.org > Subject: Context > > I have python version 3.6.0 installed into my desktop)windows 7) but the > menu/context (file,edit..etc) i

Re: context managers inline?

2016-03-11 Thread jmp
On 03/10/2016 07:59 PM, Neal Becker wrote: sohcahto...@gmail.com wrote: On Thursday, March 10, 2016 at 10:33:47 AM UTC-8, Neal Becker wrote: Is there a way to ensure resource cleanup with a construct such as: x = load (open ('my file', 'rb)) Is there a way to ensure this file gets closed?

Re: context managers inline?

2016-03-11 Thread Jussi Piitulainen
Paul Rubin writes: > Jussi Piitulainen writes: >> return ModeIO(f.read()) > > These suggestions involving f.read() assume the file contents are small > enough to reasonably slurp into memory. That's unlike the original > where "load" receives a stream and might process it piecewise. If y

Re: context managers inline?

2016-03-10 Thread Paul Rubin
Jussi Piitulainen writes: > return ModeIO(f.read()) These suggestions involving f.read() assume the file contents are small enough to reasonably slurp into memory. That's unlike the original where "load" receives a stream and might process it piecewise. -- https://mail.python.org/mailma

Re: context managers inline?

2016-03-10 Thread Jussi Piitulainen
Chris Angelico writes: > On Fri, Mar 11, 2016 at 5:33 AM, Neal Becker wrote: >> Is there a way to ensure resource cleanup with a construct such as: >> >> x = load (open ('my file', 'rb)) >> >> Is there a way to ensure this file gets closed? > > Yep! > > def read_file(fn, *a, **kw): > with ope

Re: context managers inline?

2016-03-10 Thread Steven D'Aprano
On Fri, 11 Mar 2016 05:33 am, Neal Becker wrote: > Is there a way to ensure resource cleanup with a construct such as: > > x = load (open ('my file', 'rb)) > > Is there a way to ensure this file gets closed? Depends on what you mean by "ensure". Have load() call the file's close method may be g

Re: context managers inline?

2016-03-10 Thread Chris Angelico
On Fri, Mar 11, 2016 at 5:33 AM, Neal Becker wrote: > Is there a way to ensure resource cleanup with a construct such as: > > x = load (open ('my file', 'rb)) > > Is there a way to ensure this file gets closed? Yep! def read_file(fn, *a, **kw): with open(fn, *a, **kw) as f: return f.

Re: context managers inline?

2016-03-10 Thread Mark Lawrence
On 10/03/2016 18:33, Neal Becker wrote: Is there a way to ensure resource cleanup with a construct such as: x = load (open ('my file', 'rb)) Is there a way to ensure this file gets closed? I don't see how there can be. Surely you must split it into two lines to use the context manager via

Re: context managers inline?

2016-03-10 Thread Ian Kelly
On Thu, Mar 10, 2016 at 11:59 AM, Neal Becker wrote: > sohcahto...@gmail.com wrote: > >> On Thursday, March 10, 2016 at 10:33:47 AM UTC-8, Neal Becker wrote: >>> Is there a way to ensure resource cleanup with a construct such as: >>> >>> x = load (open ('my file', 'rb)) >>> >>> Is there a way to e

Re: context managers inline?

2016-03-10 Thread Neal Becker
sohcahto...@gmail.com wrote: > On Thursday, March 10, 2016 at 10:33:47 AM UTC-8, Neal Becker wrote: >> Is there a way to ensure resource cleanup with a construct such as: >> >> x = load (open ('my file', 'rb)) >> >> Is there a way to ensure this file gets closed? > > with open('my file', 'rb')

Re: context managers inline?

2016-03-10 Thread sohcahtoa82
On Thursday, March 10, 2016 at 10:33:47 AM UTC-8, Neal Becker wrote: > Is there a way to ensure resource cleanup with a construct such as: > > x = load (open ('my file', 'rb)) > > Is there a way to ensure this file gets closed? with open('my file', 'rb') as f: x = load(f) -- https://mail.p

Re: Context-aware return

2015-09-16 Thread Steven D'Aprano
On Sun, 13 Sep 2015 09:27 am, Ned Batchelder wrote: > On Thursday, September 10, 2015 at 8:44:01 PM UTC-4, Denis McMahon wrote: >> On Fri, 11 Sep 2015 03:54:14 +1000, Steven D'Aprano wrote: >> >> > If I did this thing, would people follow me down the street booing and >> > jeering and throwing th

Re: Context-aware return

2015-09-12 Thread Ned Batchelder
On Thursday, September 10, 2015 at 8:44:01 PM UTC-4, Denis McMahon wrote: > On Fri, 11 Sep 2015 03:54:14 +1000, Steven D'Aprano wrote: > > > If I did this thing, would people follow me down the street booing and > > jeering and throwing things at me? > > Yes > > >>> x = func() > >>> x > >>> func

Re: Context-aware return

2015-09-11 Thread Mark Lawrence
On 10/09/2015 18:54, Steven D'Aprano wrote: I have a function which is intended for use at the interactive interpreter, but may sometimes be used non-interactively. I wish to change it's output depending on the context of how it is being called. If the function is being called as if it were a pr

Re: Context-aware return

2015-09-11 Thread Rustom Mody
On Friday, September 11, 2015 at 12:53:28 AM UTC+5:30, Grant Edwards wrote: > On 2015-09-10, Steven D'Aprano wrote: > > > I have a function which is intended for use at the interactive interpreter, > > but may sometimes be used non-interactively. I wish to change it's output > > depending on the

Re: Context-aware return

2015-09-11 Thread Rustom Mody
On Friday, September 11, 2015 at 12:53:28 AM UTC+5:30, Grant Edwards wrote: > On 2015-09-10, Steven D'Aprano wrote: > > > I have a function which is intended for use at the interactive interpreter, > > but may sometimes be used non-interactively. I wish to change it's output > > depending on the

Re: Context-aware return

2015-09-10 Thread Denis McMahon
On Fri, 11 Sep 2015 03:54:14 +1000, Steven D'Aprano wrote: > If I did this thing, would people follow me down the street booing and > jeering and throwing things at me? Yes >>> x = func() >>> x >>> func() >>> print x == func() >>> assert x == func() Would you expect the last two calls to func(

Re: Context-aware return

2015-09-10 Thread random832
On Thu, Sep 10, 2015, at 16:15, Akira Li wrote: > There are cases when it might be justified to alter the behavior e.g., > *colorama* allows to strip ANSI codes (e.g., disable colored output) if > stdout is not a tty or *win-unicode-console* make sys.stdout to use > WriteConsoleW() to write Unicode

Re: Context-aware return

2015-09-10 Thread Grant Edwards
On 2015-09-10, Akira Li <4kir4...@gmail.com> wrote: > Grant Edwards writes: > >> On 2015-09-10, Steven D'Aprano wrote: >> >>> I have a function which is intended for use at the interactive interpreter, >>> but may sometimes be used non-interactively. I wish to change it's output >>> depending on

Re: Context-aware return

2015-09-10 Thread Akira Li
Grant Edwards writes: > On 2015-09-10, Steven D'Aprano wrote: > >> I have a function which is intended for use at the interactive interpreter, >> but may sometimes be used non-interactively. I wish to change it's output >> depending on the context of how it is being called. > > [...] > > Sounds

Re: Context-aware return

2015-09-10 Thread Grant Edwards
On 2015-09-10, Steven D'Aprano wrote: > I have a function which is intended for use at the interactive interpreter, > but may sometimes be used non-interactively. I wish to change it's output > depending on the context of how it is being called. [...] Sounds like an excellent way to waste someb

Re: Context-aware return

2015-09-10 Thread Paul Rubin
Steven D'Aprano writes: > I want the function to know its own context. > I don't mind if it is CPython only, or if it is a bit expensive. That sounds crazy but if it's really what you want, you can probably fake it by examining the control stack using the backtrace module. I remember doing some

Re: Context-aware return

2015-09-10 Thread Sven R. Kunze
Oops, missing print: On 10.09.2015 20:45, Sven R. Kunze wrote: On 10.09.2015 20:34, Sven R. Kunze wrote: You are right. I turned out to me harder that I first thought. My initial guess was like: use AST. But now I see, it would be hard to get the source code. So, what actually could work, w

Re: Context-aware return

2015-09-10 Thread Sven R. Kunze
On 10.09.2015 20:34, Sven R. Kunze wrote: You are right. I turned out to me harder that I first thought. My initial guess was like: use AST. But now I see, it would be hard to get the source code. So, what actually could work, would be faking the interactive interpreter wrapping it up and th

Re: Context-aware return

2015-09-10 Thread Laura Creighton
In a message of Fri, 11 Sep 2015 03:54:14 +1000, "Steven D'Aprano" writes: >def func(): >do_stuff() >if procedure: # FIXME what goes here??? >return "Awesome" >else: >return 999 > >Now I can do this: > > >x = func() >assert x == 999 > >L = [1, 2, func(), 4] >assert L[2

Re: Context-aware return

2015-09-10 Thread Chris Angelico
On Fri, Sep 11, 2015 at 3:54 AM, Steven D'Aprano wrote: > If the function is being called as if it were a procedure or command, that > is the return result is just ignored, I want to return one thing. But if it > is being called where the return result goes somewhere, I want to return > something

Re: Context-aware return

2015-09-10 Thread Sven R. Kunze
On 10.09.2015 20:14, Ben Finney wrote: "Sven R. Kunze" writes: http://stackoverflow.com/questions/2356399/tell-if-python-is-in-interactive-mode I'm pretty sure Steven knows full well the answer to that question, which is not anything like the one he asked. Would you care to read the question

Re: Context-aware return

2015-09-10 Thread Sven R. Kunze
On 10.09.2015 20:12, Ben Finney wrote: First thing in the morning I will purchase a head of cabbage and store it in a warm place to make it rot, on the off chance you find some obscure way to achieve your benighted goal, just so I can be first in line to throw it as you pass. Well, go ahead. An

Re: Context-aware return

2015-09-10 Thread Sven R. Kunze
I need to add: you need to look up the stack to see if you have been called by __main__ and if __main__.__file__ is missing. Implementation: I would write decorator for your func. Best, Sven PS: did I say it would probably be a bad idea? If not, it would probably be a bad idea. PPS: what is

Re: Context-aware return

2015-09-10 Thread Ben Finney
"Sven R. Kunze" writes: > http://stackoverflow.com/questions/2356399/tell-if-python-is-in-interactive-mode I'm pretty sure Steven knows full well the answer to that question, which is not anything like the one he asked. Would you care to read the question he did ask? -- \ “The optimist

Re: Context-aware return

2015-09-10 Thread Ben Finney
Steven D'Aprano writes: > I have a function which is intended for use at the interactive > interpreter, but may sometimes be used non-interactively. I wish to > change it's output depending on the context of how it is being called. > […] > > x = func() > assert x == 999 > > L = [1, 2, func(), 4]

Re: Context-aware return

2015-09-10 Thread Sven R. Kunze
http://stackoverflow.com/questions/2356399/tell-if-python-is-in-interactive-mode On 10.09.2015 19:54, Steven D'Aprano wrote: I have a function which is intended for use at the interactive interpreter, but may sometimes be used non-interactively. I wish to change it's output depending on the con

Re: context manager based alternative to Re: Proposal: === and !===

2014-07-11 Thread Chris Angelico
On Sat, Jul 12, 2014 at 8:37 AM, Cameron Simpson wrote: > On 11Jul2014 14:37, Chris Angelico wrote: >> >> Does C-level code have to check this flag before comparing >> nans, > > > If you mean: > > float x, y; > [...] > if (x == y) { > action... > } > > then no. > > >> or is this appli

Re: context manager based alternative to Re: Proposal: === and !===

2014-07-11 Thread Cameron Simpson
On 11Jul2014 14:37, Chris Angelico wrote: On Fri, Jul 11, 2014 at 11:17 AM, Roy Smith wrote: In article , Cameron Simpson wrote: [... context manager changing NaN comparisons ...] I'm a bit wary of anything that makes a global, even if temporary, change to comparisons' behaviours. What hap

Re: context manager based alternative to Re: Proposal: === and !===

2014-07-11 Thread Cameron Simpson
On 10Jul2014 17:34, Ethan Furman wrote: On 07/10/2014 05:20 PM, Cameron Simpson wrote: Here's an alternative proposal that doesn't involve a new operator. [snip float-context manager stuff] Decimal has a context manager like that already (I don't know if it supports allowing NaNs to equal ea

Re: context manager based alternative to Re: Proposal: === and !===

2014-07-10 Thread Chris Angelico
On Fri, Jul 11, 2014 at 11:17 AM, Roy Smith wrote: > In article , > Cameron Simpson wrote: > >> Q: How many user support people does it take to change a light bulb? >> A: We have an exact copy of the light bulb here and it seems to be >> working fine. Can you tell me what kind of system you

Re: context manager based alternative to Re: Proposal: === and !===

2014-07-10 Thread Roy Smith
In article , Cameron Simpson wrote: > Q: How many user support people does it take to change a light bulb? > A: We have an exact copy of the light bulb here and it seems to be > working fine. Can you tell me what kind of system you have? So, what are we talking about here? my_lightbulb ==

Re: context manager based alternative to Re: Proposal: === and !===

2014-07-10 Thread Ethan Furman
On 07/10/2014 05:20 PM, Cameron Simpson wrote: I posted this the other day and haven't seen a response, not even a scathing rejection... Here's an alternative proposal that doesn't involve a new operator. [snip float-context manager stuff] Decimal has a context manager like that already (I

Re: context aware execution

2012-12-19 Thread Michael Torrie
On 12/19/2012 09:51 AM, Bart Thate wrote: > Think of sending JSON over the wire, reconstruct an object with it and then > let the object figure out what it can and cannot do in this external > environment. Probably the better way to do it is to formally define an API that lets an object discover t

Re: context aware execution

2012-12-19 Thread Bart Thate
Thanks for your response Chris ! Ha ! the job of the mad man is todo the things the are not "advisable" and see what gives. Like why it is not advisable and, if possible, their are ways to achieve things that are previously overseen. i already do a lot of travelling of the callstack to see from

Re: context aware execution

2012-12-19 Thread Wayne Werner
On Thu, 20 Dec 2012, Chris Angelico wrote: On Thu, Dec 20, 2012 at 2:57 AM, Bart Thate wrote: I want in a function or method determine the context of my caller and adapt the functionality accordingly. First off, please don't! Your code will be *extremely* confusing. Usually, the best way to

Re: context aware execution

2012-12-19 Thread Chris Angelico
On Thu, Dec 20, 2012 at 2:57 AM, Bart Thate wrote: > Hi All ! > > Is is possible and if yes, is it more easily possible (i am thinking f_back > maybe) to get the context of the caller when in a function ? > > Like to which variable name is this object assigned ? > > Or whatever of the callers cont

Re: Context manager to save/restore a name binding

2012-08-31 Thread Chris Withers
Hi Ben, On 31/08/2012 03:36, Ben Finney wrote: That way, I can set ‘sys.dont_write_bytecode’ to the value I need in this part of the code, knowing that however the code continues the previous value of that setting will be restored to whatever it was before I touched it. Have I re-invented a con

Re: Context manager to save/restore a name binding

2012-08-31 Thread Ben Finney
Peter Otten <__pete...@web.de> writes: > You should wrap yield in a try ... finally. You might allow setting > the new value in the manager (untested): Thank you, both good advice. I would still like to know if Python already has something to make this unnecessary. -- \ “Compulsory unifi

Re: Context manager to save/restore a name binding

2012-08-30 Thread Peter Otten
Ben Finney wrote: > I have written a context manager to save and restore a name binding:: > > import contextlib > > @contextlib.contextmanager > def preserve_value(namespace, name): > """ A context manager to preserve, then restore, the specified > binding. > >

RE: Context Manager getting str instead of AttributeError instance

2012-03-15 Thread Peter Otten
Prasad, Ramit wrote: >> Prasad, Ramit wrote: >> >> > So I have a context manager used to catch errors >> > >> > def __exit__( self, exceptionClass, exception, tracebackObject ): >> > if isinstance( exception, self.exceptionClasses ): >> > #do something here >> > >> > Normally excepti

Re: Context Manager getting str instead of AttributeError instance

2012-03-15 Thread Ian Kelly
On Thu, Mar 15, 2012 at 2:25 PM, Prasad, Ramit wrote: >> > ... >> > (, "'A' object has no attribute 'x'", >> ) >> > AttributeError: 'A' object has no attribute 'x' >> > >> > As you can see, I am getting a string while you are not. >> >>Ian Kelly said: >> Looks like a version difference.  I don't h

RE: Context Manager getting str instead of AttributeError instance

2012-03-15 Thread Prasad, Ramit
> > ... > > (, "'A' object has no attribute 'x'", > ) > > AttributeError: 'A' object has no attribute 'x' > > > > As you can see, I am getting a string while you are not. > >Ian Kelly said: > Looks like a version difference. I don't have Python 2.6 handy to > test on, but I get a str in Python 2

Re: Context Manager getting str instead of AttributeError instance

2012-03-15 Thread Ian Kelly
On Thu, Mar 15, 2012 at 1:10 PM, Prasad, Ramit wrote: >> Prasad, Ramit wrote: >> >> > So I have a context manager used to catch errors >> > >> > def __exit__( self, exceptionClass, exception, tracebackObject ): >> >     if isinstance( exception, self.exceptionClasses ): >> >          #do something

RE: Context Manager getting str instead of AttributeError instance

2012-03-15 Thread Prasad, Ramit
> Prasad, Ramit wrote: > > > So I have a context manager used to catch errors > > > > def __exit__( self, exceptionClass, exception, tracebackObject ): > > if isinstance( exception, self.exceptionClasses ): > > #do something here > > > > Normally exception would be the exception insta

Re: Context Manager getting str instead of AttributeError instance

2012-03-15 Thread Peter Otten
Prasad, Ramit wrote: > So I have a context manager used to catch errors > > def __exit__( self, exceptionClass, exception, tracebackObject ): > if isinstance( exception, self.exceptionClasses ): > #do something here > > Normally exception would be the exception instance, but for > A

Re: Context manager with class methods

2011-09-23 Thread Gregory Ewing
Terry Reedy wrote: it is normal to look for special methods on the class (and superclasses) > of an object rather than starting with the object itself. I suspect there was a deliberate change to correct an anomaly, though this might have been done as part of some other change. It's a necess

Re: Context manager with class methods

2011-09-22 Thread Terry Reedy
On 9/22/2011 6:21 AM, Gavin Panella wrote: On Python 2.6 and 3.1 the following code works fine: class Foo(object): @classmethod def __enter__(cls): print("__enter__") @classmethod def __exit__(cls, exc_type, exc_value, traceback):

Re: Context manager with class methods

2011-09-22 Thread Mel
Mel wrote: > This seems to work: > > > > class MetaWith (type): > @classmethod > def __enter__(cls): > print("__enter__") > > @classmethod > def __exit__(cls, exc_type, exc_value, traceback): > print("__exit__") > > class With (object): > __metaclass__ = Met

Re: Context manager with class methods

2011-09-22 Thread Mel
Gavin Panella wrote: > Hi, > > On Python 2.6 and 3.1 the following code works fine: > > class Foo(object): > > @classmethod > def __enter__(cls): > print("__enter__") > > @classmethod > def __exit__(cls, exc_type, exc_value, traceback): >

Re: Context manager with class methods

2011-09-22 Thread Thomas Rachel
Am 22.09.2011 12:21 schrieb Gavin Panella: Hi, On Python 2.6 and 3.1 the following code works fine: class Foo(object): @classmethod def __enter__(cls): print("__enter__") @classmethod def __exit__(cls, exc_type, exc_value, traceback):

Re: Context manager to temporarily change the variable of a register [aka write swap(a,b)]

2009-08-26 Thread Evan Driscoll
On Aug 26, 10:15 am, Carl Banks wrote: > Well, it wouldn't be a "can I rebind a variable using a with- > statement" thread if someone didn't post a solution that they thought > worked, but didn't test it on local variables. I'm not going to deny it was pretty stupid... though in my defense, I'm n

Re: Context manager to temporarily change the variable of a register [aka write swap(a,b)]

2009-08-26 Thread Carl Banks
On Aug 25, 1:07 pm, Evan Driscoll wrote: > On Aug 25, 2:33 pm, Evan Driscoll wrote: > > > I want to make a context manager that will temporarily change the > > value of a variable within the scope of a 'with' that uses it. This is > > inspired by a C++ RAII object I've used in a few projects. Ide

Re: Context manager to temporarily change the variable of a register [aka write swap(a,b)]

2009-08-26 Thread Diez B. Roggisch
Evan Driscoll schrieb: On Aug 25, 3:47 pm, Evan Driscoll wrote: So here is my simplified version that only works for globals: So I think this works if (1) you only use changed_value in the same module as it's defined in (otherwise it picks up the globals from the module it's defined in, which

Re: Context manager to temporarily change the variable of a register [aka write swap(a,b)]

2009-08-25 Thread Evan Driscoll
On Aug 25, 3:47 pm, Evan Driscoll wrote: > So here is my simplified version that only works for globals: So I think this works if (1) you only use changed_value in the same module as it's defined in (otherwise it picks up the globals from the module it's defined in, which is almost certainly not

Re: Context manager to temporarily change the variable of a register [aka write swap(a,b)]

2009-08-25 Thread Evan Driscoll
On Aug 25, 3:25 pm, "Diez B. Roggisch" wrote: > Modifying locals isn't really allowed - it might stop working with > certain implementations of python. > > And to be honest - I don't really see a use-case for your whole > approache. Why don't you want to introduce a new name? Wow, this actually w

Re: Context manager to temporarily change the variable of a register [aka write swap(a,b)]

2009-08-25 Thread Diez B. Roggisch
Evan Driscoll schrieb: On Aug 25, 2:33 pm, Evan Driscoll wrote: I want to make a context manager that will temporarily change the value of a variable within the scope of a 'with' that uses it. This is inspired by a C++ RAII object I've used in a few projects. Ideally, what I want is something l

Re: Context manager to temporarily change the variable of a register [aka write swap(a,b)]

2009-08-25 Thread Evan Driscoll
On Aug 25, 3:07 pm, Evan Driscoll wrote: > Okay, so I think I actually got this with some consultation with a > friend. Critiques? This is wrong; it's not quite working right. It does with the example I posted, but not in a more thorough test. I'm still ignoring the "you can't do this" answers f

Re: Context manager to temporarily change the variable of a register [aka write swap(a,b)]

2009-08-25 Thread Diez B. Roggisch
Evan Driscoll schrieb: (If you don't want to read the following, note that you can answer my question by writing a swap function.) I want to make a context manager that will temporarily change the value of a variable within the scope of a 'with' that uses it. This is inspired by a C++ RAII objec

Re: Context manager to temporarily change the variable of a register [aka write swap(a,b)]

2009-08-25 Thread Evan Driscoll
On Aug 25, 2:33 pm, Evan Driscoll wrote: > I want to make a context manager that will temporarily change the > value of a variable within the scope of a 'with' that uses it. This is > inspired by a C++ RAII object I've used in a few projects. Ideally, > what I want is something like the following:

Re: Context manager to temporarily change the variable of a register [aka write swap(a,b)]

2009-08-25 Thread Emile van Sebille
On 8/25/2009 12:33 PM Evan Driscoll said... So my question is: is what I want possible to do in Python? Probably not with immutables (as in your example) -- maybe otherwise. Emile -- http://mail.python.org/mailman/listinfo/python-list

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-19 Thread Gunter Henriksen
> > If there is a function which triggers a one-shot switch, I like > > to have a way to find out if it has already been triggered, I > > prefer to have the function tell me if it triggered the switch > > or not, but I would not want that to be by raising an exception. > > In this case, though, we'

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-19 Thread MRAB
Duncan Booth wrote: Ben Finney wrote: MRAB writes: Gunter Henriksen wrote: If there is a function which triggers a one-shot switch, I like to have a way to find out if it has already been triggered, I prefer to have the function tell me if it triggered the switch or not, but I would not wa

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-19 Thread Ben Finney
Gunter Henriksen writes: > If there is a function which triggers a one-shot switch, I like to > have a way to find out if it has already been triggered, I prefer to > have the function tell me if it triggered the switch or not, but I > would not want that to be by raising an exception. In this c

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-19 Thread Duncan Booth
Ben Finney wrote: > MRAB writes: > >> Gunter Henriksen wrote: >> > If there is a function which triggers a one-shot switch, I like to >> > have a way to find out if it has already been triggered, I prefer >> > to have the function tell me if it triggered the switch or not, but >> > I would not

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-18 Thread Ben Finney
MRAB writes: > Gunter Henriksen wrote: > > If there is a function which triggers a one-shot switch, I like to > > have a way to find out if it has already been triggered, I prefer to > > have the function tell me if it triggered the switch or not, but I > > would not want that to be by raising an

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-18 Thread MRAB
Gunter Henriksen wrote: Anyone else? If there is a function which triggers a one-shot switch, I like to have a way to find out if it has already been triggered, I prefer to have the function tell me if it triggered the switch or not, but I would not want that to be by raising an exception. Th

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-18 Thread Gunter Henriksen
> Anyone else? If there is a function which triggers a one-shot switch, I like to have a way to find out if it has already been triggered, I prefer to have the function tell me if it triggered the switch or not, but I would not want that to be by raising an exception. -- http://mail.python.org/ma

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-16 Thread Ben Finney
Carl Banks writes: > I don't think this is anything more than a trivial consideration, Okay, thank you. Anyone else? -- \ “All television is educational television. The question is: | `\ what is it teaching?” —Nicholas Johnson | _o__)

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-16 Thread Carl Banks
On May 16, 8:20 pm, Ben Finney wrote: > Carl Banks writes: > > There's already precedent for what to do in the Python library. > > > Python 2.5.2 (r252:60911, Jan  4 2009, 17:40:26) > > [GCC 4.3.2] on linux2 > > Type "help", "copyright", "credits" or "license" for more information. > > >>> f = op

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-16 Thread Ben Finney
Carl Banks writes: > There's already precedent for what to do in the Python library. > > Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) > [GCC 4.3.2] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> f = open('somefile') > >>> f.close() > >>> f.close() >

Re: Context manager, atexit processing, and PEP 3143 DaemonContext.close

2009-05-16 Thread Carl Banks
On May 16, 5:50 pm, Ben Finney wrote: > Ideas? How should this be addressed both Pythonically and respecting the > intent of PEP 3143? There's already precedent for what to do in the Python library. Python 2.5.2 (r252:60911, Jan 4 2009, 17:40:26) [GCC 4.3.2] on linux2 Type "help", "copyright",

Re: context not cleaned at the end of a loop containing yield?

2009-03-26 Thread Mensanator
On Mar 26, 1:34 pm, MRAB wrote: > TP wrote: > > Hi everybody, > > > This example gives strange results: > > > > > def foo( l = [] ): > > >     l2 = l > >     print l2 > >     for i in range(10): > >         if i%2 == 0: > >             l2.append( i ) > >         yield i > > > > >

Re: context not cleaned at the end of a loop containing yield?

2009-03-26 Thread MRAB
TP wrote: Hi everybody, This example gives strange results: def foo( l = [] ): l2 = l print l2 for i in range(10): if i%2 == 0: l2.append( i ) yield i [i for i in ut.foo()] [] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] [i for i in ut.foo()] [0

Re: Context manager for files vs garbage collection

2008-06-17 Thread Sebastian "lunar" Wiesner
Floris Bruynooghe <[EMAIL PROTECTED]>: > I was wondering when it was worthwil to use context managers for > file. Consider this example: > > def foo(): > t = False > for line in file('/tmp/foo'): > if line.startswith('bar'): > t = True > break > return

Re: Context manager for files vs garbage collection

2008-06-16 Thread Benjamin
On Jun 16, 8:24 am, Bruno Desthuilliers wrote: > > IIRC (please someone correct me if I'm wrong), proper release of file > resources as soon as the file object gets out of scope is not garanteed > in the language spec and is implementation dependant. Right. Resources are freed in CPython right af

Re: Context manager for files vs garbage collection

2008-06-16 Thread Bruno Desthuilliers
Floris Bruynooghe a écrit : Hi I was wondering when it was worthwil to use context managers for file. Consider this example: def foo(): t = False for line in file('/tmp/foo'): if line.startswith('bar'): t = True break return t What would the benefit

Re: context managers and generators

2007-01-12 Thread Bjoern Schliessmann
Laszlo Nagy wrote: >> I'm still thinking there is a better way to do it, and would >> appreciate any ideas. > > Everything can be implemented with goto and arrays. But is that really better? Regards, Björn -- BOFH excuse #174: Backbone adjustment -- http://mail.python.org/mailman/list

Re: context managers and generators

2007-01-12 Thread Laszlo Nagy
> I'm still thinking there is a better way to do it, and would appreciate > any ideas. > Everything can be implemented with goto and arrays. http://entrian.com/goto/ :-P -- http://mail.python.org/mailman/listinfo/python-list

Re: context managers and generators

2007-01-12 Thread [EMAIL PROTECTED]
Jean-Paul Calderone wrote: > On 12 Jan 2007 06:17:01 -0800, [EMAIL PROTECTED] wrote: > >I'm happily using context managers and co-routines, and would like to > >use both at the same time, e.g. > > Python has generators, not co-routines. They may be called generators, but I'm using them as co-routi

  1   2   >