Posts tagged Python
July 14th, 2009
web.py captcha form
Topics: Web.py, Python, CodeI coded a basic captcha for my web.py form, here’s how:
First I made a function that generates an image and returns it + the code (yea yea it’s a mess).
import Image, ImageDraw, ImageFont, cStringIO, random
def getCaptcha():
im = Image.new("RGB", (100, 60))
draw = ImageDraw.Draw(im)
for x in range(0, 100):
for y in range(0, 60):
draw.point((x, y), (135, 191, 107))
font = ImageFont.truetype('cracked.ttf', 50)
alphabet = 'abcdefghijklmnopqrstuvwxyz'
word = ''
for i in range(5):
word = word + alphabet[random.randint(0, len(alphabet) -1)]
draw.text((5, 5), word, font=font, fill=(0, 0, 0))
f = cStringIO.StringIO()
im.save(f, "GIF")
f.seek(0)
return word, f
Now here’s snippets of web.py code:
urls = (
...
'/captcha.gif', 'captcha'
)
if web.config.get('_session') is None:
session = web.session.Session(app, web.session.DiskStore('sessions'), initializer={'captcha': ''})
web.config._session = session
else:
session = web.config._session
vcaptcha = form.Validator('Please enter the code', lambda x:x == session.captcha)
enquiry_form = form.Form(
...
form.Textbox("captcha", vcaptcha, description="Validation Code", pre="<img src='/captcha.gif' valign=center><br>", class_="standard", style="width:70px;"),
)
class captcha:
def GET(self):
web.header("Content-Type", "image/gif")
captcha = getCaptcha()
session.captcha = captcha[0]
return captcha[1].read()
Also to make the form look prettier I modified web.py to give the table rows an id and then used css to alter their padding etc.
Sorry I didn’t take much time over this post, hopefully the code will get you started though, Dave.
July 10th, 2009
web.py tutorial + sqlite
Topics: Python, Web.py, HowtoIf you want to follow the web.py tutorial using sqlite here’s how:
Line for your python to connect to DB
db = web.database(dbn='sqlite', db='testdb')
To create the database type
sqlite3 testdb
and input this SQL:
CREATE TABLE todo (id integer primary key, title text, created date, done boolean default 'f');
CREATE TRIGGER insert_todo_created after insert on todo
begin
update todo set created = datetime('now')
where rowid = new.rowid;
end;
Now create an entry with this SQL
insert into todo (title) values ('Learn web.py');Finally quit sqlite3 when your ready by typing
.quit
July 10th, 2009
I like Python
Topics: Python, ThoughtsSo I decided to learn Python and Django in the hopes it would be a half way house between PHP and Lisp, letting me actually get stuff done whilst not being PHP.
My first impression of Django has been pretty bad, I bought the ‘definitive guide’ book only to find it was not so much definitive as obsolete. Also the amount of arbritary seeming magic didn’t impress. Having said that I have passed through that and I’m beginning to get the hang of it.
Python on the other hand was different, to start with I HATED the look of the code compared to Lisp but after a few hours hacking it doesn’t seem that bad. For my first program I decided to solve this little puzzle. This great guide got me going quickly and along with a few small pointers from verte in the friendly seeming #python I came to a solution.
def draw_diamond(letter):
alphabet = 'abcdefghijklmnopqrstuvqxyz'
size = alphabet.find(letter)
for x in range(size) + range(size, -1, -1):
line = [' '] * ((2 * size) + 1)
line[size + x] = alphabet[x]
line[size - x] = alphabet[x]
print "".join(line)
draw_diamond('d')
It didn’t take long, the code looks nice and most importantly I enjoyed writing it. I’m pretty impressed!