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


Groups > comp.lang.python > #79895 > unrolled thread

What does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain.

Started bysatishmlmlml@gmail.com
First post2014-10-28 23:15 -0700
Last post2014-10-29 09:13 +0100
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  What does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain. satishmlmlml@gmail.com - 2014-10-28 23:15 -0700
    Re: What does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain. Peter Otten <__peter__@web.de> - 2014-10-29 08:42 +0100
    Re: What does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain. Christian Gollwitzer <auriocus@gmx.de> - 2014-10-29 09:13 +0100

#79895 — What does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain.

Fromsatishmlmlml@gmail.com
Date2014-10-28 23:15 -0700
SubjectWhat does %%(%s)s mean/expand to in Python? What does rowshtml += (rowhtml % ((fieldname, ) * 3)) expand to? Kindly explain.
Message-ID<ee27d38d-8732-485c-a64b-0da2eee1d93b@googlegroups.com>
def fetchRecord(db, form): 
 try: 
  key = form['key'].value 
  record = db[key] 
  fields = record.__dict__ 
  fields['key'] = key 
 except: 
  fields = dict.fromkeys(fieldnames, '?') 
  fields['key'] = 'Missing or invalid key!' 
 return fields 
def updateRecord(db, form): 
 if not 'key' in form: 
  fields = dict.fromkeys(fieldnames, '?') 
  fields['key'] = 'Missing key input!' 
 else: 
  key = form['key'].value 
  if key in db: 
   record = db[key] 
 else: 
   from person import Person 
   record = Person(name='?', age='?') 
 for field in fieldnames: 
   setattr(record, field, eval(form[field].value)) 
  db[key] = record 
  fields = record.__dict__ 
  fields['key'] = key 
 return fields 
db  =  shelve.open(shelvename) 
action = form['action'].value if 'action' in form else None 
if action == 'Fetch': 
 fields = fetchRecord(db, form) 
elif action == 'Update': 
 fields = updateRecord(db, form) 
else: 
 fields = dict.fromkeys(fieldnames, '?') 
 fields['key'] = 'Missing or invalid action!' 
db.close() 
print(replyhtml % htmlize(fields)) 

What does %%(%s)s mean in Python? 

also 
what does 
rowshtml += (rowhtml % ((fieldname,) * 3)) expand to? 

[toc] | [next] | [standalone]


#79897

FromPeter Otten <__peter__@web.de>
Date2014-10-29 08:42 +0100
Message-ID<mailman.15289.1414568549.18130.python-list@python.org>
In reply to#79895
satishmlmlml@gmail.com wrote:


> What does %%(%s)s mean in Python?

According to 

https://docs.python.org/2/library/stdtypes.html#string-formatting

in an expression like

"%%(%s)s" % ("foo",)

'%%' expands to '%' and

'%s' expands to 'foo'

so the whole thing gives

>>> "%%(%s)s" % ("foo",)
'%(foo)s'

To this you can then apply another formatting operation that takes a dict as 
its right operand to look up 'foo':

>>> "%(foo)s" % {"foo": "bar"}
'bar'

What you have with "%%(%s)s" is then a template for a template.

> also
> what does
> rowshtml += (rowhtml % ((fieldname,) * 3)) expand to?

(fieldname,)

is a 1-tuple, so

(fieldname,) *3  gives a 3-tuple (fieldname, fieldname, fieldname):

>>> fieldname = "foo"
>>> (fieldname,) * 3
('foo', 'foo', 'foo')

rowhhtml % (fieldname, fieldname, fieldname)

is again string interpolation. Assuming rowhtml contains

"<%s>%s<%s>" may you get

>>> fieldname = "foo"
>>> rowhtml = "<%s>%s</%s>"
>>> rowhtml % ((fieldname,)*3)
'<foo>foo</foo>'

[toc] | [prev] | [next] | [standalone]


#79898

FromChristian Gollwitzer <auriocus@gmx.de>
Date2014-10-29 09:13 +0100
Message-ID<m2q7j1$a29$1@dont-email.me>
In reply to#79895
Am 29.10.14 07:15, schrieb satishmlmlml@gmail.com:
> What does %%(%s)s mean in Python?

Instead of posting all those questions here, you can simply try it in an 
interactive python interpreter:

Apfelkiste:VecTcl chris$ python
Python 2.7.2 (default, Oct 11 2012, 20:14:37)
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
 >>> test='%%(%s)s'
 >>> test % 'foo'
'%(foo)s'
 >>>

> also
> what does
> rowshtml += (rowhtml % ((fieldname,) * 3)) expand to?
>
 >>> fieldname='foo'
 >>> (fieldname,)*3
('foo', 'foo', 'foo')
 >>> test='first %s second %s third %s'
 >>> test % ((fieldname,)*3)
'first foo second foo third foo'
 >>>

	Christian

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web