Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
str.format utility function
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dag Moxnes  
View profile  
 More options May 8 2009, 6:05 pm
From: Dag Moxnes <dagmox...@hotmail.com>
Date: Sat, 9 May 2009 00:05:15 +0200
Local: Fri, May 8 2009 6:05 pm
Subject: [Python-ideas] str.format utility function

Hi list,

I'm sorry if this has been discussed before, but I did not find any references. I've been playing abit with the new str.format function. I really like the syntax and simplicity.

However, when simply printing named variables in locals() or a class, I quite common use-case, I find it a bit too complicated with the **:

"Local variable var1 is %(var1)s" % locals()

vs

"Local variable var1 is {var1}".format(**locals())

and

"Instance variable var2 is %(var2)s" % self.__dict__

vs

"Instance variable var2 is {var2}" % **self.__dict__

Therefore I have made myself a utility funcion:

def easyformat(s, data):
    try:
        return s.format(**data)
    except TypeError:
        return s.format(**data.__dict__)

so that I can do the following:

"Local variable var1 is {var1}".format(**locals())

and

"Instance variable var2 is %(var2)s" % self

Should a function similar to this (maybe with a better name) be included included in some standard library?

-Dag

_________________________________________________________________
Få mer ut av Windows Live™ med Internet Explorer® 8.
http://microsoft.no/ie

_______________________________________________
Python-ideas mailing list
Python-id...@python.org
http://mail.python.org/mailman/listinfo/python-ideas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gerald Britton  
View profile  
 More options May 8 2009, 7:27 pm
From: Gerald Britton <gerald.brit...@gmail.com>
Date: Fri, 8 May 2009 19:27:15 -0400
Local: Fri, May 8 2009 7:27 pm
Subject: Re: [Python-ideas] str.format utility function
fwiw:

    one = 'slower'
    two = 'yours'
    "my str is %(one)s than %(two)s" % locals()

is slower than:

    "my str is %(one)s than %(two)s" % {"one":faster, "two":yours}

probably because of the overhead of the function call to locals().
Basically I find using locals() this way is just lazy programming,
since you hope your variables are in there somewhere.  If someone
changes them later, your string expression may fail.  I'd rather be
explicit about what I'm doing.

--
Gerald Britton
_______________________________________________
Python-ideas mailing list
Python-id...@python.org
http://mail.python.org/mailman/listinfo/python-ideas

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Terry Reedy  
View profile  
 More options May 8 2009, 7:52 pm
From: Terry Reedy <tjre...@udel.edu>
Date: Fri, 08 May 2009 19:52:59 -0400
Local: Fri, May 8 2009 7:52 pm
Subject: Re: [Python-ideas] str.format utility function

Dag Moxnes wrote:
> Hi list,

> I'm sorry if this has been discussed before, but I did not find any
> references. I've been playing abit with the new str.format function. I
> really like the syntax and simplicity.

> However, when simply printing named variables in locals() or a class, I
> quite common use-case, I find it a bit too complicated with the **:

> "Local variable var1 is %(var1)s" % locals()

> vs

> "Local variable var1 is {var1}".format(**locals())

I see nothing compicated about **. In any case, it is a common idiom
that Python progrmmers should learn and hopefully become comfortable
with.  I see nothing gained with
s.easyformat(s, locals()) # over
s.format(**locals)
except a few extra chars to type ;-).

If you were doing that often, you could write

def lform(s): # format caller locals
   l = <locals of caller> # ask on Python list or check python recipies
   return s.format(**l)

> "Instance variable var2 is %(var2)s" % self.__dict__

> vs

> "Instance variable var2 is {var2}" % **self.__dict__\

You meant,
"Instance variable var2 is {var2}".format(**self.__dict__)

Wrapping this seems like a possible method.  For a constant format
applicable to all instances, override .__str__().

 > def easyformat(s, data):
 >     try:
 >         return s.format(**data)
 >     except TypeError:
 >         return s.format(**data.__dict__)

> Should a function similar to this (maybe with a better name) be
> included in some standard library?

I think not.

Terry Jan Reedy

_______________________________________________
Python-ideas mailing list
Python-id...@python.org
http://mail.python.org/mailman/listinfo/python-ideas


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »