building strings from variables

G

Gal Diskin

Following a discussion with an associate at work about various ways to
build strings from variables in python, I'd like to hear your opinions
and preferred methods. The methods we discussed are:
1. some_string = "cd %s ; %s %d %s %s" % ( working_dir, ssh_cmd,
some_count, some_param1, some_param2)

2. import string
template = string.Template("cd $dir ; $cmd $count $param1
$param2")
some_string = template.substitute(dict(dir=working_dir,

cmd=ssh_cmd,

count=some_count,

pararm1=some_param1,

param2=some_param2))
here you can use a couple of nice tricks by using class.__dict__ and
globals() \ locals() dictionaries.

3. some_string = "cd "+working_dir+" ; "+ssh_cmd+ "
"+str(some_count)+" "+some_param1+" "+some_param2
(all these are supposed to produce the same strings)

Which methods do you know of \ prefer \ think is better because...?
I will appreciate any opinions about the matter.
 
R

Rainy

Gal said:
Following a discussion with an associate at work about various ways to
build strings from variables in python, I'd like to hear your opinions
and preferred methods. The methods we discussed are:
1. some_string = "cd %s ; %s %d %s %s" % ( working_dir, ssh_cmd,
some_count, some_param1, some_param2)

2. import string
template = string.Template("cd $dir ; $cmd $count $param1
$param2")
some_string = template.substitute(dict(dir=working_dir,

cmd=ssh_cmd,

count=some_count,

pararm1=some_param1,

param2=some_param2))
here you can use a couple of nice tricks by using class.__dict__ and
globals() \ locals() dictionaries.

3. some_string = "cd "+working_dir+" ; "+ssh_cmd+ "
"+str(some_count)+" "+some_param1+" "+some_param2
(all these are supposed to produce the same strings)

Which methods do you know of \ prefer \ think is better because...?
I will appreciate any opinions about the matter.

My opinion is that 1st method is the best. It's both very readable and
easy to write. 2nd way is too involved, and 3rd way is both hard to
read and difficult to compose. Another useful variant of method #1 is
to do this: %(varname)s ..." % globals()
 
S

Scott David Daniels

Gal said:
Following a discussion with an associate at work about various ways to
build strings from variables in python, I'd like to hear your opinions
and preferred methods. The methods we discussed are:
1. some_string = "cd %s ; %s %d %s %s" % ( working_dir, ssh_cmd,
some_count, some_param1, some_param2)
....
Or another for readability:

4. some_string = ' '.join(["cd", working_dir, ";",
ssh_cmd, str(some_count), some_param1, some_param2])

--Scott David Daniels
(e-mail address removed)
 
G

Gal Diskin

Matthew said:
-----Original Message-----
From:
[email protected]
[mailto:p[email protected]
rg] On Behalf Of Gal Diskin
Sent: 05 October 2006 16:01
To: (e-mail address removed)
Subject: building strings from variables

Following a discussion with an associate at work about various ways to
build strings from variables in python, I'd like to hear your opinions
and preferred methods. The methods we discussed are:
1. some_string = "cd %s ; %s %d %s %s" % ( working_dir, ssh_cmd,
some_count, some_param1, some_param2)

2. import string
template = string.Template("cd $dir ; $cmd $count $param1
$param2")
some_string = template.substitute(dict(dir=working_dir,

cmd=ssh_cmd,

count=some_count,

pararm1=some_param1,

param2=some_param2))
here you can use a couple of nice tricks by using class.__dict__ and
globals() \ locals() dictionaries.

3. some_string = "cd "+working_dir+" ; "+ssh_cmd+ "
"+str(some_count)+" "+some_param1+" "+some_param2
(all these are supposed to produce the same strings)

Which methods do you know of \ prefer \ think is better because...?
I will appreciate any opinions about the matter.

:D

I think, it would depend really on what your aims are (readable code,
fast string generation...), and how the vars you want to build the
string from are respresented in your code (is it natural to use a dict
etc..)

I kicked off a conversation similar to this earlier today, and that was
my conclusion after helpful debate & advice.

Matt.


This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer.

You should not copy the email, use it for any purpose or disclose its contents to any other person.
Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com

Matt,
Thanks for replying. I tried looking up your discussion and I'm unsure
if I found the right post. Would you mind linking to it?

--
Gal Diskin
(e-mail address removed)
(e-mail address removed)
Work: +972-4-865-1637
Cell: +972-54-7594166
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top