Re: Very stupid question about a % symbol

2010-09-17 Thread Xavier Ho
On 17 September 2010 12:48, Terry Reedy  wrote:

> Doubling an escape char, whatever it is, is a common convention:
> >>> print("Print a {{}} format string line this: {{{}}}".format(2))
> Print a {} format string line this: {2}
>

Wow. That's convoluted. Took me a minute to process.

Cheers,
Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question about a % symbol

2010-09-17 Thread Steven D'Aprano
On Thu, 16 Sep 2010 11:25:06 -0400, J wrote:

> OK, this is a very stupid question about a very simple topic, but Google
> is failing me this morning...
[...]

Others have already answered your question, but for future reference, 
many people won't bother to read posts with a meaningless subject line 
like "Very stupid question about ...". 

Very stupid questions invite very stupid answers. I guess you can be 
grateful that this Python group is more friendly than the average tech 
group, otherwise you might have got no answers at all, or a sarcastic 
one. Besides, your actual question isn't stupid at all. It is a sensible, 
although basic, question.

A better subject line would have been:

"How to insert percent sign in % format strings?"

or some variation thereof. This will also be of benefit to others, who 
some day may be Googling for the answer to the same question.


Regards,



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question about a % symbol

2010-09-16 Thread Terry Reedy

On 9/16/2010 12:23 PM, J wrote:


Thanks for the replies... I KNEW there was a simple way to escape the
% but I had no idea what it was (I just had conviction).

I was thrown when the \ didn't escape it... never knew about %%.  But
now I do!  Thanks for the replies!


Doubling an escape char, whatever it is, is a common convention:
>>> print("Print a {{}} format string line this: {{{}}}".format(2))
Print a {} format string line this: {2}

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question about a % symbol

2010-09-16 Thread Jason Swails
Ha, I had this same problem, but I was trying to do dynamic formatting:

("%%%s" % format) % number

where "format" is a python-ized fortran format string (i.e. "9.4E").  Looks
kinda weird and less elegant than the {0:{1}}-type .format() syntax, but at
least it preserves backwards compatibility to pythons older than 2.6.
Before I found out how to cancel the %, the statement looked like ("%"+"%s"
% format) % number.  Not much of a change, but still a nice thing to know
since I play with %s a lot.

