Oh, pshaw!

 

cs490_september26

Page history last edited by James 1 year, 1 month ago

Random Numbers

Random numbers can easily be generated using the "random" module.

random.randint(a,b)Returns a random integer number, a <= Value <= b
random.uniform(a,b)Returns a random floating point number, a <= Value < b
random.random()Returns a random floating point number in the range of 0.0 <= Value < 1.0
random.choice(list)Returns a randomly selected item from list
random.shuffle(list)Shuffles all of the values in list in place, returns nothing

>>> random.random()        # Random float x, 0.0 <= x < 1.0
0.37444887175646646
>>> random.uniform(1, 10)  # Random float x, 1.0 <= x < 10.0
1.1800146073117523
>>> random.randint(1, 10)  # Integer from 1 to 10, endpoints included
7
>>> random.randrange(0, 101, 2)  # Even integer from 0 to 100
26
>>> random.choice('abcdefghij')  # Choose a random element
'c'
>>> items = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(items)
>>> items
[7, 3, 2, 5, 6, 4, 1]
>>> random.sample([1, 2, 3, 4, 5],  3)  # Choose 3 elements
[4, 1, 5]

Cookies

HTTP is for the most part stateless. The actions that take place on one page do not change the actions on another page. There are two ways to change this. First, is INPUT forms, which we used on the last homework assignment. Using an input form, we changed the header information on a page and can communicate with a server. Cookies work similar to input forms, but are used to communicate with a client. Both input forms and cookies use HTTP headers to pass information back and forth.

When a web browser request a page, the sever will read all of the HTTP headers in the request. This includes information placed in input forms. When the server forms the response page, it has the option of sending new headers in the response. In this header information are where the cookies are stored. The next time the browser selects a page from server, the cookie information is carried with it (unless the user selects to disable cookies).

To set a cookie, three things are needed:

  1. A Key to identify a cookie (string)
  2. A Value which represents that key (string)
  3. An expiration date, which tells the browser when to stop using this cookie. (integer)

To set a cookie:
c = Cookie.Cookie('key', value) # Cookie created, but not set
c.expires = time.time() + 30 * 24 * 60 * 60 # Expires in 30 Days
Cookie.add_cookie(req, c) # Cookie added

To get a cookie:

value = 0 # Some default value
cookies = Cookie.get_cookies(req) # This returns a dictionary
if 'key' in cookies:
value = int(cookies['key'].value)

#!/usr/bin/env python
try:
from mod_python import apache
from mod_python import Cookie
except ImportError:
pass
import time
def index(req):
s = """<html>
<head><title>Welcome!</title></head>
<body>
<h1>%s</h1>
</body>
</html>"""
cookies = Cookie.get_cookies(req)
visits = 1
if 'visits' in cookies:
visits = int(cookies['visits'].value)
message = "You have visited this page %s times." % (visits)
else:
message = "This is your first visit."
c = Cookie.Cookie('visits', visits+1)
c.expires = time.time() + 30 * 24 * 60 * 60 # Expires in 30 Days
Cookie.add_cookie(req, c)
return s % message

Hand back the test and discuss

Comments (0)

You don't have permission to comment on this page.