Fun with python string formatting

2022-12-18 Thread Axy via Python-list
Hi all, what do you see looking at format string syntax https://docs.python.org/3/library/string.html#formatstrings ? In particular, at something like this: {h[1].red.jumbo-header:Hello, World!} Yes, this is syntactically correct statement and if we tweak Formatter methods, we can generate

Re: print & string formatting

2018-07-22 Thread Sharan Basappa
On Sunday, 22 July 2018 10:24:55 UTC+5:30, Cameron Simpson wrote: > On 21Jul2018 21:33, Sharan Basappa wrote: > >I get a lot confused while using print functions in Python. > > > >For example, I get the same results for the following code: > > > >str = "one two three" > > Pleasetry not to name v

Re: print & string formatting

2018-07-22 Thread Thomas Jollans
On 22/07/18 06:54, Cameron Simpson wrote: > On 21Jul2018 21:33, Sharan Basappa wrote: >> I get a lot confused while using print functions in Python. >> >> For example, I get the same results for the following code: >> >> str = "one two three" > > Pleasetry not to name variables after builtin clas

Re: print & string formatting

2018-07-21 Thread Cameron Simpson
On 21Jul2018 21:33, Sharan Basappa wrote: I get a lot confused while using print functions in Python. For example, I get the same results for the following code: str = "one two three" Pleasetry not to name variables after builtin classes ("str" is the name of Python's string class). prin

Re: print & string formatting

2018-07-21 Thread Chris Angelico
On Sun, Jul 22, 2018 at 2:53 PM, Abdur-Rahmaan Janhangeer wrote: > *Folks,I get a lot confused while using print functions in Python.For > example, I get the same results for the following code:str = "one two > three"print strprint "%s" %(str)So, what is the need to use the second > method which I

Re: print & string formatting

2018-07-21 Thread Abdur-Rahmaan Janhangeer
*Folks,I get a lot confused while using print functions in Python.For example, I get the same results for the following code:str = "one two three"print strprint "%s" %(str)So, what is the need to use the second method which I see being used in many programs I am referring to* well 1) that is more

print & string formatting

2018-07-21 Thread Sharan Basappa
Folks, I get a lot confused while using print functions in Python. For example, I get the same results for the following code: str = "one two three" print str print "%s" %(str) So, what is the need to use the second method which I see being used in many programs I am referring to -- https:/

Re: String Formatting with new .format()

2018-03-28 Thread Chris Angelico
On Thu, Mar 29, 2018 at 1:54 AM, Dan Stromberg wrote: > On Wed, Mar 28, 2018 at 7:30 AM, Ganesh Pal wrote: >>> >>> Or maybe they're not giving the same result. I'm a little confused here. >>> >> >> >> My Bad and Apologies , I should be fined for pasting wrong question. >> >> Actually I wanted t

Re: String Formatting with new .format()

