Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77167

Re: python string, best way to concat

From Peter Otten <__peter__@web.de>
Subject Re: python string, best way to concat
Date 2014-08-28 00:34 +0200
Organization None
References <55bab2a0-e0bc-4398-90b4-c9937498f5d8@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.13528.1409178864.18130.python-list@python.org> (permalink)

Show all headers | View raw


dennisearlevans@gmail.com wrote:

> 
>   Hi,
> 
>   Sorry about the simple question but I am very new to Python.
> 
>   Anyway, I have a function that will be used to call a stored procedure
>   and I need to format the string with the correct number of parameter
>   markers for the ODBC driver, fairly standard stuff.
> 
>   What I have works but looks ugly, is there a better way to build or
>   concatenate a string or is a list or a tuple a better option?
> 
> the function looks like this
> 
>   def callSp(self, schema, spName) :
>    sqlCode = "{call " + schema + "." + spName + "("
>    par_Markers = ""
>    y = len(self.param)
>    x = 0
>    while x < y :
>      par_Markers = par_Markers.join("?")
>      if (x < y - 1) :
>        par_Markers = par_Markers.join(", ")
>      x += 1
>    self.cmdText = sqlCode + par_Markers + ")}"
>    self.ExecuteCursor()
> 
>    return
> 
> self.param is a list of parameters.  sself.cmdText is the text that will
> be used to call the stored procedure.
> 
> the function would be called like this
> 
>   class.AddParameter(some value 1)
>   class.AddParameter(some value 2)
>   callSp("schemaName", "storedProcedureName")
>   
>   the self.cmdText will look like this just before the ExecuteCursor call
> 
>   "{call schemaName.StoredProcedureName(?, ?)}"
> 
>   the code works, but as I said is a bit ugly, is there better methods?

If I understand you correctly you have two problems.

(1) build a string from a few parts. You can use string formatting for that:

>>> "It's a {size} {animal}".format(size="big", animal="bird")
"It's a big bird"

You can escape { and } by repeating them:

>>> "{{It's a {size} {animal}}}".format(size="big", animal="bird")
"{It's a big bird}"

(2) Build a string containing a comma-separated list with a varying number 
of question marks. That's easy with str.join():

>>> ", ".join(7*"?")
'?, ?, ?, ?, ?, ?, ?'

The complete method may become

    def callSp(self, schema, spName) :
        qm = ", ".join("?"*len(self.param))
        self.cmdText = "{{call {schema}.{procname} ({qm})}}".format(
            schema=schema, procname=spName, qm=qm)
        self.ExecuteCursor()

... but please consider adopting the conventions recommended in the style 
guide

http://legacy.python.org/dev/peps/pep-0008/

before you write a significant amount of Python code.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

python string, best way to concat dennisearlevans@gmail.com - 2014-08-27 13:31 -0700
  Re: python string, best way to concat Dan Stromberg <drsalists@gmail.com> - 2014-08-27 15:30 -0700
  Re: python string, best way to concat Peter Otten <__peter__@web.de> - 2014-08-28 00:34 +0200
  Re: python string, best way to concat MRAB <python@mrabarnett.plus.com> - 2014-08-27 23:42 +0100
  Re: python string, best way to concat Tim Chase <python.list@tim.thechases.com> - 2014-08-27 17:44 -0500
  Re: python string, best way to concat Chris Angelico <rosuav@gmail.com> - 2014-08-28 08:55 +1000
  Re: python string, best way to concat Peter Otten <__peter__@web.de> - 2014-08-28 00:59 +0200
  Re: python string, best way to concat MRAB <python@mrabarnett.plus.com> - 2014-08-28 08:12 +0100
  Re: python string, best way to concat peter <peter.mosley@talk21.com> - 2014-08-28 01:30 -0700
    Re: python string, best way to concat Marko Rauhamaa <marko@pacujo.net> - 2014-08-28 11:34 +0300
    Re: python string, best way to concat Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-28 09:43 +0100
    Re: python string, best way to concat Chris Angelico <rosuav@gmail.com> - 2014-08-28 18:58 +1000
    Re: python string, best way to concat Roy Smith <roy@panix.com> - 2014-08-28 08:08 -0400
      Re: python string, best way to concat Mihamina Rakotomandimby <mihamina.rakotomandimby@rktmb.org> - 2014-08-28 15:19 +0300
      Re: python string, best way to concat Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-08-28 15:48 +0100

csiph-web