Re: Receing a form variable as a list instead of as a string

2013-06-11 Thread Steven D'Aprano
On Tue, 11 Jun 2013 18:29:05 -0700, nagia.retsina wrote:

> if page or form.getvalue('show') == 'log':
> # it is a python script
> page = page.replace( '/home/nikos/public_html/cgi-bin', '' )
> elif page or form.getvalue('show') == 'stats':
> page = page.replace( '/home/nikos/public_html/cgi-bin', '' )
> 
> 
> in the first if option page works a string and replace happens , while
> in the second it behaves liek a list and the error i mentioned is being
> produced.


It doesn't *behave* like a list. It **IS** a list. I know that people 
have already pointed you to the documentation, where getvalue is 
documented to return either a string or a list, depending on how many 
values there are in the form.

So, yet again, here is the documentation:

http://docs.python.org/3/library/cgi.html


READ IT.

Here is an extract:

[quote]
In the previous section, you learned to write following code anytime you 
expected a user to post more than one value under one name:

item = form.getvalue("item")
if isinstance(item, list):
# The user is requesting more than one item.
else:
# The user is requesting only one item.
[end quote]


Read the rest of the docs. Don't ask any more questions until you have 
read them. Then, if anything is still unclear, you can point to a section 
of the docs and say "I don't understand this".



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Receing a form variable as a list instead of as a string

2013-06-11 Thread nagia . retsina
if page or form.getvalue('show') == 'log': 
# it is a python script 
page = page.replace( '/home/nikos/public_html/cgi-bin', '' ) 
elif page or form.getvalue('show') == 'stats': 
page = page.replace( '/home/nikos/public_html/cgi-bin', '' ) 


in the first if option page works a string and replace happens , while in the 
second it behaves liek a list and the error i mentioned is being produced.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Receing a form variable as a list instead of as a string

2013-06-11 Thread alex23
On Jun 12, 6:16 am, Νικόλαος Κούρας  wrote:
> I get the error that i cannot use 'replac'e to a 'list'.
> How can i avoid that?

BY NOT USING STRING METHODS ON A LIST.

Ulrich even *told* you what to do: "convert it to a string yourself"

Do you do that in your code? No. Instead, you assign a value _to the
same label_ and expect it to do magic.

For someone who is adamant he's not trolling, you're sure doing your
damnedest to live up to your self-applied troll name.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Receing a form variable as a list instead of as a string

2013-06-11 Thread alex23
On Jun 12, 5:42 am, Νικόλαος Κούρας  wrote:
> Indeed as Andreas said i was overusing the form variable 'page'

No. You're simply _not_ thinking about what you're doing.

> elif page or form.getvalue('show'):
>         # it is a python script
>         page = page
> else:
>         #when everything else fails default
>         page = page

What do you think "page = page" is doing? Hint: you could replace
those lines with 'pass' and your code would work _exactly the same_.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Receing a form variable as a list instead of as a string

2013-06-11 Thread Νικόλαος Κούρας
But if i write it as:

if not page and os.path.exists( file ): 
# it is an html template 
page = file.replace( '/home/nikos/public_html/', '' ) 
elif page or form.getvalue('show') == 'log': 
# it is a python script 
page = page 
elif page or form.getvalue('show') == 'stats': 
page = file.replace( '/home/nikos/public_html/', '' )
==

I get the error that i cannot use 'replac'e to a 'list'.
How can i avoid that?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Receing a form variable as a list instead of as a string

2013-06-11 Thread Νικόλαος Κούρας
Τη Τρίτη, 11 Ιουνίου 2013 2:51:04 μ.μ. UTC+3, ο χρήστης Ulrich Eckhardt έγραψε:

> 
> For that, you'd have to adjust the code that you received it from. If 
> that's not possible, convert it to a string yourself. But didn't you 
> want a "form variable"?

i manages to work around it by using this:
Indeed as Andreas said i was overusing the form variable 'page'

file = form.getvalue('file')   # this comes from .htaccess
page = form.getvalue('page')   # this comes form metrites.py or index.html

if not page and os.path.exists( file ):
# it is an html template
page = file.replace( '/home/nikos/public_html/', '' )
elif page or form.getvalue('show'):
# it is a python script
page = page
else:
#when everything else fails default
page = page

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


Re: Receing a form variable as a list instead of as a string

2013-06-11 Thread Ulrich Eckhardt

Am 11.06.2013 12:38, schrieb Νικόλαος Κούρας:

File "/home/nikos/public_html/cgi-bin/metrites.py", line 28, in , 
referer: http://xxxredactedxxx/
   page = page.replace( '/home/nikos/public_html/', '' ), referer: 
http://xxxredactedxxx/
AttributeError: 'list' object has no attribute 'replace', referer: 
http://xxxredactedxxx

but page is a form variable coming from a previous sumbitted form
why the error says 'page' is a list?


Maybe because it is a list? Try e.g. "print(type(page))" or 
"print(page)" to gain some insight.




How to receive that form variable as a string?


For that, you'd have to adjust the code that you received it from. If 
that's not possible, convert it to a string yourself. But didn't you 
want a "form variable"?



Uli

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


Re: Receing a form variable as a list instead of as a string

2013-06-11 Thread Andreas Perstinger

On 11.06.2013 12:38, Νικόλαος Κούρας wrote:

but page is a form variable coming from a previous sumbitted form
why the error says 'page' is a list?


RTFM:
"If the submitted form data contains more than one field with the same 
name, the object retrieved by form[key] is not a FieldStorage or 
MiniFieldStorage instance but a list of such instances. Similarly, in 
this situation, form.getvalue(key) would return a list of strings."

http://docs.python.org/3.3/library/cgi.html#using-the-cgi-module

Bye, Andreas
--
http://mail.python.org/mailman/listinfo/python-list


Receing a form variable as a list instead of as a string

2013-06-11 Thread Νικόλαος Κούρας
page = form.getvalue('page')

if form.getvalue('show') == 'log' or page:
# it is a python script
page = page.replace( '/home/nikos/public_html/cgi-bin/', '' )
elif os.path.exists( page ):
# it is an html template
page = page.replace( '/home/nikos/public_html/', '' )



[Tue Jun 11 13:35:30 2013] [error] [client 79.103.41.173]   File 
"/home/nikos/public_html/cgi-bin/metrites.py", line 28, in , referer: 
http://superhost.gr/
[Tue Jun 11 13:35:30 2013] [error] [client 79.103.41.173] page = 
page.replace( '/home/nikos/public_html/', '' ), referer: http://superhost.gr/
[Tue Jun 11 13:35:30 2013] [error] [client 79.103.41.173] AttributeError: 
'list' object has no attribute 'replace', referer: http://superhost.gr
=

but page is a form variable coming from a previous sumbitted form
why the error says 'page' is a list?

How to receive that form variable as a string?
-- 
http://mail.python.org/mailman/listinfo/python-list