Thanks! (even though I'm not the original asker)

Jason

On Thu, Sep 16, 2010 at 12:09 PM, Grant Edwards wrote:

> On 2010-09-16, J  wrote:
>
> > Reported memory amounts are within 10% tolerance
>
> >>> "Reported memory amounts are within %d%% tolerance" % 10
> 'Reported memory amounts are within 10% tolerance'
>
> --
> Grant Edwards   grant.b.edwardsYow! It's the RINSE
> CYCLE!!
>  at   They've ALL IGNORED the
>  gmail.comRINSE CYCLE!!
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Jason M. Swails
Quantum Theory Project,
University of Florida
Ph.D. Graduate Student
352-392-4032
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question about a % symbol

2010-09-16 Thread J
On Thu, Sep 16, 2010 at 12:09, Grant Edwards  wrote:
> On 2010-09-16, J  wrote:
>
>> Reported memory amounts are within 10% tolerance
>
 "Reported memory amounts are within %d%% tolerance" % 10
> 'Reported memory amounts are within 10% tolerance'

Thanks for the replies... I KNEW there was a simple way to escape the
% but I had no idea what it was (I just had conviction).

I was thrown when the \ didn't escape it... never knew about %%.  But
now I do!  Thanks for the replies!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question about a % symbol

2010-09-16 Thread Tim Chase

On 09/16/10 10:25, J wrote:

OK, this is a very stupid question about a very simple topic, but
print "Reported memory amounts are within %s%s tolerance" %
(self.mem_tolerance,'%')

Is there a better way to print a '%' in the string when also using formating?

I've tried things like this:

print "blahblahblah %s \%" % variable


So close

  print "blah %s %%" % variable

-tkc


--
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question about a % symbol

2010-09-16 Thread Grant Edwards
On 2010-09-16, J  wrote:

> Reported memory amounts are within 10% tolerance

>>> "Reported memory amounts are within %d%% tolerance" % 10
'Reported memory amounts are within 10% tolerance'

-- 
Grant Edwards   grant.b.edwardsYow! It's the RINSE CYCLE!!
  at   They've ALL IGNORED the
  gmail.comRINSE CYCLE!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question about a % symbol

2010-09-16 Thread Xavier Ho
On 17 September 2010 01:25, J  wrote:

> Is there a better way to print a '%' in the string when also using
> formating?
>

I believe %% will escape the % and prints it straight out.

Cheers,
Xav
-- 
http://mail.python.org/mailman/listinfo/python-list


Very stupid question about a % symbol

2010-09-16 Thread J
OK, this is a very stupid question about a very simple topic, but
Google is failing me this morning...

I'm trying to print a string that looks like this:

Reported memory amounts are within 10% tolerance

and the print line looks (for now) like this:

print "Reported memory amounts are within %s%s tolerance" %
(self.mem_tolerance,'%')

Is there a better way to print a '%' in the string when also using formating?

That works, but I've been looking for a way that looks... less kludgey.

I've tried things like this:

print "blahblahblah %s \%" % variable

but the first %s forces every other % to be interpreted as additional
format markers.

So while what I'm doing works, I was hoping someone could either
confirm that that's the right way, or show me a better way.

Cheers

Jeff
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question.

2006-03-31 Thread Juha-Matti Tapio
Peter Hansen <[EMAIL PROTECTED]> wrote:
>  >>> from path import path
>  >>> path('foobar').getsize()
> 12345L
> (But note that it's just a nice wrapper around the scattered builtin 
> ways of doing the same thing, in this case the os.stat().st_size 
> approach mentioned above.  That's not a bad thing, though, IMHO.)

Also if the file in question is already open, it can be done like this:

os.fstat(fileobject.fileno()).st_size

This form avoids some race condition scenarious with the file being changed 
between stat and open.

I think file sizes should be used carefully and only for visual reasons. 
For code logic it is almost always better to go the "it's easier to ask 
forgiveness than ask permission" -route. Therefore looking up the file size 
is done only rarely and it is not worthy to be a file-object method.

-- 
Juha-Matti Tapio - fil.yo. - +358-50-5419230
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question.

2006-03-30 Thread Peter Hansen
On 3/30/06, *Sullivan Zheng* <[EMAIL PROTECTED]
> wrote:
> 
> Wow, seems I am not that supid. Why python does not include this
> function in the file object. It is almost a tradition in other
> languages...
>  
> import os
>  
> os.stat(path).st_size
>  
> really not elegant or OO.

You might find something like Jason Orendorff's path.py module (Google 
for it) to be more elegant.  With it, this works fine:

 >>> from path import path
 >>> path('foobar').getsize()
12345L

(But note that it's just a nice wrapper around the scattered builtin 
ways of doing the same thing, in this case the os.stat().st_size 
approach mentioned above.  That's not a bad thing, though, IMHO.)

-Peter

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question.

2006-03-30 Thread Benji York

On 3/30/06, *Sullivan Zheng* <[EMAIL PROTECTED]> wrote:
 > Wow, seems I am not that supid. Why python does not include this
 > function in the file object. It is almost a tradition in other
 > languages...

 > really not elegant or OO.

A file isn't an object.

You can get a "file object" by opening a file (on disk), but it
doesn't make much sense to have to open a file just to see how big it
is.
--
Benji York
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question.

2006-03-30 Thread Sebastjan Trepca
On 3/30/06, Sullivan Zheng <[EMAIL PROTECTED]> wrote:
Wow, seems I am not that supid. Why python does not include this function in the file object. It is almost a tradition in other languages...
 
import os
 
os.stat(path).st_size
 
really not elegant or OO.True.-- Sebastjanhttp://www.trepca.si/blog
-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Very stupid question.

2006-03-30 Thread Jorge Godoy
"Sullivan WxPyQtKinter" <[EMAIL PROTECTED]> writes:

> How to get the length of a file via build-in file object support? In
> Visual Basic there is len(file) of something like that. But in python,
> where is this property?
>
> Sorry for this stupid question, if it is.

pydoc osand then look for "stat"...  In "stat_result" there's a
description of the tuple you'll get.

-- 
Jorge Godoy  <[EMAIL PROTECTED]>

"Quidquid latine dictum sit, altum sonatur."
- Qualquer coisa dita em latim soa profundo.
- Anything said in Latin sounds smart.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question.

2006-03-30 Thread Sebastjan Trepca
Check os.stat() function here http://docs.python.org/lib/os-file-dir.html-- Sebastjanhttp://www.trepca.si/blog
On 30 Mar 2006 09:34:32 -0800, Sullivan WxPyQtKinter <[EMAIL PROTECTED]> wrote:
In addition, f=file('filename','r');len(f.read()) is quite expensive inmy point of view, If the file is serveral MB or larger.--http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Very stupid question.

2006-03-30 Thread Fredrik Lundh
"Sullivan WxPyQtKinter" wrote:

> How to get the length of a file via build-in file object support? In
> Visual Basic there is len(file) of something like that. But in python,
> where is this property?

import os

size = os.path.getsize(filename)





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Very stupid question.

2006-03-30 Thread Sullivan WxPyQtKinter
In addition, f=file('filename','r');len(f.read()) is quite expensive in
my point of view, If the file is serveral MB or larger.

-- 
http://mail.python.org/mailman/listinfo/python-list


Very stupid question.

2006-03-30 Thread Sullivan WxPyQtKinter
How to get the length of a file via build-in file object support? In
Visual Basic there is len(file) of something like that. But in python,
where is this property?

Sorry for this stupid question, if it is.

Thank you for help.

-- 
http://mail.python.org/mailman/listinfo/python-list