2018-03-28 Thread Dan Stromberg
On Wed, Mar 28, 2018 at 7:30 AM, Ganesh Pal wrote: >> >> Or maybe they're not giving the same result. I'm a little confused here. >> > > > My Bad and Apologies , I should be fined for pasting wrong question. > > Actually I wanted to know if its ok to use just empty {} with .format() > or use {

Re: String Formatting with new .format()

2018-03-28 Thread Frank Millman
"Ganesh Pal" wrote in message news:CACT3xuUmOzR=5G9=zaf3fp2lytbgjv74vsyjfsvsifo77lf...@mail.gmail.com... Actually I wanted to know if its ok to use just empty {} with .format() or use {} with values i.e {0} {1} both will give the same results anyway The benefit of using empty {} is that yo

Re: String Formatting with new .format()

2018-03-28 Thread Ganesh Pal
> > Or maybe they're not giving the same result. I'm a little confused here. > My Bad and Apologies , I should be fined for pasting wrong question. Actually I wanted to know if its ok to use just empty {} with .format() or use {} with values i.e {0} {1} both will give the same results anyway

Re: String Formatting with new .format()

2018-03-27 Thread Michael Torrie
On 03/26/2018 09:37 AM, Ganesh Pal wrote: > Hi Team, > > Just a quick suggestion, on string formatting with .format() which of the > below is better , given both give the same result . No they don't. Look more closely at the output. >>>> attempts = 1 >>

Re: String Formatting with new .format()

2018-03-27 Thread Ganesh Pal
> > > Or maybe they're not giving the same result. I'm a little confused here. > > Thanks Chris, for the reply they appear to give the same result . -- https://mail.python.org/mailman/listinfo/python-list

Re: String Formatting with new .format()

2018-03-26 Thread W Yg
在 2018年3月26日星期一 UTC+8下午11:37:46,Ganesh Pal写道: > Hi Team, > > Just a quick suggestion, on string formatting with .format() which of the > below is better , given both give the same result . > > >>> attempts = 1 > >>> msg2 = "Hello" > >>

Re: String Formatting with new .format()

2018-03-26 Thread Chris Angelico
On Tue, Mar 27, 2018 at 2:37 AM, Ganesh Pal wrote: > Hi Team, > > Just a quick suggestion, on string formatting with .format() which of the > below is better , given both give the same result . > >>>> attempts = 1 >>>> msg2 = "Hello" >>>

String Formatting with new .format()

2018-03-26 Thread Ganesh Pal
Hi Team, Just a quick suggestion, on string formatting with .format() which of the below is better , given both give the same result . >>> attempts = 1 >>> msg2 = "Hello" >>> print "Retry attempt:{0} for error:{1}".format(attempts,msg2) Retry

Re: String formatting

2018-03-25 Thread D'Arcy Cain
Was "Accessing parent objects." On 03/25/2018 12:26 PM, Jugurtha Hadjar wrote: >> print("I am {0.__class__.__name__} foo".format(self)) > > I prefer keyword arguments, but if I used it that way I'd do: > > print("I am {0} foo".format(self.__class__.__name__)) These are contrived examples. In r

Re: % string formatting - what special method is used for %d?

2016-12-11 Thread Veek M
Ian Kelly wrote: > On Sat, Dec 10, 2016 at 11:40 PM, Veek M wrote: >> Well take a look at this: >> ### >> #!/usr/bin/python >> >> class Foo(int): >> def __init__(self, value): >> self.value = value >> >> def __str__(self): >> print '

Re: % string formatting - what special method is used for %d?

2016-12-10 Thread Ian Kelly
On Sat, Dec 10, 2016 at 11:40 PM, Veek M wrote: > Well take a look at this: > ### > #!/usr/bin/python > > class Foo(int): > def __init__(self, value): > self.value = value > > def __str__(self): > print '__str__' > return str(

Re: % string formatting - what special method is used for %d?

2016-12-10 Thread Veek M
Steve D'Aprano wrote: > On Sat, 10 Dec 2016 06:06 pm, Veek M wrote: > >> When we do: >> >> print '%s %d' % ('hello', 10) >> >> what special method is being invoked internally within the string- >> format-specifier? > > %d requires the argument to be an int, or able to be converted to int > usi

Re: % string formatting - what special method is used for %d?

2016-12-10 Thread Steve D'Aprano
On Sat, 10 Dec 2016 06:06 pm, Veek M wrote: > When we do: > > print '%s %d' % ('hello', 10) > > what special method is being invoked internally within the string- > format-specifier? %d requires the argument to be an int, or able to be converted to int using the __int__ special method. py> cl

% string formatting - what special method is used for %d?

2016-12-09 Thread Veek M
When we do: print '%s %d' % ('hello', 10) what special method is being invoked internally within the string- format-specifier? format() invokes format__ print invokes __str__ I'm basically trying to make sense of: raise TypeError('urkle urkle %s' % list(dictionary)) <=> raise TypeError('urkle

Re: logging TypeError: not all arguments converted during string formatting

2016-09-15 Thread Peter Otten
gging/__init__.py", line 828, in format return fmt.format(record) File "/usr/lib/python3.4/logging/__init__.py", line 565, in format record.message = record.getMessage() File "/usr/lib/python3.4/logging/__init__.py", line 328, in getMessage msg = msg

Re: logging TypeError: not all arguments converted during string formatting

2016-09-15 Thread MRAB
Message() File "C:\Continuum\Anaconda3\lib\logging\__init__.py", line 330, in getMessage msg = msg % self.args TypeError: not all arguments converted during string formatting [snip] File "C:\Users\dweng\PycharmProjects\lumar_ingestion\ingestion_workflow_modules\import_to_d

logging TypeError: not all arguments converted during string formatting

2016-09-15 Thread Daiyue Weng
return fmt.format(record) File "C:\Continuum\Anaconda3\lib\logging\__init__.py", line 567, in format record.message = record.getMessage() File "C:\Continuum\Anaconda3\lib\logging\__init__.py", line 330, in getMessage msg = msg % self.args TypeError: not all argume

Re: TypeError: not all arguments converted during string formatting

2016-02-18 Thread Ganesh Pal
On Wed, Feb 17, 2016 at 7:32 PM, Chris Angelico wrote: > The print statement/function happily accepts multiple arguments, and > will join them according to a set of predefined rules. The logging > functions don't have those rules, so they take one message and some > optional parameters. Try this,

Re: TypeError: not all arguments converted during string formatting

2016-02-17 Thread Chris Angelico
On Thu, Feb 18, 2016 at 12:58 AM, Ganesh Pal wrote: > Iam on python 2.6 and Linux , I had replaced print out, err ret with > logging.info(out, err ,ret) in the below code . I am getting > > "TypeError: not all arguments converted during string formatting" > error an

Re: TypeError: not all arguments converted during string formatting

2016-02-17 Thread Ganesh Pal
logging.info(out, err ,ret) in the below code . I am getting > > "TypeError: not all arguments converted during string formatting" > error any quick suggestion > > > try: > out, err, ret = run(cmd, timeout=60) > # New line add

TypeError: not all arguments converted during string formatting

2016-02-17 Thread Ganesh Pal
Hi Team, Iam on python 2.6 and Linux , I had replaced print out, err ret with logging.info(out, err ,ret) in the below code . I am getting "TypeError: not all arguments converted during string formatting" error any quick suggestion try: out, err, ret = run(cmd,

Re: Cheat sheet for the new string formatting?

2015-06-09 Thread Mark Lawrence
On 09/06/2015 13:15, Skip Montanaro wrote: One thing which seems obvious now is that since format() delegates to the individual types for formatting, much of the documentation of this stuff must now be delegated to the individual types. However, I can't find anything about the formatting syntax

Re: Cheat sheet for the new string formatting?

2015-06-09 Thread Skip Montanaro
On Tue, Jun 9, 2015 at 7:29 AM, wrote: > Where have you looked? Have you read > https://docs.python.org/3/library/string.html#formatspec ? Yes, but I missed the None section. I looked closely at 'g', but didn't see anything like "this is the default". I will admit I was a bit frustrated to see t

Re: Cheat sheet for the new string formatting?

2015-06-09 Thread random832
On Tue, Jun 9, 2015, at 08:15, Skip Montanaro wrote: > Skip> Why don't floats support "{:.Ns}"? (I know I can use "{!s}".) > > random832> Why would they? The old style didn't support %.Ns either. > > Well, the old style does, though it appears the N is ignored: > > >>> "%5s" % -0.00666762259822

Re: Cheat sheet for the new string formatting?

2015-06-09 Thread Skip Montanaro
Skip> Why don't floats support "{:.Ns}"? (I know I can use "{!s}".) random832> Why would they? The old style didn't support %.Ns either. Well, the old style does, though it appears the N is ignored: >>> "%5s" % -0.00666762259822 '-0.00666762259822' It doesn't raise an exception though. (This i

Re: Cheat sheet for the new string formatting?

2015-06-08 Thread Mark Lawrence
On 08/06/2015 21:05, Steven K Knight wrote: June 8 2015 3:11 PM, "Skip Montanaro" mailto:%22Skip%20Montanaro%22%20>> wrote: I have so far ignored the new string formatting (you know, the stuff with all the braces, dots and brackets that make Python strings look lik

Re: Cheat sheet for the new string formatting?

2015-06-08 Thread random832
On Mon, Jun 8, 2015, at 16:32, Skip Montanaro wrote: > This is counterintuitive: > > >>> "{:.3}".format(-0.00666762259822) > '-0.00667' > >>> "{:.3f}".format(-0.00666762259822) > '-0.007' > >>> "%.3f" % -0.00666762259822 > '-0.007' > >>> "{:.3s}".format(-0.00666762259822) > ValueError Unknown form

Re: Cheat sheet for the new string formatting?

2015-06-08 Thread Serhiy Storchaka
On 08.06.15 23:32, Skip Montanaro wrote: This is counterintuitive: >>> "{:.3}".format(-0.00666762259822) '-0.00667' >>> "{:.3f}".format(-0.00666762259822) '-0.007' >>> "%.3f" % -0.00666762259822 '-0.007' >>> "{:.3s}".format(-0.00666762259822) ValueError Unknown format code 's' for object of

Re: Cheat sheet for the new string formatting?

2015-06-08 Thread Yann Kaiser
It just means significant digits in the general format, which alternates between 10-exponent notation and plain decimal notation. https://docs.python.org/3.4/library/string.html#format-specification-mini-language >>> '{:.3}'.format(0.356785) '3.57e-05' >>> '{:.3}'.format(0.0035678

Re: Cheat sheet for the new string formatting?

2015-06-08 Thread Skip Montanaro
This is counterintuitive: >>> "{:.3}".format(-0.00666762259822) '-0.00667' >>> "{:.3f}".format(-0.00666762259822) '-0.007' >>> "%.3f" % -0.00666762259822 '-0.007' >>> "{:.3s}".format(-0.00666762259822) ValueError Unknown format code 's' for object of type 'float' Why does the first form display f

Re: Cheat sheet for the new string formatting?

2015-06-08 Thread Steven K Knight
June 8 2015 3:11 PM, "Skip Montanaro" wrote: I have so far ignored the new string formatting (you know, the stuff with all the braces, dots and brackets that make Python strings look like Perl code ). I am still only using Python 2.7, but have recently started forcing mys

Re: Cheat sheet for the new string formatting?

2015-06-08 Thread Skip Montanaro
On Mon, Jun 8, 2015 at 3:05 PM, Steven K Knight wrote: > I think http://pyformat.info/ is what you're looking for. Perfect, thanks! S -- https://mail.python.org/mailman/listinfo/python-list

Cheat sheet for the new string formatting?

2015-06-08 Thread Skip Montanaro
I have so far ignored the new string formatting (you know, the stuff with all the braces, dots and brackets that make Python strings look like Perl code ). I am still only using Python 2.7, but have recently started forcing myself to use the print() function. I figure maybe I should also start to

Re: TypeError: not all arguments converted during string formatting

2013-12-13 Thread Peter Otten
or.execute(sql,(gender,main_link,cat,item_content,price,seller)) This looks very much like your previous mistake (1) Don't put quotes around the placeholders. They are probably interpreted as string literals. (2) The last placeholder is missing the % > error: > > > quer

Re: TypeError: not all arguments converted during string formatting

2013-12-13 Thread Ervin Hegedüs
"" may be you miss a "%" sign? > query = query % db.literal(args) > TypeError: not all arguments converted during string formatting cheers: a. -- https://mail.python.org/mailman/listinfo/python-list

TypeError: not all arguments converted during string formatting

2013-12-13 Thread Jai
| varchar(20) | YES | | NULL | | | created_on | timestamp| NO | | CURRENT_TIMESTAMP | | +--+--+--+-+---++ 8 rows in set (0.00 sec) +++ error: query = query % db.literal(args) TypeError: not all arguments converted during string formatting -- https://mail.python.org/mailman/listinfo/python-list

Re: Python String Formatting - passing both a dict and string to .format()

2013-11-26 Thread Michael Torrie
On 11/26/2013 05:01 PM, Victor Hooi wrote: > Hi, > > I'm trying to use Python's new style string formatting with a dict > and string together. > > For example, I have the following dict and string variable: > > my_dict = { 'cat': 'ernie',

Re: Python String Formatting - passing both a dict and string to .format()

2013-11-26 Thread Chris Kaynor
On Tue, Nov 26, 2013 at 4:01 PM, Victor Hooi wrote: > Hi, > > I'm trying to use Python's new style string formatting with a dict and > string together. > > For example, I have the following dict and string variable: > > my_dict = { 'cat': '

Re: Python String Formatting - passing both a dict and string to .format()

2013-11-26 Thread Steven D'Aprano
On Tue, 26 Nov 2013 16:01:48 -0800, Victor Hooi wrote: > '{0['cat']} {1} {0['dog']}'.format(my_dict, foo) ... > SyntaxError: invalid syntax It's a syntax error because you are using the same quotes. You have: '{0['cat']} {1} {0['dog']}' which is parsed as: STR '{0[' NAME cat STR '

Python String Formatting - passing both a dict and string to .format()

2013-11-26 Thread Victor Hooi
Hi, I'm trying to use Python's new style string formatting with a dict and string together. For example, I have the following dict and string variable: my_dict = { 'cat': 'ernie', 'dog': 'spot' } foo = 'lorem ipsum' If I w

Re: String formatting - mysql insert

2011-07-14 Thread Christian
On 14 Jul., 17:31, Billy Mays wrote: > On 07/14/2011 11:00 AM, Christian wrote: > > > > > > > > > > > Hi, > > > I get some problem  when i like to set the table name dynamic. > > I'm appreciate for any help. > > > Christian > > > ### works > > newcur.execute (  """ INSERT INTO events (id1,id2

Re: String formatting - mysql insert

2011-07-14 Thread Billy Mays
On 07/14/2011 11:00 AM, Christian wrote: Hi, I get some problem when i like to set the table name dynamic. I'm appreciate for any help. Christian ### works newcur.execute ( """ INSERT INTO events (id1,id2) VALUES (%s,%s); """ , (rs[1],rs[2])) ### works not newcur.execute ( """ INSE

Re: String formatting - mysql insert

2011-07-14 Thread Chris Angelico
On Fri, Jul 15, 2011 at 1:00 AM, Christian wrote: > Hi, > > I get some problem  when i like to set the table name dynamic. > I'm appreciate for any help. > > ### works but is not really perfect: None from rs list result in > "None" instead of NULL. > newcur.execute (  """ INSERT INTO %s_events (id

String formatting - mysql insert

2011-07-14 Thread Christian
Hi, I get some problem when i like to set the table name dynamic. I'm appreciate for any help. Christian ### works newcur.execute ( """ INSERT INTO events (id1,id2) VALUES (%s,%s); """ , (rs[1],rs[2])) ### works not newcur.execute ( """ INSERT INTO %s_events (id1,id2) VALUES (%s,

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 w

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 no

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

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
the idea of intentionally having multiple ways to do something, all with roughly equal merit. On 2011.07.08 04:38 PM, Ian Kelly wrote: > Also, string formatting (especially using the new syntax like you are) > is much clearer because there's less noise (the quotes all over the >

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

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
racketing syntax, since the function parens are still open. * The ‘%’ string formatting operator is superseded in current Python versions by the more flexible ‘format’ method of string objects. So: logger.error( '{0} could not be stored - {1}'.format( (self.preset

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

Re: String concatenation vs. string formatting

2011-07-08 Thread Benjamin Kaplan
ong. > Sorry if this seems a bit silly, but I'm a novice when it comes to > design. Plus, there's not really supposed to be "more than one way to do > it" in Python. > String formatting is the One Right Way here. It's fine to use string concatenation for a few

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

Re: new string-formatting preferred? (was "What is this syntax ?")

2011-06-21 Thread Tim Chase
On 06/21/2011 05:19 PM, Terry Reedy wrote: On 6/21/2011 7:33 AM, Tim Chase wrote: http://docs.python.org/library/stdtypes.html#str.format> Is there a good link to a thread-archive on when/why/how .format(...) became "preferred to the % formatting"? That is a controversial statement. I'm no

Re: new string-formatting preferred? (was "What is this syntax ?")

2011-06-21 Thread Terry Reedy
On 6/21/2011 7:33 AM, Tim Chase wrote: On 06/20/2011 09:17 PM, Terry Reedy wrote: On 6/20/2011 8:46 PM, Tim Chase wrote: On 06/20/2011 05:19 PM, Ben Finney wrote: “This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in

Re: new string-formatting preferred? (was "What is this syntax ?")

2011-06-21 Thread Tim Chase
On 06/20/2011 09:17 PM, Terry Reedy wrote: On 6/20/2011 8:46 PM, Tim Chase wrote: On 06/20/2011 05:19 PM, Ben Finney wrote: “This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code

Re: new string-formatting preferred? (was "What is this syntax ?")

2011-06-20 Thread Terry Reedy
On 6/20/2011 8:46 PM, Tim Chase wrote: On 06/20/2011 05:19 PM, Ben Finney wrote: “This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code.” http://docs.python.org/library/stdtypes.html

Re: new string-formatting preferred? (was "What is this syntax ?")

2011-06-20 Thread Tim Chase
On 06/20/2011 05:19 PM, Ben Finney wrote: “This method of string formatting is the new standard in Python 3.0, and should be preferred to the % formatting described in String Formatting Operations in new code.” http://docs.python.org/library/stdtypes.html#str.format> Is there a good link t

Re: new string formatting with local variables

2011-06-07 Thread Ben Finney
Steven D'Aprano writes: > On Tue, 07 Jun 2011 10:11:01 +1000, Ben Finney wrote: > > > I tend to use ‘u"foo {bar} baz".format(**vars())’, since ‘vars’ can > > also take the namespace of an object. I only need to remember one > > “give me the namespace” function for formatting. […] > > It's a code

Re: new string formatting with local variables

2011-06-06 Thread Steven D'Aprano
On Tue, 07 Jun 2011 10:11:01 +1000, Ben Finney wrote: > Chris Rebert writes: > >> print "{solo} was captured by {jabba}".format(**locals()) # RIGHT > > I tend to use ‘u"foo {bar} baz".format(**vars())’, since ‘vars’ can also > take the namespace of an object. I only need to remember one “give m

Re: new string formatting with local variables

2011-06-06 Thread Ian Kelly
On Mon, Jun 6, 2011 at 6:11 PM, Ben Finney wrote: > Chris Rebert writes: > >> print "{solo} was captured by {jabba}".format(**locals()) # RIGHT > > I tend to use ‘u"foo {bar} baz".format(**vars())’, since ‘vars’ can also > take the namespace of an object. I only need to remember one “give me > th

Re: new string formatting with local variables

2011-06-06 Thread Ian Kelly
On Mon, Jun 6, 2011 at 6:11 PM, Ben Finney wrote: >> You must use prefix-** in the call to unpack the mapping as keyword >> arguments. Note that using locals() like this isn't best-practice. > > Who says so, and do you find their argument convincing? Do you have a > reference for that so we can se

Re: new string formatting with local variables

2011-06-06 Thread Ben Finney
Chris Rebert writes: > print "{solo} was captured by {jabba}".format(**locals()) # RIGHT I tend to use ‘u"foo {bar} baz".format(**vars())’, since ‘vars’ can also take the namespace of an object. I only need to remember one “give me the namespace” function for formatting. > You must use prefix-*

Re: new string formatting with local variables

2011-06-06 Thread Ethan Furman
Prasad, Ramit wrote: print "{} was captured by {}".format(solo, jabba) Is this Python2.7 specific? Python 2.6.x : print "{} was captured by {}".format('t1', 't2') ValueError: zero length field name in format Apparently it is 2.7 and greater -- my apologies for not specifying that. ~Ethan~

RE: new string formatting with local variables

2011-06-06 Thread Prasad, Ramit
> print "{} was captured by {}".format(solo, jabba) Is this Python2.7 specific? Python 2.6.x : >>>print "{} was captured by {}".format('t1', 't2') ValueError: zero length field name in format Ramit This communication is for informational purposes only. It is not intended as an offer or soli

Re: new string formatting with local variables

2011-06-06 Thread Eric Snow
On Mon, Jun 6, 2011 at 10:15 AM, Jabba Laci wrote: > Hi, > > I'd like to simplify the following string formatting: > > solo = 'Han Solo' > jabba = 'Jabba the Hutt' > print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba) >

Re: new string formatting with local variables

2011-06-06 Thread Ethan Furman
Steve Crook wrote: On Mon, 6 Jun 2011 12:15:35 -0400, Jabba Laci wrote in Message-Id: : solo = 'Han Solo' jabba = 'Jabba the Hutt' print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba) # Han Solo was captured by Jabba the Hutt How about:- print "%s was captured by %s" % (solo

Re: new string formatting with local variables

2011-06-06 Thread Steve Crook
On Mon, 6 Jun 2011 12:15:35 -0400, Jabba Laci wrote in Message-Id: : > solo = 'Han Solo' > jabba = 'Jabba the Hutt' > print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba) > # Han Solo was captured by Jabba the Hutt How about:- print "%s was captured by %s" % (solo, jabba) -- ht

Re: new string formatting with local variables

2011-06-06 Thread Chris Rebert
On Mon, Jun 6, 2011 at 9:15 AM, Jabba Laci wrote: > Hi, > > I'd like to simplify the following string formatting: > > solo = 'Han Solo' > jabba = 'Jabba the Hutt' > print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba) >

new string formatting with local variables

2011-06-06 Thread Jabba Laci
Hi, I'd like to simplify the following string formatting: solo = 'Han Solo' jabba = 'Jabba the Hutt' print "{solo} was captured by {jabba}".format(solo=solo, jabba=jabba) # Han Solo was captured by Jabba the Hutt What I don't like here is this: "so

  1   2   3   4   5   >