Re: % symbol in python

2014-10-29 Thread Mark Lawrence

On 29/10/2014 05:48, satishmlm...@gmail.com wrote:

kindly let me know
what does
%%(%s)% mean



What did you not understand from the link I posted ten hours ago?

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list


% symbol in python

2014-10-28 Thread satishmlmlml
trthkeytdinput type=text name=key value=%(key)s
rowhtml = 'trth%stdinput type=text name=%s value%%(%s)s\n

what does % mean in first line of code and
what does %%(%s)s mean in second line of code

kindly explain
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: % symbol in python

2014-10-28 Thread Mark Lawrence

On 28/10/2014 21:41, satishmlm...@gmail.com wrote:

trthkeytdinput type=text name=key value=%(key)s
rowhtml = 'trth%stdinput type=text name=%s value%%(%s)s\n

what does % mean in first line of code and
what does %%(%s)s mean in second line of code

kindly explain



Please refer to 
https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting


--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

--
https://mail.python.org/mailman/listinfo/python-list


Re:% symbol in python

2014-10-28 Thread Dave Angel
satishmlm...@gmail.com Wrote in message:
 trthkeytdinput type=text name=key value=%(key)s
 rowhtml = 'trth%stdinput type=text name=%s value%%(%s)s\n
 
 what does % mean in first line of code and
 what does %%(%s)s mean in second line of code
 
 kindly explain
 

Please post Python code, and we can try to comment on it. Those
 extra tr and th thingies are really killing my mental syntax
 checker.

Also, always specify the python version. 
-- 
DaveA

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: % symbol in python

2014-10-28 Thread satishmlmlml
import cgi, shelve, sys, os
shelvename = 'class-shelve'
fieldnames = ('name', 'age', 'job', 'pay')
form = cgi.FieldStorage()
print('Content-type: text/html')
sys.path.insert(0, os.getcwd())
replyhtml = 
html
titlePeople Input Form/title
body
form method=POST action=peoplecgi.py
table
trthkeytdinput type=text name=key value=%(key)s
$ROWS$
/table
p
input type=submit value=Fetch, name=action
input type=submit value=Update, name=action
/form
/body/html

rowhtml = 'trth%stdinput type=text name=%s value=%%(%s)s\n'
rowshtml = ''
for fieldname in fieldnames:
rowshtml += (rowhtml % ((fieldname, ) * 3))
replyhtml = replyhtml.replace('$ROWS$', rowshtml)
def htmlize(adict):
new = adict.copy()
for field in fieldnames:
value = new[field]
new[field] = cgi.escape(repr(value))
return new

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))
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: % symbol in python

2014-10-28 Thread Chris Angelico
On Wed, Oct 29, 2014 at 2:51 PM,  satishmlm...@gmail.com wrote:
 def fetchRecord(db, form):
 try:
 key = form['key'].value

When you paste Python code into an email, it's absolutely crucial that
you maintain formatting. The indentation is significant. Can you paste
it again, please?

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: % symbol in python

2014-10-28 Thread satishmlmlml
import cgi, shelve, sys, os 
shelvename = 'class-shelve' 
fieldnames = ('name', 'age', 'job', 'pay') 
form = cgi.FieldStorage() 
print('Content-type: text/html') 
sys.path.insert(0, os.getcwd()) 
replyhtml =  
html 
titlePeople Input Form/title 
body 
form method=POST action=peoplecgi.py 
 table 
 trthkeytdinput type=text name=key value=%(key)s 
 $ROWS$ 
 /table 
 p 
 input type=submit value=Fetch, name=action 
 input type=submit value=Update, name=action 
/form 
/body/html 
 
rowhtml = 'trth%stdinput type=text name=%s value=%%(%s)s\n' 
rowshtml = '' 
for fieldname in fieldnames: 
 rowshtml += (rowhtml % ((fieldname, ) * 3)) 
 replyhtml = replyhtml.replace('$ROWS$', rowshtml) 
def htmlize(adict): 
 new = adict.copy() 
 for field in fieldnames: 
  value = new[field] 
  new[field] = cgi.escape(repr(value)) 
 return new 

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)) 


kindly let me know what is $ROWS$ along with % symbol's meaning
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: % symbol in python

2014-10-28 Thread alex23

On 29/10/2014 2:41 PM, satishmlm...@gmail.com wrote:
 kindly let me know what is $ROWS$ along with % symbol's meaning

It's a token, a static value added to the template to indicate where 
additional data will be added.


So $ROW$ in this section:


  table
  trthkeytdinput type=text name=key value=%(key)s
  $ROWS$

   /table

Will be replaced by whatever rowshtml contains at this point:


  replyhtml = replyhtml.replace('$ROWS$', rowshtml)


Of note: the template section above has opening tags for tr, th and 
td but no closing ones. It's not valid html.


Also I don't think this is doing what you think it is:

 rowhtml = 'trth%stdinput type=text name=%s value=%%(%s)s\n'
 rowshtml = ''
 for fieldname in fieldnames:
  rowshtml += (rowhtml % ((fieldname, ) * 3))
  replyhtml = replyhtml.replace('$ROWS$', rowshtml)

After the first fieldname ('name' in this case), the token $ROWS$ will 
be replaced by the string :


'trthnametdinput type=text name=name value=%%(name)s\n'.

On the second iteration of the loop, when fieldname is 'age', rowshtml 
will equal:


 'trthnametdinput type=text name=name 
value=%%(name)s\ntrthagetdinput type=text name=age 
value=%%(age)s\n'


...but nothing will happen with it as $ROWS$ has already been replaced 
during the first time through the loop. Either the `replyhtml = ...` 
line is mis-indented or the code is terribly broken.


Unless this is for homework, I highly recommend just using one of the 
many templating libraries that exist for Python, such as Jinja2.

--
https://mail.python.org/mailman/listinfo/python-list


Re: % symbol in python

2014-10-28 Thread satishmlmlml
kindly let me know 
what does
%%(%s)% mean 
-- 
https://mail.python.org/mailman/listinfo/python-list