Home arrow Python arrow Page 2 - Python on the Web

Creating a Warning - Python

Pythonís flexible nature means it can bend to almost any application you can imagine and web development is no exception. This article covers simple form handling and creating cookies and presents an example using everything demonstrated.

TABLE OF CONTENTS:
  1. Python on the Web
  2. Creating a Warning
  3. Cookies
  4. Sending Email
  5. A Last Example
  6. Inside uploads()...
By: Mark Lee Smith
Rating: starstarstarstarstar / 30
May 18, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Like life itself, we seem to want more control. And this next function we'll create will give us just that -- and output a warning if the field wasn't filled in correctly!

#!/usr/bin/env python

import cgi, re

form = cgi.FieldStorage()

def check(**fields):
 for field in fields:
  if field in form:
   value = form[field].value
   if re.search(fields[field], value): print value + '<br />'
   else: print field, 'was not filled in correctly!<br />'

if __name__ == '__main__':
 
 print 'Content-Type: text/htmln'
 
 if 'submit' and 'done' in form:
  check(name = '^[a-zA-Z ]+$', age = '^d{2}$')
 else:
  print 'if you were directed here in error please visit here.com'

Since this is pretty similar to our other function, I'm just going to skip over it quickly. If you've ever used Perl, then you've spotted the regular expressions hiding in there.

Regular expressions in Python are accessed though the 're' module (Python's regular expression library), for obvious reasons... regular expressions still seem like the best way to describe and check form values.

If you want to find out more about regular expressions and Python check out the 're' module at http://www.python.org/doc/2.3.3/lib/module-re.html

Then if the value isn't what we want, we get a warning instead of just ignoring the field.

Anyone wondering what **fields is all about? I'm sure someone is. The simplest explanation I can think of is that it tells the function to expect a variable number of arguments in X format (currying in functional programming) i.e.:

>>> def show(*args, **kwds):
...     print args, kwds
...
>>> show()
() {}
>>> show('interesting', 'dont ya think!', arg1 = 'str1', arg2 = 2)
('interesting', 'dont ya think!') {'arg1': 'str1', 'arg2': 2}
>>>

Simple, and very useful form time to time! Time for a cookie break... No, not the food... although, still pretty sweet over all.



 
 
>>> More Python Articles          >>> More By Mark Lee Smith
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: