Form and math.pow Function Help.

This is the place for queries that don't fit in any of the other categories.

Form and math.pow Function Help.

Postby paulm » Sun Jun 16, 2013 1:42 am

math.pow exponent function takes 2 integers separated by a comma. I've been advised to use map and split functions, but I need help implementing with form. Please walk me through this, using the form specifically. 500 errors with any of the following...it "breaks" beyond printing "raw" (which works). Errors include:

AttributeError: 'NoneType' object has no attribute 'split'
map = math.pow(*map(int, raw.split(',')))
and
ValueError: invalid literal for int() with base 10: 'raw'
map = math.pow(*map(int, 'raw'.split(',')))

Code: Select all
form = cgi.FieldStorage()

raw = form.getvalue('input')
map = math.pow(*map(int, raw.split(',')))

#map = map(raw.split(","))
#pow(*map(int, raw.split(",")))


print"""

<html>
<body>
<p>Enter base and exponent, separated by a comma.</p>
<form method='post' action="/">
<input type = 'text' name='input'/></p>
<input type = 'submit' value="Calc"></p>
</form>
paulm
 
Posts: 8
Joined: Sun Jun 16, 2013 1:36 am

Re: Form and math.pow Function Help.

Postby hansn » Sun Jun 16, 2013 8:51 am

You say printing raw works, but your error says that it is a NoneType. What happens when you print it?

What are you trying to do here? (I have never used the cgi module)

I don't know what kind of input you are dealing with so I'm probably wrong. But it seems to me that you want to raise every number in a string to the power of some number.
Using map, that would go something like this:
Code: Select all
>>> input = '1 2 3 4'
>>> raw = [int(x) for x in list(input.split())] # Convert input to an integer list
>>> map(lambda x: math.pow(x, 2), raw) # Raise every integer to the power of 2
[1.0, 4.0, 9.0, 16.0]

If this does not help you can tell me what kind of input you are dealing with and what you want to do with it and I can probably help you out.
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Form and math.pow Function Help.

Postby paulm » Sun Jun 16, 2013 11:03 am

Thanks for reply.

Printing raw input works, (the form.getvalue one). But when I add the other stuff, the errors get thrown. These errors and the lines they pertain to are clearly shown on my post above. math.pow is a Python math function, I don't have to raise anything to any power, the function does that. math.pow requires 2 number inputs separated by a comma, as mentioned in my post.
paulm
 
Posts: 8
Joined: Sun Jun 16, 2013 1:36 am

Re: Form and math.pow Function Help.

Postby Mekire » Sun Jun 16, 2013 12:08 pm

paulm wrote:AttributeError: 'NoneType' object has no attribute 'split'
map = math.pow(*map(int, raw.split(',')))
and
ValueError: invalid literal for int() with base 10: 'raw'
map = math.pow(*map(int, 'raw'.split(',')))
The first one: split is a string method and apparently raw is not a string according to your own error.
The second one: You used the string "raw" which is certainly not what you wanted to do. You then tried to map int to it.
Try typing:
Code: Select all
int("raw")
and you will get the same error because that is what you are doing here.

Please show us exactly what raw is. According to your errors, as hans said, it is None.

If raw actually was a string you could do stuff like this:
Code: Select all
import math
raw_list = '1 2 3 4'

pow_list = [math.pow(x,2) for x in map(int,raw_list.split())]
print(pow_list)
Code: Select all
>>>
[1.0, 4.0, 9.0, 16.0]
>>>

-Mek

Edit:
On further inspection this appears to be what you are doing:
Code: Select all
import math
raw = "5,2"
pow_it = math.pow(*map(int,raw.split(",")))
print(pow_it)
Code: Select all
>>>
25.0
>>>
as you can see, this works fine, which means the problem here is with your raw variable.
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Form and math.pow Function Help.

Postby paulm » Sun Jun 16, 2013 4:36 pm

Mek, thanks for reply.

This is a form we're talking about: raw = form.getvalue('input') << 'input' is the form value from form, shown in first post. Me typing 2 static numbers (as you suggest) in place of the form value would not be using a form...

It's all in my first post.

It's easy to use math.pow in the command line, typing in values as others suggest here. That's how I started this. But now I'm using a form...a form...a form.
paulm
 
Posts: 8
Joined: Sun Jun 16, 2013 1:36 am

Re: Form and math.pow Function Help.

Postby hansn » Sun Jun 16, 2013 4:59 pm

Like I said in my first post. It appears that your 'raw' variable contains a NoneType (as suggested by the error message you posted).
Would you be so kind as to post the output of 'print raw'?

I looked briefly at the cgi documentation and from what I understand the getValue method does not have some kind of standard return value, it can even return different data types. How are we supposed to know what your data looks like?

Also, repeating yourself wont help us understand, we allready read your first post and obviously we(I) require some more information to help you.
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Form and math.pow Function Help.

Postby paulm » Sun Jun 16, 2013 6:47 pm

Thanks for reply.

Entering 2,3 in form does print 2,3 with print raw, no errors.
paulm
 
Posts: 8
Joined: Sun Jun 16, 2013 1:36 am

Re: Form and math.pow Function Help.

Postby hansn » Sun Jun 16, 2013 6:58 pm

So given that value, you want to raise 2 to the power of 3?

In that case:
Code: Select all
>>> import math
>>> raw = '2,3'
>>> l = [int(x) for x in raw if x.isdigit()]
>>> l
[2, 3]
>>> math.pow(l[0], l[1])
8.0
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Form and math.pow Function Help.

Postby paulm » Sun Jun 16, 2013 7:17 pm

I'm afraid my communication is off...

As I've tried to express unsuccessfully, plugging in the value works, like you've done. But, try doing this with my form, and use raw = form.getvalue('input') Type 2,3 in the form and see what happens.

That's where the problem is...between the form input and math.pow.
paulm
 
Posts: 8
Joined: Sun Jun 16, 2013 1:36 am

Re: Form and math.pow Function Help.

Postby hansn » Sun Jun 16, 2013 7:56 pm

What do you mean 'your form'? Whatever that is, I don't have access to it. You have to show me what data you are working on for me to help you.

I tried running your code, which obviously threw the same errors that you got, however when I did 'print raw' I got back 'None', So I don't know what you are really doing here.


The problem with your code is this line:
Code: Select all
raw = form.getvalue('input')

form.getvalue('input') returns None

It's probably because of this line:
Code: Select all
form = cgi.FieldStorage()

Which returns this: FieldStorage(None, None, [])

So you're not really working with a 'form', you are working with None.

My suggestion: Go read the cgi docs, and see if the line form = cgi.FieldStorage() is what you really want.
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Form and math.pow Function Help.

Postby paulm » Sun Jun 16, 2013 9:11 pm

Thanks for the effort...

Check out my first post. You'll see the form.
paulm
 
Posts: 8
Joined: Sun Jun 16, 2013 1:36 am

Re: Form and math.pow Function Help.

Postby hansn » Sun Jun 16, 2013 10:03 pm

Reading online about the cgi module I find this:

The main use of the cgi module is to extract the values passed to a CGI program from an HTML form.

This is most likely where your problem is. I don't know how you make your script access' that form of yours, but your form variable does not contain any data so it's probably not working.


Good luck. If you figure it out please let me know, I'm also qurious how you try to access the form as I have no idea how the cgi module works.
hansn
 
Posts: 87
Joined: Thu Feb 21, 2013 8:46 pm

Re: Form and math.pow Function Help.

Postby Mekire » Mon Jun 17, 2013 12:11 am

paulm wrote:Me typing 2 static numbers (as you suggest) in place of the form value would not be using a form...
I was in no way suggesting you type two static numbers. I was demonstrating that, given the correct input, that function works perfectly. If your getvalue is not giving you a string of this exact formant, then that is where your problem lies. You aren't going to convince str.split to take a non-string as an argument. You need to investigate why your
Code: Select all
raw = form.getvalue('input')
function is not giving you a string and fix that problem. No amount of changing the algorithm is going to make it work if the input is faulty.

Code: Select all
import cgi
form = cgi.FieldStorage()
raw = form.getvalue('input')
print(raw)
Code: Select all
>>>
None
>>>
Wherever your data is stored, I can tell you one thing. You never accessed it.
-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Form and math.pow Function Help.

Postby Mekire » Mon Jun 17, 2013 7:21 am

After quite a bit of research I managed to figure out what was going on here.

Short version. This works for me:
Code: Select all
import cgi,cgitb,math
cgitb.enable()
form = cgi.FieldStorage()

raw = form.getvalue('input')
try:
    result = math.pow(*map(int, raw.split(',')))
except (ValueError,TypeError,AttributeError):
    result = None

print(
"""
<html>
<body>
<p>Enter base and exponent, separated by a comma.</p>
<form action="/cgi-bin/test_cgi.py" method="post">
<input type = 'text' name='input'/></p>
<input type = 'submit' value="Calc"></p>
</form>
""")

if result != None:
    print("Result: {}".format(result))
else:
    print("Invalid input")
print("</body></html>")


Long version. I used this the following to locally host the cgi script so I could test it:
Code: Select all
from BaseHTTPServer import HTTPServer
from CGIHTTPServer import CGIHTTPRequestHandler

serv = HTTPServer(("", 8080), CGIHTTPRequestHandler)
serv.serve_forever()

I then made a folder called "cgi-bin" within the folder that I ran the hosting script from and saved your script as "test_cgi.py" within it. I then accessed the page via the address:
http://localhost:8080/cgi-bin/test_cgi.py

But yeah, anyway, hopefully this solves your problem. Basically you were trying to do the calculation before you ever submitted the data and got an exception.
-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Form and math.pow Function Help.

Postby paulm » Mon Jun 17, 2013 1:10 pm

Great job Mek, thanks a lot!

Short version works here (using GAE's dev_appserver.py for development). Error handling working too...not sure why previous was not working as I'm not seeing anything different with your math.pow function...does the new error handling have something to do with it?

Thanks a lot for getting me started. As I study math I want to develop user-input forms like this with more advanced math functions.
paulm
 
Posts: 8
Joined: Sun Jun 16, 2013 1:36 am

Re: Form and math.pow Function Help.

Postby Mekire » Mon Jun 17, 2013 1:23 pm

That was actually the point I was initially trying to get across.
There was never anything wrong with your original function.

The problem is that when your program starts you have not yet given an input, so when the line:
Code: Select all
raw = form.getvalue('input')
runs, it sets raw to None. Then python tries to do None.split() which of course fails because None does not have a split method. You have to catch that exception so your program doesn't fail (well you could do it other ways too, but catching it is straight forward).

Catching the AttributeError stops the None.split() from killing your code. The other two exceptions we catch are there to stop invalid inputs. ValueError catches the exceptions thrown when you try to map the int function to an inappropriate string. TypeError catches exceptions thrown when math.pow() gets something it doesn't like.

-Mek
User avatar
Mekire
 
Posts: 987
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Form and math.pow Function Help.

Postby paulm » Mon Jun 17, 2013 2:42 pm

I get it now, Mek, thanks for the explanation. That did cross my mind previously, but now you've explained it with the exception handling. And thanks hansn too for the effort.
paulm
 
Posts: 8
Joined: Sun Jun 16, 2013 1:36 am


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot], crazychemist, snippsat and 5 guests