Re: [BangPypers] How to run a block of code just before the interpreter is being exited like END{} in perl ??
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Sunday 08 September 2013 10:43 PM, Saager Mhatre wrote: With a verb name like *register* I'd much prefer to call the function as opposed to using it as a decorator. The more idiomatic decorators have noun names, viz- property, classmethod, staticmethod, unittest.expectedFailure, (or the one you used below) contextmanager, etc. I'll admit 'unittest.skipTest' is an anomaly, but I've always felt that the unittest module was a somewhat unpythonic 1-1 port of JUnit, so we'll let that one slide. :) The other reason in favour of the invocation syntax over the decorator syntax would be that the 'atexit.register' function is variadic and it would not be possible to send in the arguments to be passed in at exit if 'atexit.register' were used as a decorator. Of course, we could nest the decorated function in another function to provide a lexical closure that would then allow us to specify those arguments, but explicitly declaring the parameters to the cleanup function and explicitly passing them to register just seems much more idiomatic to me. Idiomatic code is always quite subjective. It is easier to differentiate between good code vs bad code than between really good code vs idiomatic (beautiful) code. I can write a very nasty looking decorator function which hides a lot of complexity (possibly with horrible code) and then decorate a function with it. Is it idiomatic - Probably not. But there would be people who think otherwise. I gave that example as it was given in the Python documentation for atexit module. http://docs.python.org/2/library/atexit.html#module-atexit [...] ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers I realize I'm a little late to the thread, but just thought I'd put in my 2c. - d ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers - -- Regards, - --Anand - -- Software Architect/Consultant anandpil...@letterboxes.org Please note my updated email address anandpil...@letterboxes.org. Kindly update your address books. -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBAgAGBQJSLdLPAAoJEMTxYeOp9eaoKtYIAL7wknmJk8G31PHnTNaALDDw stdHN7Ln0kvLnCCIJmkAQeSa3mFPkG127RHneglmoUXptFL/9KMWlQJz6YHnzMWd CHK3+PNeFaDDqMQpU9b/pzbzMEq/UM4BvtFPQB7LhKWZgWa/YeMRQNHk/jS6+fl+ y92zSY0gaZEwRJj85Yh+WNyMlHoj3dRHhBtVSTRikHiImBv0CMNx49HymBATUm8E z0G5eGd8W9okGkaiGjCcx4JSXZ+A9WiP3M46GUYY21I3Em+ocnxfD6wQIUzSlOj+ ZJgU653joCQbUTcwVBjEEsGc3OkavbMTilkuBg/30QeHf9H1ohgcq7zRQSEJ0Mc= =ibBh -END PGP SIGNATURE- ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] How to run a block of code just before the interpreter is being exited like END{} in perl ??
On Tue, Sep 3, 2013 at 1:27 AM, Anand B Pillai anandpil...@letterboxes.orgwrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Monday 02 September 2013 08:09 PM, venkatakrishnan g wrote: import atexit def callback(): print about to exit! atexit.register( callback ) Better use it as decorator. import atexit @atexit.register def last_man_standing(): print Hasta la vista, Baby.p With a verb name like *register* I'd much prefer to call the function as opposed to using it as a decorator. The more idiomatic decorators have noun names, viz- property, classmethod, staticmethod, unittest.expectedFailure, (or the one you used below) contextmanager, etc. I'll admit 'unittest.skipTest' is an anomaly, but I've always felt that the unittest module was a somewhat unpythonic 1-1 port of JUnit, so we'll let that one slide. :) The other reason in favour of the invocation syntax over the decorator syntax would be that the 'atexit.register' function is variadic and it would not be possible to send in the arguments to be passed in at exit if 'atexit.register' were used as a decorator. Of course, we could nest the decorated function in another function to provide a lexical closure that would then allow us to specify those arguments, but explicitly declaring the parameters to the cleanup function and explicitly passing them to register just seems much more idiomatic to me. [...] - -- Regards, - --Anand - -- Software Architect/Consultant anandpil...@letterboxes.org Please note my updated email address anandpil...@letterboxes.org. Kindly update your address books. -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBAgAGBQJSJO29AAoJEHKU2n17CpvDSK4IALaLNb/tP3MjUE6EvQskn8EQ SLg2BuUY0dy2f+QfHQ2bBZLj3unuHHPHlugfK8wGbNpB80rGm0lpoyqxiU7H2JnN aSXGyAS435VemqohOr26ZN/5nsYdSkz87KnJNlZ8YagPd+RaDZd/dc6BNM3er7va DioJnm+txAqIenv3xGeYNQfs7U7QljKcdrzqPjJuQp65ZmSUbcfmPls1I7B6Oxxe 1Xj+ax/N3wJeNKkWZKsvOJsHgQNMjNhTICfH8Ofsv0c04mlXI/Ej3+yIzQ/2tZwc k9jQrlX/Wgv+Mza41RPCtJTJGkbfFVTCOiQQjvwA4ZnriuwmTAcH2axELbSr1aU= =E2cM -END PGP SIGNATURE- ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers I realize I'm a little late to the thread, but just thought I'd put in my 2c. - d ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] How to run a block of code just before the interpreter is being exited like END{} in perl ??
On Tue, Sep 3, 2013 at 8:47 AM, Anand B Pillai anandpil...@letterboxes.orgwrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Tuesday 03 September 2013 06:27 AM, Anand Chitipothu wrote: This is another version of this by mucking around directly with sys.exitfunc using a with context. from contextlib import contextmanager @contextmanager def end_of_days(): def wrapper(): print 'Goodbye.' yield wrapper with end_of_days() as sys.exitfunc: print 3+4 How is this different from: import sys def goodbye(): print Goodbye sys.exitfunc = goodbye I don't think we are really doing anything with the context in your example except initializing sys.exitfunc. I'm with Anand C on this one, the context manager serves no real purpose here; the initialization of sys.exitfunc is entirely incidental and has little bearing on the fact that it is being initialized from the value returned by the context manager. This seems to me even more un-idiomatic than using atexit.register as a decorator. YMMV, but this is more like shoehorning in a 'cute' language feature just for the heck of it. Well, nothing really except maybe, @contextmanager def end_of_days(): before_context() def wrapper(): print 'Goodbye.' yield wrapper print after_context() with end_of_days() as sys.exitfunc: print 3+4 This just conflates context/resource management with registering cleanup functions, which seems entirely unnecessary. Just provides a context manager as well to do things before entering context and after exiting (just before sys.exit in this case), in a modular way. That would only hold if there's nothing following the with block in my thread of execution. In any other case the cleanup function will be executed much later and encapsulating it inside the context manager only elides the intent of the code and makes it harder to read. Even if this were just a single-file standalone script, I would much rather explicitly serialize the code. I honestly fail to see the modularity in this idiom. Anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers - -- Regards, - --Anand - -- Software Architect/Consultant anandpil...@letterboxes.org Please note my updated email address anandpil...@letterboxes.org. Kindly update your address books. -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBAgAGBQJSJVS7AAoJEHKU2n17CpvDbdsIAJD8T8AJp49jmE5l/tEpUCKu k5U0e3cTcy7rAevxhE40lFjvmTAENw6K85Eh1hTbVt0uRQDhdDs7Lv47WuVHEppH 28WlGkufwMHc5fIPb1XVbL7UbzPcqZd/6tUTXN+AAYZYtx2OB1rRiG6REhiFyY+Z +8hPwMWcs6n3oV8hCJbdFITaRM9pm4RozBolFVe+r5LWBp1IO56fSt0IFDS9gceI 5NhLI9JZReoqYEfK8cPtNBjM9y1tLGJ8AZP60E0n3WGgVZMFK0Mw1xloefIZvaBC xa5l81BVq+zov50WkXTQ/FVCGQpJM7l9A9XRfTznC5nnx2QVbitOPgFNNZpIYQc= =V14p -END PGP SIGNATURE- ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers Also, aside from the point about python idioms, most of this discussion is rendered moot by the fact that sys.exitfunchttp://docs.python.org/2/library/sys.html#sys.exitfunchas been flagged as deprecated in favour of atexit.register since 2.4 and suggesting it to a newbie just feels inappropriate. - d ___ BangPypers mailing list BangPypers@python.org https://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] How to run a block of code just before the interpreter is being exited like END{} in perl ??
babmis bab...@outlook.com writes: [...] Really. Please use the body of the email for the message and the subject line for the subject. -- Cordially, Noufal http://nibrahim.net.in ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] How to run a block of code just before the interpreter is being exited like END{} in perl ??
import atexit def callback(): print about to exit! atexit.register( callback ) On 2 September 2013 20:00, babmis bab...@outlook.com wrote: __**_ BangPypers mailing list BangPypers@python.org http://mail.python.org/**mailman/listinfo/bangpypershttp://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] How to run a block of code just before the interpreter is being exited like END{} in perl ??
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Monday 02 September 2013 08:09 PM, venkatakrishnan g wrote: import atexit def callback(): print about to exit! atexit.register( callback ) Better use it as decorator. import atexit @atexit.register def last_man_standing(): print Hasta la vista, Baby.p This is another version of this by mucking around directly with sys.exitfunc using a with context. from contextlib import contextmanager @contextmanager def end_of_days(): def wrapper(): print 'Goodbye.' yield wrapper with end_of_days() as sys.exitfunc: print 3+4 On 2 September 2013 20:00, babmis bab...@outlook.com wrote: __**_ BangPypers mailing list BangPypers@python.org http://mail.python.org/**mailman/listinfo/bangpypershttp://mail.python.org/mailman/listinfo/bangpypers ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers - -- Regards, - --Anand - -- Software Architect/Consultant anandpil...@letterboxes.org Please note my updated email address anandpil...@letterboxes.org. Kindly update your address books. -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBAgAGBQJSJO29AAoJEHKU2n17CpvDSK4IALaLNb/tP3MjUE6EvQskn8EQ SLg2BuUY0dy2f+QfHQ2bBZLj3unuHHPHlugfK8wGbNpB80rGm0lpoyqxiU7H2JnN aSXGyAS435VemqohOr26ZN/5nsYdSkz87KnJNlZ8YagPd+RaDZd/dc6BNM3er7va DioJnm+txAqIenv3xGeYNQfs7U7QljKcdrzqPjJuQp65ZmSUbcfmPls1I7B6Oxxe 1Xj+ax/N3wJeNKkWZKsvOJsHgQNMjNhTICfH8Ofsv0c04mlXI/Ej3+yIzQ/2tZwc k9jQrlX/Wgv+Mza41RPCtJTJGkbfFVTCOiQQjvwA4ZnriuwmTAcH2axELbSr1aU= =E2cM -END PGP SIGNATURE- ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] How to run a block of code just before the interpreter is being exited like END{} in perl ??
This is another version of this by mucking around directly with sys.exitfunc using a with context. from contextlib import contextmanager @contextmanager def end_of_days(): def wrapper(): print 'Goodbye.' yield wrapper with end_of_days() as sys.exitfunc: print 3+4 How is this different from: import sys def goodbye(): print Goodbye sys.exitfunc = goodbye I don't think we are really doing anything with the context in your example except initializing sys.exitfunc. Anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers
Re: [BangPypers] How to run a block of code just before the interpreter is being exited like END{} in perl ??
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Tuesday 03 September 2013 06:27 AM, Anand Chitipothu wrote: This is another version of this by mucking around directly with sys.exitfunc using a with context. from contextlib import contextmanager @contextmanager def end_of_days(): def wrapper(): print 'Goodbye.' yield wrapper with end_of_days() as sys.exitfunc: print 3+4 How is this different from: import sys def goodbye(): print Goodbye sys.exitfunc = goodbye I don't think we are really doing anything with the context in your example except initializing sys.exitfunc. Well, nothing really except maybe, @contextmanager def end_of_days(): before_context() def wrapper(): print 'Goodbye.' yield wrapper print after_context() with end_of_days() as sys.exitfunc: print 3+4 Just provides a context manager as well to do things before entering context and after exiting (just before sys.exit in this case), in a modular way. Anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers - -- Regards, - --Anand - -- Software Architect/Consultant anandpil...@letterboxes.org Please note my updated email address anandpil...@letterboxes.org. Kindly update your address books. -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.11 (GNU/Linux) Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/ iQEcBAEBAgAGBQJSJVS7AAoJEHKU2n17CpvDbdsIAJD8T8AJp49jmE5l/tEpUCKu k5U0e3cTcy7rAevxhE40lFjvmTAENw6K85Eh1hTbVt0uRQDhdDs7Lv47WuVHEppH 28WlGkufwMHc5fIPb1XVbL7UbzPcqZd/6tUTXN+AAYZYtx2OB1rRiG6REhiFyY+Z +8hPwMWcs6n3oV8hCJbdFITaRM9pm4RozBolFVe+r5LWBp1IO56fSt0IFDS9gceI 5NhLI9JZReoqYEfK8cPtNBjM9y1tLGJ8AZP60E0n3WGgVZMFK0Mw1xloefIZvaBC xa5l81BVq+zov50WkXTQ/FVCGQpJM7l9A9XRfTznC5nnx2QVbitOPgFNNZpIYQc= =V14p -END PGP SIGNATURE- ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers