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.substi tute(dict(dir=w orking_dir,
cmd=ssh_cmd,
count=some_coun t,
pararm1=some_pa ram1,
param2=some_par am2))
here you can use a couple of nice tricks by using class.__dict__ and
globals() \ locals() dictionaries.
3. some_string = "cd "+working_d ir+" ; "+ssh_cmd+ "
"+str(some_coun t)+" "+some_para m1+" "+some_para m2
(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. 3 1807
Gal Diskin wrote:
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.substi tute(dict(dir=w orking_dir,
cmd=ssh_cmd,
count=some_coun t,
pararm1=some_pa ram1,
param2=some_par am2))
here you can use a couple of nice tricks by using class.__dict__ and
globals() \ locals() dictionaries.
3. some_string = "cd "+working_d ir+" ; "+ssh_cmd+ "
"+str(some_coun t)+" "+some_para m1+" "+some_para m2
(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()
Gal Diskin wrote:
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 sc***********@a cm.org
Matthew Warren wrote:
-----Original Message-----
From:
py************* *************** *************** @python.org
[mailto:py****** *************** *************** *******@python. o
rg] On Behalf Of Gal Diskin
Sent: 05 October 2006 16:01
To: py*********@pyt hon.org
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.substi tute(dict(dir=w orking_dir,
cmd=ssh_cmd,
count=some_coun t,
pararm1=some_pa ram1,
param2=some_par am2))
here you can use a couple of nice tricks by using class.__dict__ and
globals() \ locals() dictionaries.
3. some_string = "cd "+working_d ir+" ; "+ssh_cmd+ "
"+str(some_coun t)+" "+some_para m1+" "+some_para m2
(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 G_********@Inte l.com G_********@gmai l.com
Work: +972-4-865-1637
Cell: +972-54-7594166 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: meckhert |
last post by:
I am currently building a sports web application and have run into a
design question.
In order to create a new coach record, there are two pieces of
information that are needed: 1) username 2) team_id
It seems to me that the best way to do this is to break the problem
into two parts. First, search for the username and append that to the...
|
by: Adrian Parker |
last post by:
I'm using the code below in my project. When I print all of these fixed
length string variables, one per line, they strings in questions do not
properly pad with 0s. strQuantity prints as " 4". Six spaces than the
value of intQuantity. This is correct.
But all the others end up being string objects of only 6 characters long
(with...
|
by: Olaf Meyer |
last post by:
Sometimes if find it clumsy unsing the following approach building strings:
cmd = "%s -start %s -end %s -dir %s" % (executable, startTime, endTime,
directory)
Especially if you have a lot of variable input it makes it hard to match
the variables to the proper fields. From other scripting languanges I'm
used to something like:
$cmd =...
|
by: ILCSP |
last post by:
Hi, I have a sql table containing the answers for some tests. The
information in this table is presented vertically and I need to create
strings with them. I know how to read the data in VB.Net and use a
StreamWriter to build the strings. However, the problem lies with the
reading of each row.
Most of the test takers don't give answers...
|
by: Hans |
last post by:
Hi,
I want to define a couple of constant strings, like in C:
#define mystring "This is my string"
or using a const char construction.
Is this really not possible in Python?
Hans
| |
by: flameboy |
last post by:
I am currently trying to figure my way how to output strings to a file.
I have seen all the many posts and such how to do it in a console program, and have had much success doing it that way. This is a gui app though. What I want to be able to do is take strings from a text box, output those strings to char arrays, and then output the char arrays...
|
by: john |
last post by:
All:
I'm a long-time developer, new to PHP....
Is there an idiom used in PHP to construct SQL statments from $_POST
data?
I would guess that in many applications, the data read from $_POST are
used to build SQL statements. Certainly, we can do the following:
|
by: patriciashoe |
last post by:
Good AM:
I have struggling with constructing a string to execute some SQL. Here is my update statement
update teacherresources set = (( / variable1) * Variable 2 )) where(( trschool_id = 12 and = forms!form1!yearcombo) or (where trschool_id = 13 and [tryear = forms!form!yearcombo))
the variables are both numeric. I know the...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |