Friday, December 26, 2008
Should I make a new wheel?
I must admit that I'm a pythonic,I like everything of python.After a long time to experiment Django. I think I need my own web framework.To tell you the turth,I'm a java fan previously.I like Hibernate,spring,struts etc.So I decided to make a simple but better web framework, its ORM like hibernate, its web architecture like spring, and may be I'll create a simple server, I refer to the Django,CherryPy ,both of them are enough good.may be I just need reuse them. who knows,but for now my plan is write a simple web server firstly. I think cherryPy's server is a good reference.
Tuesday, December 16, 2008
clean_data and cleaned_data
When we use the form object in Django,Frequently,we need validate the fields of the form.for example:
We could access the form's data by attribute form.data, If it's validated we can also access the valid data by attribute form.clean_data,but when you upgrade to Django 1.0,this attribute will be no long in use.Instead of it,you should use the attribute cleaned_data:
>>>python manage.py shell
>>>from pageage1 import MyForm
>>>form=MyForm({'username':'Eric','email':'test@test.com'})
>>>form.is_valid()
True
We could access the form's data by attribute form.data, If it's validated we can also access the valid data by attribute form.clean_data,but when you upgrade to Django 1.0,this attribute will be no long in use.Instead of it,you should use the attribute cleaned_data:
...
>>>form.clean_data # Django 0.96
>>>form.cleaned_data # Django 1.0
Labels:
Django,
Django 1.0
Monday, December 15, 2008
errors and has_errors
There is a slight difference between Django 1.0 and 0.95.When we use user authentication in Django,and we create pass a form object to login.html template for example.
In version 0.95,it's work fine.but if you run it under the version 1.0,you can't see the error message when you login-failing.The question is in Django 1.0,the attribute of form object is errors instead of has_error.so change the attribute it'll work well.
<div style="margin-left: 40px;"> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Django Bookmarks - User Login</title>
</head>
<body>
<h1>User Login</h1>
{%if form.has_errors%}
<p>Your username and password didn't match.
Please try again.</p>
{%endif%}
<form method="post" action=".">
<p><label for="id_username">Username:</label>
{{ form.username }}</p>
<p><label for="id_password">Password:</label>
{{ form.password }}</p>
<input type="hidden" name="next" value="/">
<input type="submit" value="login">
</form>
</body>
</html>
In version 0.95,it's work fine.but if you run it under the version 1.0,you can't see the error message when you login-failing.The question is in Django 1.0,the attribute of form object is errors instead of has_error.so change the attribute it'll work well.
Labels:
Django
Thursday, November 27, 2008
How to write something in PDF by python
Did you ever meet this problem?your customer want to show their report by PDF format.I think so.I think the best lib to use is ReportLab ,you can download it from site and install it.Today I want to provide an simple example to illustrate how to use it,especially when you write something none standard character in it.Let's begin with a real example in our app,suppose you want to convert a text to a PDF.now you have a file at c:/log.txt,and you want to covert it to c:/h.pdf file.The code is :
It's simple that is,right?
#first we import necessary module.
from reportlab.pdfgen import canvas
#now,open the file which we want to covert to
f=open("c:/log.txt","rb")
#read each line in the file.
lines=f.readlines()
#create a canvas objects,it can write the string
#in the pdf
c=canvas.Canvas("c:/h.pdf")
#maybe the file we read was very big so we must
#consider that display them in multiple page
j=700 #for example it's height is 700 point
for row in lines:
#firstly, we do an encoding cover.
uniline=unicode(row,'latin-1')
if j==0:
j=700
#showPage method means,we want to write in next page
c.showPage()
else:
j=j-10
#drawString method will draw the text information in PDF
c.drawString(100, j, uniline.strip())
#don't forget close the file
f.close()
#don't forget save the canvas,otherwise,you can't see anything
#in the .pdf file
c.save()
It's simple that is,right?
Labels:
Python
Wednesday, November 26, 2008
The order of the Python's class inheritance
Every OOP language provide the inheritance feature.In Python one subclass could have multiple super class,For example:we have two class A and B,now we create another class C,it's inherit from A and B:
That means,the inheritance rest with which super class in the starting location,in this example,class A in the zero position,so the method1 in class A will be call instead of class B
class A:
def method1(self):
print "this is the method1 of class A"
class B:
def method1(self):
print "this is the method2 of class B"
class C(A,B): #class C inherit from A and B
pass
c=C()
c.method1()#Now the question is which method will be call,A or B
#The result is:
this is the method1 of class A
That means,the inheritance rest with which super class in the starting location,in this example,class A in the zero position,so the method1 in class A will be call instead of class B
Labels:
Python
Tuesday, November 25, 2008
A better way to create your own python path
When we program we have to create our own module,sometime we just want to test some feature,and then write a series of .py file.We want to use them conveniently.So I think a better way to do this is create a .pth file,then save it in the top level of python program.For example:
we create a file mypath.pth,then save it into c:/python25
edit this file like:
D:\my-python-module\
Then when we put new .py file in this folder, we can reuse them as they are the standard python module.
Labels:
Python
Sunday, November 23, 2008
Function argument-matching forms In Python
| Syntax | Location | Interpretation |
|---|---|---|
func(value) | Caller | Normal argument: matched by position |
func(name=value) | Caller | Keyword argument: matched by name |
def func(name) | Function | Normal argument: matches any by position or name |
def func(name=value) | Function | Default argument value, if not passed in the call |
def func(*name) | Function | Matches remaining positional args (in a tuple) |
def func(**name) | Function | Matches remaining keyword args (in a dictionary |
Labels:
Python
Subscribe to:
Posts (Atom)