[Python-ideas] Efficient debug logging

2017-02-14 Thread Barry Scott
A common pattern I use is to have logging calls for debug and information with my applications. The logging calls can be separately enabled and disabled. For example: debug_log_enabled = False def debugLog( msg ): If debug_log_enabled: print( ‘Debug: %s’ % (msg,) ) Then the ca

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread Cory Benfield
> On 14 Feb 2017, at 15:51, Barry Scott wrote: > And have the evaluation of the argument skipped unless its dbg_log is enabled. > > I cannot see how to do this with python as it stands. > > Something would have to be added to allow python to short circuit the > argument tuple evaluation. > >

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread Kyle Lahnakoski
Can you wrap the expensive functions in lambdas? And have your logger evaluate it, only if required? > debugLog( ‘info is %r’ % (lambda: expensiveFunction(),) ) On 2017-02-14 10:51, Barry Scott wrote: > A common pattern I use is to have logging calls for debug and information > with my applica

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread Stephan Houben
Seems slightly simpler to just make debugLog accept a callable as an alternative to a string. debugLog(lambda:( ‘info is %s’ % expensiveFunction()) ) Op 14 feb. 2017 18:42 schreef "Kyle Lahnakoski" : Can you wrap the expensive functions in lambdas? And have your logger evaluate it, only if req

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread MRAB
On 2017-02-14 15:51, Barry Scott wrote: A common pattern I use is to have logging calls for debug and information with my applications. The logging calls can be separately enabled and disabled. For example: debug_log_enabled = False def debugLog( msg ): If debug_log_enabled:

[Python-ideas] Contraction for "for x in range()"

2017-02-14 Thread Mikhail V
I have a small syntax idea. In short, contraction of for x in range(a,b,c) : to for x in a,b,c : I really think there is something cute in it. So like a shortcut for range() which works only in for-in statement. So from syntactical POV, do you find it nice syntax? Visually it seems to me less b

Re: [Python-ideas] Contraction for "for x in range()"

2017-02-14 Thread Zachary Ware
On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: > I have a small syntax idea. > In short, contraction of > > for x in range(a,b,c) : > > to > > for x in a,b,c : > > I really think there is something cute in it. > So like a shortcut for range() which works only in for-in statement. > So from synt

Re: [Python-ideas] Contraction for "for x in range()"

2017-02-14 Thread Abe Dillon
I would be surprised if this hasn't been suggested many times before. It's similar to Matlab's syntax: for x = start:step:finish end Any such change would represent a large departure from normal python syntax for dubious gain. In general, you can put any expression after the `in` keyword so

Re: [Python-ideas] Define a method or function attribute outside of a class with the dot operator

2017-02-14 Thread Barry Warsaw
On Feb 14, 2017, at 12:48 PM, Steven D'Aprano wrote: >On Fri, Feb 10, 2017 at 09:05:49PM -0500, Terry Reedy wrote: >> Saving about 10 keystrokes is close to trivial. > >The same argument can be made for @decorator syntax. > >And, if I understand correctly, the same argument *was* made against >d

Re: [Python-ideas] Contraction for "for x in range()"

2017-02-14 Thread MRAB
On 2017-02-14 21:09, Zachary Ware wrote: On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: I have a small syntax idea. In short, contraction of for x in range(a,b,c) : to for x in a,b,c : I really think there is something cute in it. So like a shortcut for range() which works only in for-in

Re: [Python-ideas] Contraction for "for x in range()"

2017-02-14 Thread Todd
On Tue, Feb 14, 2017 at 4:41 PM, MRAB wrote: > On 2017-02-14 21:09, Zachary Ware wrote: > >> On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: >> >>> I have a small syntax idea. >>> In short, contraction of >>> >>> for x in range(a,b,c) : >>> >>> to >>> >>> for x in a,b,c : >>> >>> I really thin

Re: [Python-ideas] Contraction for "for x in range()"

2017-02-14 Thread Nick Timkovich
for i in range(...) is *sometimes* indicative of code smell, especially when then doing x[i], though it has its uses. I've never had a need to shorten a for...range line though. Other than it being "cute", do you have an example where it's definitively better? On Tue, Feb 14, 2017 at 4:03 PM, Tod

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread Abe Dillon
There are several mechanisms in the logging module to handle this use-case. First, note that logging functions can take multiple arguments ( https://docs.python.org/3/library/logging.html?highlight=logging#logging.Logger.debug ): >>> import logging >>> msg = "hello, %s! %s to %s you!" >>> args = (

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread Barry
> On 14 Feb 2017, at 17:39, Kyle Lahnakoski wrote: > > > Can you wrap the expensive functions in lambdas? And have your logger > evaluate it, only if required? > >> debugLog( ‘info is %r’ % (lambda: expensiveFunction(),) ) Interesting idea. I will bench mark and see what the cost of the lamba

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread Barry
> On 14 Feb 2017, at 17:03, Cory Benfield wrote: > > >> On 14 Feb 2017, at 15:51, Barry Scott wrote: >> And have the evaluation of the argument skipped unless its dbg_log is >> enabled. >> >> I cannot see how to do this with python as it stands. >> >> Something would have to be added to all

Re: [Python-ideas] Contraction for "for x in range()"

2017-02-14 Thread Mikhail V
On 14 February 2017 at 22:41, MRAB wrote: > On 2017-02-14 21:09, Zachary Ware wrote: > >> On Tue, Feb 14, 2017 at 3:06 PM, Mikhail V wrote: >> >>> I have a small syntax idea. >>> In short, contraction of >>> >>> for x in range(a,b,c) : >>> >>> to >>> >>> for x in a,b,c : >>> >>> I really think t

Re: [Python-ideas] Contraction for "for x in range()"

2017-02-14 Thread Nick Timkovich
Make some shim object that you can index into to get that functionality, could even call it Z (for the set of all integers). Short, and requires no new syntax. class IndexableRange: def __getitem__(self, item): if isinstance(item, slice): start = item.start if item.start is

Re: [Python-ideas] Contraction for "for x in range()"

2017-02-14 Thread Mikhail V
On 15 February 2017 at 00:41, Nick Timkovich wrote: > Make some shim object that you can index into to get that functionality, > could even call it Z (for the set of all integers). Short, and requires no > new syntax. > > class IndexableRange: > def __getitem__(self, item): > if isins

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread Abe Dillon
The point is that the cost of creating the msg argument can be very high. At the point that logging decides to skip output it is to late to save the cost of creating the arg tuple. This sounds like an optimization that's sufficiently rare and complex to warrant a custom fix or a 3rd p

Re: [Python-ideas] Efficient debug logging

2017-02-14 Thread Kyle Lahnakoski
On 2017-02-14 19:51, Abe Dillon wrote: >The point is that the cost of creating the msg argument can be very > high. > > At the point that logging decides to skip output it is to late to > save the cost of creating the arg tuple. > > This sounds like an optimization that's sufficiently

Re: [Python-ideas] Fwd: Define a method or function attributeoutsideof a class with the dot operator

2017-02-14 Thread Random832
On Sun, Feb 12, 2017, at 21:55, Steven D'Aprano wrote: > But honestly, no. This is not going to happen. .Net VB and C# have > something like this, as does Lua, and people still write classes the > ordinary way 99.99% of the time. The VB/C# thing you are referring to is, I assume, extension metho