Re: String concatenation vs. string formatting

2011-07-10 Thread Andrew Berg
-BEGIN PGP SIGNED MESSAGE- Hash: RIPEMD160 On 2011.07.10 09:33 AM, Roy Smith wrote: > The canonical way to do that would be something like > > fields = [demux_filter, field_filter, fpsin_filter, i2pfilter, > dn_filter, fpsout_filter, trim_filter, info_filter] > avs.write(''.join(fields)

Re: String concatenation vs. string formatting

2011-07-10 Thread Steven D'Aprano
Roy Smith wrote: > The canonical way to do that would be something like > > fields = [demux_filter, > field_filter, > fpsin_filter, > i2pfilter, > dn_filter, > fpsout_filter, > trim_filter, > info_filter] > avs.write(''.join(fi

Re: String concatenation vs. string formatting

2011-07-10 Thread Roy Smith
In article , Andrew Berg wrote: > How should I go about switching from concatenation to string formatting > for this? > > avs.write(demux_filter + field_filter + fpsin_filter + i2pfilter + > dn_filter + fpsout_filter + trim_filter + info_filter) > > I can think of a few ways, but none of them

Re: String concatenation vs. string formatting

2011-07-10 Thread Steven D'Aprano
Andrew Berg wrote: > How should I go about switching from concatenation to string formatting > for this? > > avs.write(demux_filter + field_filter + fpsin_filter + i2pfilter + > dn_filter + fpsout_filter + trim_filter + info_filter) > > I can think of a few ways, but none of them are pretty. fi

Re: String concatenation vs. string formatting

2011-07-10 Thread Andrew Berg
-BEGIN PGP SIGNED MESSAGE- Hash: RIPEMD160 On 2011.07.10 04:47 AM, Vinay Sajip wrote: > You don't need logutils, just the BraceMessage class - which is > shown in the blog post (around 10 lines). Feel free to use it with > copy and paste :-) I didn't realize that was the actual class when

Re: String concatenation vs. string formatting

2011-07-10 Thread Vinay Sajip
Andrew Berg gmail.com> writes: > On 2011.07.10 02:23 AM, Vinay Sajip wrote: > > There are examples in the blog post I linked to earlier: > It seems that would require logutils. I'm trying to keep dependencies to > a minimum in my project, but I'll take a look at logutils and see if > there's anyt

Re: String concatenation vs. string formatting

2011-07-10 Thread Andrew Berg
-BEGIN PGP SIGNED MESSAGE- Hash: RIPEMD160 On 2011.07.10 02:23 AM, Vinay Sajip wrote: > There are examples in the blog post I linked to earlier: It seems that would require logutils. I'm trying to keep dependencies to a minimum in my project, but I'll take a look at logutils and see if the

Re: String concatenation vs. string formatting

2011-07-10 Thread Vinay Sajip
Andrew Berg gmail.com> writes: > How would I do that with the newer formatting? I've tried: There are examples in the blog post I linked to earlier: http://plumberjack.blogspot.com/2010/10/supporting-alternative-formatting.html Regards, Vinay Sajip -- http://mail.python.org/mailman/listinf

Re: String concatenation vs. string formatting

2011-07-09 Thread Andrew Berg
-BEGIN PGP SIGNED MESSAGE- Hash: RIPEMD160 How should I go about switching from concatenation to string formatting for this? avs.write(demux_filter + field_filter + fpsin_filter + i2pfilter + dn_filter + fpsout_filter + trim_filter + info_filter) I can think of a few ways, but none of th

Re: String concatenation vs. string formatting

2011-07-09 Thread Andrew Berg
-BEGIN PGP SIGNED MESSAGE- Hash: RIPEMD160 On 2011.07.10 12:55 AM, Dennis Lee Bieber wrote: > Maybe it's been removed, but from the help file for my installation help(file) returns a NameError in 3.2. It shows up as a built-in function in the 2.7 docs, but not in the py3k docs. It's not me

Re: String concatenation vs. string formatting

2011-07-09 Thread Andrew Berg
-BEGIN PGP SIGNED MESSAGE- Hash: RIPEMD160 On 2011.07.09 11:04 PM, Andrew Berg wrote: > > Is barf built-in as well? > That came off more hostile than I wanted, so I'll rephrase it: I doubt it has anything to do with built-ins, since it fails on a variable name that obviously does not re

Re: String concatenation vs. string formatting

2011-07-09 Thread Andrew Berg
-BEGIN PGP SIGNED MESSAGE- Hash: RIPEMD160 On 2011.07.09 09:54 PM, Dennis Lee Bieber wrote: > "file" is a built-in (related to "open"). Also: > Traceback (most recent call last): File > "C:\Users\Bahamut\workspace\Disillusion\disillusion.py", line 178, in > save_preset() File > "C:\Users\

Re: String concatenation vs. string formatting

2011-07-09 Thread Andrew Berg
-BEGIN PGP SIGNED MESSAGE- Hash: RIPEMD160 On 2011.07.09 09:54 PM, Dennis Lee Bieber wrote: > "file" is a built-in (related to "open"). It is? What is it? >>> type(file) Traceback (most recent call last): File "", line 1, in NameError: name 'file' is not defined I don't see it in

Re: String concatenation vs. string formatting

2011-07-09 Thread Andrew Berg
On 2011.07.09 06:06 AM, Vinay Sajip wrote: > In a logging context at least, using the form like > > logger.debug("formatting message with %s", "arguments") > > rather than > > logger.debug("formatting message with %s" % "arguments") How would I do that with the newer formatting? I've tried: > logge

Re: String concatenation vs. string formatting

2011-07-09 Thread Vinay Sajip
Andrew Berg gmail.com> writes: > Other than the case where a variable isn't a string (format() converts > variables to strings, automatically, right?) and when a variable is used > a bunch of times, concatenation is fine, but somehow, it seems wrong. > Sorry if this seems a bit silly, but I'm a n

Re: String concatenation vs. string formatting

2011-07-08 Thread Ian Kelly
On Sat, Jul 9, 2011 at 12:16 AM, Chris Angelico wrote: > Has the same optimization been implemented for Unicode? The page > doesn't mention Python 3 at all, and I would guess that the realloc > optimization would work fine for both types of string. Seems to be implemented for strs in 3.2, but not

Re: String concatenation vs. string formatting

2011-07-08 Thread Chris Angelico
On Sat, Jul 9, 2011 at 3:30 PM, Steven D'Aprano wrote: > It also doesn't generalise: only appends are optimized, not prepends. > > If you're interested in learning about the optimization: > > http://utcc.utoronto.ca/~cks/space/blog/python/ExaminingStringConcatOpt >From that page: "Also, this is o

Re: String concatenation vs. string formatting

2011-07-08 Thread Ian Kelly
On Fri, Jul 8, 2011 at 11:30 PM, Steven D'Aprano wrote: > Billy Mays wrote: > >> If it means anything, I think concatenation is faster. > > You are measuring the speed of an implementation-specific optimization. > You'll likely get *very* different results with Jython or IronPython, or > old versi

Re: String concatenation vs. string formatting

2011-07-08 Thread Steven D'Aprano
Billy Mays wrote: > If it means anything, I think concatenation is faster. You are measuring the speed of an implementation-specific optimization. You'll likely get *very* different results with Jython or IronPython, or old versions of CPython, or even if you use instance attributes instead of lo

Re: String concatenation vs. string formatting

2011-07-08 Thread Steven D'Aprano
Andrew Berg wrote: > Is it bad practice to use this >> logger.error(self.preset_file + ' could not be stored - ' + >> sys.exc_info()[1]) > Instead of this? >> logger.error('{file} could not be stored - >> {error}'.format(file=self.preset_file, error=sys.exc_info()[1])) > > > Other than the case

Re: String concatenation vs. string formatting

2011-07-08 Thread Thorsten Kampe
* John Gordon (Fri, 8 Jul 2011 20:23:52 + (UTC)) > I prefer this usage: > > logger.error('%s could not be stored - %s' % \ > (self.preset_file, sys.exc_info()[1])) The syntax for formatting logging messages according to the documentation is: Logger.error(msg, *args) NOT Logger.erro

Re: String concatenation vs. string formatting

2011-07-08 Thread Andrew Berg
On 2011.07.08 05:59 PM, Ben Finney wrote: > With the caveat that the formatting of that line should be using PEP 8 > indentation for clarity: PEP 8 isn't bad, but I don't agree with everything in it. Certain lines look good in chunks, some don't, at least to me. It's quite likely I'm going to be wr

Re: String concatenation vs. string formatting

2011-07-08 Thread Ben Finney
Ben Finney writes: > logger.error( > '{0} could not be stored - {1}'.format( > (self.preset_file, sys.exc_info()[1])) > > I usually prefer to use named placeholders instead of positional, but > this duplicates your original. Ah, I see that the OP *did* use named placeholders.

Re: String concatenation vs. string formatting

2011-07-08 Thread Dan Stromberg
On Fri, Jul 8, 2011 at 3:50 PM, Ben Finney wrote: > * The ‘%’ string formatting operator is superseded in current Python > versions by the more flexible ‘format’ method of string objects. > AFAIK, % formatting is the only kind of formatting that works portably across all of CPythons 2.5, 2.6, 2.

Re: String concatenation vs. string formatting

2011-07-08 Thread Ben Finney
Andrew Berg writes: > Is it bad practice to use this > > logger.error(self.preset_file + ' could not be stored - ' + > > sys.exc_info()[1]) This is not necessarily bad practice, but there are not many points in its favour. It's inflexible and makes the eventual formatting harder to discern. > I

Re: String concatenation vs. string formatting

2011-07-08 Thread Ben Finney
John Gordon writes: > I prefer this usage: > > logger.error('%s could not be stored - %s' % \ > (self.preset_file, sys.exc_info()[1])) That can be improved by learning two things: * The backslash-linebreak is ugly and fragile, and almost never needed, since Python knows to continue a st

Re: String concatenation vs. string formatting

2011-07-08 Thread Ian Kelly
On Fri, Jul 8, 2011 at 3:23 PM, Benjamin Kaplan wrote: > String formatting is the One Right Way here. It's fine to use string > concatenation for a few things, but the operation is O(n^2) because each > concat occurs one at a time: Python allocates space for a string the size of > the first 2 thin

Re: String concatenation vs. string formatting

2011-07-08 Thread Benjamin Kaplan
On Fri, Jul 8, 2011 at 1:18 PM, Andrew Berg wrote: > Is it bad practice to use this > > logger.error(self.preset_file + ' could not be stored - ' + > > sys.exc_info()[1]) > Instead of this? > > logger.error('{file} could not be stored - > > {error}'.format(file=self.preset_file, error=sys.exc_info

Re: String concatenation vs. string formatting

2011-07-08 Thread Billy Mays
On 07/08/2011 04:18 PM, Andrew Berg wrote: Is it bad practice to use this logger.error(self.preset_file + ' could not be stored - ' + sys.exc_info()[1]) Instead of this? logger.error('{file} could not be stored - {error}'.format(file=self.preset_file, error=sys.exc_info()[1])) Other than th

Re: String concatenation vs. string formatting

2011-07-08 Thread John Gordon
In Andrew Berg writes: > Is it bad practice to use this > > logger.error(self.preset_file + ' could not be stored - ' + > > sys.exc_info()[1]) > Instead of this? > > logger.error('{file} could not be stored - > > {error}'.format(file=self.preset_file, error=sys.exc_info()[1])) > Other than the

String concatenation vs. string formatting

2011-07-08 Thread Andrew Berg
Is it bad practice to use this > logger.error(self.preset_file + ' could not be stored - ' + > sys.exc_info()[1]) Instead of this? > logger.error('{file} could not be stored - > {error}'.format(file=self.preset_file, error=sys.exc_info()[1])) Other than the case where a variable isn't a string (f