Archive for the ‘Python’ Category

cx_Freeze在freebsd下恼人的问题

Friday, October 7th, 2011
在64位freebsd7.3中用cx_Freeze打包时遇到的问题
Traceback (most recent call last):
 ....
     import socket
  File "/usr/local/lib/python2.6/socket.py", line 64, in <module>
    from _ssl import SSLError as sslerror
ImportError: cannot import name SSLError
一顿找原因啊,google啊啥的各种无解啊。最后无奈看了下socket.py的源码
try:
    import _ssl
except ImportError:
    # no SSL support
    pass  嗯嗯,很好,这里pass了
else:
还好如果import不到的话pass了,于是乎,我很可耻的excludes掉了ssl
buildOptions = dict(
        compressed = True,
        optimize = 1,
        includes = ["_ctypes", "socket", '_socket', ],
        excludes = ['ssl', '_ssl', ],
        path = sys.path + ["modules"]
    )

				

sphinx的autodoc

Monday, January 24th, 2011

修改conf.py 先把项目目录加上

sys.path.append(os.path.abspath(‘d:/wsgi/biz_python’))

# — General configuration —————————————————–

添加扩展 sphinx.ext.autodoc
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named ‘sphinx.ext.*’) or your custom ones.
extensions = ['sphinx.ext.autodoc']

这就可以用了,在文档使用吧

.. function:: foo(x)
foo(y, z)
:module: some.module.name

Return a line of text input from the user.

django get_latest_by 取最后发布的记录

Tuesday, November 21st, 2006

get_latest_by
Models can have a get_latest_by attribute, which should be set to the name of a DateField or DateTimeField. If get_latest_by exists, the model’s module will get a get_latest() function, which will return the latest object in the database according to that field. “Latest” means “having the date farthest into the future.”

Model source code

from django.db import models

class Article(models.Model):
    headline = models.CharField(maxlength=100)
    pub_date = models.DateField()
    expire_date = models.DateField()
    class Meta:
        get_latest_by = 'pub_date'

    def __str__(self):
        return self.headline

class Person(models.Model):
    name = models.CharField(maxlength=30)
    birthday = models.DateField()

    # Note that this model doesn't have "get_latest_by" set.

    def __str__(self):
        return self.nameSample API usage

This sample code assumes the above models have been saved in a file mysite/models.py.

>>> from mysite.models import Article, Person

# Because no Articles exist yet, get_latest() raises ArticleDoesNotExist.
>>> Article.objects.latest()
Traceback (most recent call last):

DoesNotExist: Article matching query does not exist.

# Create a couple of Articles.
>>> from datetime import datetime
>>> a1 = Article(headline=’Article 1′, pub_date=datetime(2005, 7, 26), expire_date=datetime(2005, 9, 1))
>>> a1.save()
>>> a2 = Article(headline=’Article 2′, pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 7, 28))
>>> a2.save()
>>> a3 = Article(headline=’Article 3′, pub_date=datetime(2005, 7, 27), expire_date=datetime(2005, 8, 27))
>>> a3.save()
>>> a4 = Article(headline=’Article 4′, pub_date=datetime(2005, 7, 28), expire_date=datetime(2005, 7, 30))
>>> a4.save()

# Get the latest Article.
>>> Article.objects.latest()
<Article: Article 4>

# Get the latest Article that matches certain filters.
>>> Article.objects.filter(pub_date__lt=datetime(2005, 7, 27)).latest()
<Article: Article 1>

# Pass a custom field name to latest() to change the field that’s used to
# determine the latest object.
>>> Article.objects.latest(‘expire_date’)
<Article: Article 1>

>>> Article.objects.filter(pub_date__gt=datetime(2005, 7, 26)).latest(‘expire_date’)
<Article: Article 3>

# You can still use latest() with a model that doesn’t have “get_latest_by”
# set — just pass in the field name manually.
>>> p1 = Person(name=’Ralph’, birthday=datetime(1950, 1, 1))
>>> p1.save()
>>> p2 = Person(name=’Stephanie’, birthday=datetime(1960, 2, 3))
>>> p2.save()
>>> Person.objects.latest()
Traceback (most recent call last):

AssertionError: latest() requires either a field_name parameter or ‘get_latest_by’ in the model

>>> Person.objects.latest(‘birthday’)
<Person: Stephanie>

搞定pygtk在WINDOWS下运行时提示字体错误的问题

Tuesday, September 26th, 2006

搞定pygtk在WINDOWS下运行时提示字体错误的问题

错误提示
Noname1.py:12: PangoWarning: couldn’t load font “瀹嬩綋 9″, falling back to “San
s 9″, expect ugly output.
self.show_all()

解决办法
修改C:\GTK\etc\gtk-2.0\gtkrc文件内容

gtk-theme-name = “MS-Windows”
改为
style “user-font”
{
font_name=”Tahoma,SimSun 9″
}
widget_class “*” style “user-font”

gtk-font-name = “Tahoma, Simsun 9″
gtk-theme-name = “MS-Windows “

IIS设置python支持的CGI

Tuesday, September 19th, 2006

1。安装好Python;
2。配置IIS:
a.打开管理工具-〉Internet信息服务;
b.在网站属性上右键,进入属性设置;
c.转到主目录页,进入应用程序配置;
d.添加一个映射:可执行文件写:C:\Python23\Python.exe ”%s” %s
注意Pythong的路径要指向你安装Python的位置,同时注意参数间的空格。
扩展名写:.py
动作限制为:GET,HEAD,POST
e.选中脚本引擎,选中检查文件是否存在;
f.一路确定完成配置。
3。编写CGI脚本:

import cgi                                                                 #倒入cgi模块print ‘Content-Type: text/html’                                   #必须,输出HTML文档头
print # Blank line marking end of HTTP headers        #必须,文档头必须以空行结束

cgiParameters = cgi.FieldStorage()                            #取得Post或Get过来的参数集

# 检查看是否是我们需要的参数
if not (cgiParameters.has_key(“name”) and cgiParameters.has_key(“address”)):#如果不是输出form,要求填写name和address
print “<form action=” method=’post’ name=’form’><input name=’name’ id=’name’><input name=’address’ id=’address’><input type=’submit’ value=’submit’></form>”
print “Please fill in the name and address fields.”
else:                                                                           #如果是我们要求的参数,输出参数内容
print “<p>name:”, cgiParameters["name"].value
print “<p>address:”, cgiParameters["address"].value

4。在IIS里写Pythong的CGI脚本,就是这么简单。