网站迁移,请换新站

和几个朋友一起租了一个国外主机,至此,本站的所有内容将转移到新的站点,新的开始希望给朋友们新的体验,新站的地址是

http://skyfeng.org/

欢迎访问,请以前做了小站链接的朋友转换链接,另外请订阅本站RSS的朋友转换到新的RSS地址,新的是

http://skyfeng.org/blog/?feed=rss2

谢谢~~~

Posted in 生活纪实 | Leave a comment

starting nfs common utilities failed解决办法

修改/etc/network/interfaces,加上这句

auto lo
iface lo inet loopback

就可以了~~~

Posted in 使用心得 | Tagged | Leave a comment

Ubuntu下开启Framebuffer

1.开启Framebuffer

1>打开/etc/initramfs-tools/modules文件,在末尾加上:
fbcon
vesafb
#vga16fb
nvidiafb

2>打开/etc/modprobe.d/blacklist-framebuffer,找到“
blacklist vesafb
blacklist nvidiafb
#blacklist vga16fb
”等行,在前面加上“#”把它注释掉。

3>打开/boot/grub/menu.lst,找到相应的kernel一行,加上vga参数。例如我把分辨率设置为1024x768,对应的vga数值就是0x317。

怎么确定vga的数值,请看下表:

depth–640×480-800×600-1024×768–1280×1024
8bit———-769———-771———–773————775
15bit——–784———-787———–790————793
16bit——–785———-788———–791————794
24bit——–786———-789———–792————795

另外还可以使用:sudo hwinfo --framebuffer来确定你的分辨率

kernel /boot/vmlinuz-2.6.22-14-generic root=UUID=3c51a0d7-d373-473b-830e-225b6d7aafdf ro quiet splash  video=vesafb:1024x768-16@50 vga=0x317

4>最后执行:sudo update-initramfs -u更新.

5>重启一下。

2.显示中文字体

首先确定自己的locale

export LC_ALL=zh_CN.UTF-8

1.fbterm(推荐)

编辑 ~/.fbtermrc找到font-names=mono,将mono换成中文字体,如:font-names=Courier New, simsun,你还可以自定义其他的设置。可以参考这里

2.jfbterm

sudo apt-get install jfbterm unifont

* 執行 jfbterm
* 若出現找不到 unifont.pcf.gz 的錯誤時, 請
locate unifont.pcf.gz
* 尋找出正確的 unifont.pcf.gz 字型路徑後, 請修改 /etc/jfbterm.conf 將路徑設為正確。
* 例如: +fontset: iso10646.1,pcf,U,/usr/X11R6/lib/X11/fonts/misc/unifont.pcf.gz

3.显示图片(fbi)

安装fbi,执行fbi /path/to/pic就可以看了。

4.音乐(xmms2)

具体参考man手册

5.播放电影(mplayer)

记住要播放电影/dev/fb0必须有读写的权限,否则是不能看的。

可以先执行一下chmod 777 /dev/fb0

mplayer -vo fbdev /path/to/movie

可以用mplayer -vo help来查看参数。

mplayer -vo fbdev -zoom -x 1024 -y 768 /path/to/movie

用指定的分辨率来播放电影

6.截图(fbgrab)

fbgrab pic

Posted in 使用心得 | Tagged | Leave a comment

lfs时awk出现的问题

mawk: scripts/gen-sorted.awk: line 19: syntax error at or near ] mawk: scripts/gen-sorted.awk: line 19: runaway regular expression /, "", subd ... make[1]: *** No rule to make target `/mnt/lfs/sources/glibc-build/Versions.all', needed by `/mnt/lfs/sources/glibc-build/abi-versions.h'. Stop. make[1]: Leaving directory `/mnt/lfs/sources/glibc-2.8-20080929' make: *** [all] Error 2

原因,os上的awk是mawk
解决办法:删除mawk,安装gawk

Posted in 生活纪实 | Tagged , | Leave a comment

linux下使用ctrl+alt+del

NAME
       ctrlaltdel - set the function of the Ctrl-Alt-Del combination
SYNOPSIS ctrlaltdel hard | soft
DESCRIPTION Based on examination of the linux/kernel/sys.c code, it is clear that there are two supported functions that the Ctrl-Alt-Del sequence can perform: a hard reset, which immediately reboots the computer without calling sync (2) and without any other preparation; and a soft reset, which sends the SIGINT (inter- rupt) signal to the init process (this is always the process with PID 1). If this option is used, the init (8)program must support this feature. Since there are now several init (8) programs in the Linux community, please consult the documentation for the version that you are currently using.
ctrlaltdel is usually used in the /etc/rc.local file.
FILES /etc/rc.local
SEE ALSO simpleinit(8), init(8)
AUTHOR Peter Orbaek (poe@daimi.aau.dk)
AVAILABILITY The ctrlaltdel command is part of the util-linux-ng package and is available from ftp://ftp.kernel.org/pub/linux/utils/util-linux-ng/.
Posted in 生活纪实 | Tagged | Leave a comment

django panination分页

django自带了一个分页器,功能比较强大,但是有很多东西需要自己去设计,很浪费时间,关于django自带的paginator的资料和使用方法可以参照以下链接http://www.djangoproject.com/documentation/models/pagination/

下面介绍3种第三方的分页器,功能不错,都已经把视图和模板写好的,可以直接使用,具体方法,请自行参照官方文档。

1.django-pagination

http://pypi.python.org/pypi/django-pagination/

2.django-endless-pagination

http://pypi.python.org/pypi/django-endless-pagination

3.django-sorting

http://pypi.python.org/pypi/django-sorting

Posted in PYTHON学习 | Tagged , , | Leave a comment

在django项目中使用memcache

1.download memcached from http://www.danga.com/memcached/,then install it youself.
2.download a middleware to use memcache that fit django,there are three choices,The fastest available option is a module called cmemcache, available at http://gijsbert.org/cmemcache/ . If you can’t install cmemcache, you can install python-memcached, available at ftp://ftp.tummy.com/pub/python-memcached/ . If that URL is no longer valid, just go to the Memcached Web site (http://www.danga.com/memcached/) and get the Python bindings from the “Client APIs”section.
3.To use Memcached with Django, set CACHE_BACKEND to memcached://ip:port/, where ip is the IP address of the Memcached daemon and port is the port on which Memcached is running.In this example, Memcached is running on localhost (127.0.0.1) port 11211:CACHE_BACKEND = 'memcached://127.0.0.1:11211/' .One excellent feature of Memcached is its ability to share cache over multiple servers. This means you can run Memcached daemons on multiple machines,just config it like this:CACHE_BACKEND = 'memcached://172.19.26.240:11211;172.19.26.242:11211/'
4.start memcached thread with command:/usr/local/bin/memcached -m 60 -vv.
5.you can test it with ab program,use command:ab -n 10000 -c 100 http://ip/dir/file,-n 10000 means 10000 times,-c 100 means 100 request  one time

1.下载memcache,http://www.danga.com/memcached/.编译安装。
2.下载python对于memcached的绑定,有3种选择首先是cmemcache,http://gijsbert.org/cmemcache/,然后是libmemcached: http://download.tangent.org/libmemcached-0.30.tar.gz,最后一个是python-memcache,ftp://ftp.tummy.com/pub/python-memcached/,cmemcache是最快的,但是很久没有更新了,推荐libmemcached,如果上面都不行,使用另一个吧~~
3.设置 CACHE_BACKEND 为 memcached://ip:port/ 来让Django使用Memcached,这里的 ip 是Memcached后台进程的IP地址, port 则是Memcached运行所在的端口。在这个例子中,Memcached运行在本地主机 (127.0.0.1)上,端口为11211:CACHE_BACKEND = 'memcached://127.0.0.1:11211/'Memcached的一个极好的特性是它在多个服务器分享缓存的能力,这意味着你可以在多台机器上运行Memcached进程CACHE_BACKEND = 'memcached://172.19.26.240:11211;172.19.26.242:11211/'这个例子中,缓存在运行在172.19.26.240和172.19.26.242的IP地址和11211端口的Memcached实例间分享
4.使用下面的命令开始mencached进程:usr/local/bin/memcached -m 60 -vv
5.你可以使用ab命令来测试做了cache后的效率ab -n 10000 -c 100 http://ip/dir/file,-n 10000是发送一万个请求,-c 100代表100个并发
6.如果要做全站缓存,只要将``’django.middleware.cache.CacheMiddleware’`` 添加到 MIDDLEWARE_CLASSES 的设置里,就可以了,其中有一些参数的设置,请自行参考文档
7.视图级缓存,完成这项工作的方式是使用修饰器,其作用是包裹视图函数,将其行为转换为使用缓存。视图缓存修饰器称为 cache_page ,位于 django.views.decorators.cache 模块中
from django.views.decorators.cache import cache_page
@cache_page(60 * 15)
def my_view(request, param):
另外,视图级缓存还可以做到urls中,例如(r'^foo/(\d{1,2})/$', cache_page(my_view, 60 * 15)),就可以了

Posted in PYTHON学习 | Tagged | Leave a comment

Writing your own django middleware

Writing your own middleware

Writing your own middleware is easy. Each middleware component is a single Python class that defines one or more of the following methods:

process_request

process_request(self, request)

request is an HttpRequest object. This method is called on each request, before Django decides which view to execute.

process_request() should return either None or an HttpResponse object. If it returns None, Django will continue processing this request, executing any other middleware and, then, the appropriate view. If it returns an HttpResponse object, Django won't bother calling ANY other request, view or exception middleware, or the appropriate view; it'll return that HttpResponse. Response middleware is always called on every response.

process_view

process_view(self, request, view_func, view_args, view_kwargs)

request is an HttpRequest object. view_func is the Python function that Django is about to use. (It's the actual function object, not the name of the function as a string.) view_args is a list of positional arguments that will be passed to the view, and view_kwargs is a dictionary of keyword arguments that will be passed to the view. Neither view_args nor view_kwargs include the first view argument (request).

process_view() is called just before Django calls the view. It should return either None or an HttpResponse object. If it returns None, Django will continue processing this request, executing any other process_view() middleware and, then, the appropriate view. If it returns an HttpResponse object, Django won't bother calling ANY other request, view or exception middleware, or the appropriate view; it'll return that HttpResponse. Response middleware is always called on every response.

process_response

process_response(self, request, response)

request is an HttpRequest object. response is the HttpResponse object returned by a Django view.

process_response() must return an HttpResponse object. It could alter the given response, or it could create and return a brand-new HttpResponse.

Unlike the process_request() and process_view() methods, the process_response() method is always called, even if the process_request() and process_view() methods of the same middleware class were skipped because an earlier middleware method returned an HttpResponse (this means that your process_response() method cannot rely on setup done in process_request(), for example). In addition, during the response phase the classes are applied in reverse order, from the bottom up. This means classes defined at the end of MIDDLEWARE_CLASSES will be run first.

process_exception

process_exception(self, request, exception)

request is an HttpRequest object. exception is an Exception object raised by the view function.

Django calls process_exception() when a view raises an exception. process_exception() should return either None or an HttpResponse object. If it returns an HttpResponse object, the response will be returned to the browser. Otherwise, default exception handling kicks in.

Again, middleware are run in reverse order during the response phase, which includes process_exception. If an exception middleware return a response, the middleware classes above that middleware will not be called at all.

__init__

Most middleware classes won't need an initializer since middleware classes are essentially placeholders for the process_* methods. If you do need some global state you may use __init__ to set up. However, keep in mind a couple of caveats:

  • Django initializes your middleware without any arguments, so you can't define __init__ as requiring any arguments.
  • Unlike the process_* methods which get called once per request, __init__ gets called only once, when the web server starts up.

Marking middleware as unused

It's sometimes useful to determine at run-time whether a piece of middleware should be used. In these cases, your middleware's __init__ method may raise django.core.exceptions.MiddlewareNotUsed. Django will then remove that piece of middleware from the middleware process.

Guidelines

  • Middleware classes don't have to subclass anything.
  • The middleware class can live anywhere on your Python path. All Django cares about is that the MIDDLEWARE_CLASSES setting includes the path to it.
  • Feel free to look at Django's available middleware for examples.
  • If you write a middleware component that you think would be useful to other people, contribute to the community! Let us know, and we'll consider adding it to Django.
中间件方法
既然我们知道了什么是中间件和怎样安装它,让我们来看看中间件类可能定义的所有可得到的方法

初始化:__init__(self)
如果中间件类定义了一个构造方法(即一个__init__方法),它应该不使用参数(除了标准的slef)
出于性能考虑,中间件类在长时间运行的服务器过程中只初始化一次,这意味着你不能指望每次请求运行时调用__init__,
只有在服务器启动时调用一次
中间件类可能也使用初始化时间来删除它们本身而不是被安装,如果初始化时触发django.exceptions.MiddlewareNotUsed异
常,则Django将从中间件堆栈删除该中间件,你可能使用这点来检查中间件类依赖的一些软件,或者服务器是否运行在调试
模式下,或者任何其它可能让你想禁止中间件的类型的环境

请求预处理:process_request(self, request)
该方法在请求被接收和URL被解析来决定运行哪个视图之前立即调用,它传递你可能想修改的HttpRequest对象
process_request()应该返回None或者HttpResponse对象,如果它返回None,Django将继续处理该请求,执行任何其它的中间
件然后是合适的视图
如果请求中间件返回HttpResponse对象,Django将不会再调用其它任何中间件(任何类型)或者合适的视图,它将返回该应答

视图预处理:process_view(self, request, view, args, kwargs)
该方法在请求中间件运行后和URL被解析到一个视图后和视图实际上被调用之前被调用
传递给该视图的参数为:
Argument           Explanation
request            HttpRequest对象
view               Django将会调用来树立该请求的Python方法,它是实际上的方法对象本身,而不是方法名字符串
args               将被传递给视图的位置参数列表,不包括request参数(它一直是视图的第一个参数)
kwargs             将被传递给视图的关键字参数字典
像process_request()一样,process_view()应该返回None或者HttpResponse对象,如果它返回None,Django将继续处理请求
执行任何其它的视图中间件然后是合适的视图
如果视图中间件返回HttpResponse对象,Django将不会再调用其它任何中间件或者合适的视图,它将返回该应答

应答后处理:process_response(self, request, response)
该方法再视图方法已经调用和应答生成后调用,这是中间件修改应答输出的地方,输出压缩(见下)是应答中间件的一个显然
的应用
参数应该非常自明了--request是请求对象,response是从视图返回的应答对象
不像请求和视图中间件,它们可以返回None,process_response()必须返回一个HttpResponse对象,该应答可以是传递给该
方法的原始应答(可能被修改了)或者新的应答

异常后处理:process_exception(self, request, exception)
该方法只在出错并且视图触发不可捕获的异常时调用,不包括Http404异常,你可以使用这个钩子来发送错误通知,在一个日
志文件记录信息,或者甚至尝试自动恢复该错误
该方法的参数是我们一直处理的同样的request对象和视图方法触发的真正的Exception对象exception
process_exception()可能返回一个作为应答显示给浏览器的HttpResponse或者返回None来继续Django内建的异常处理

例子
Django自带一些中间件类--上面讨论了--它们是很好的例子,阅读它们的代码应该给你中间件的力量的好的感觉
你也可以在Django的wiki上找到一些社区贡献的例子:
http://code.djangoproject.com/wiki/ContributedMiddleware

Posted in PYTHON学习 | Tagged | Leave a comment

Providing initial data for models

It’s sometimes useful to pre-populate your database with hard-coded data when you’re first setting up an app. There’s a couple of ways you can have Django automatically create this data: you can provide initial data via fixtures, or you can provide initial data as SQL.

In general, using a fixture is a cleaner method since it’s database-agnostic, but initial SQL is also quite a bit more flexible.
Providing initial data with fixtures

A fixture is a collection of data that Django knows how to import into a database. The most straightforward way of creating a fixture if you’ve already got some data is to use the manage.py dumpdata command. Or, you can write fixtures by hand; fixtures can be written as XML, YAML, or JSON documents. The serialization documentation has more details about each of these supported serialization formats.

As an example, though, here’s what a fixture for a simple Person model might look like in JSON:

[
  {
    "model": "myapp.person",
    "pk": 1,
    "fields": {
      "first_name": "John",
      "last_name": "Lennon"
    }
  },
  {
    "model": "myapp.person",
    "pk": 2,
    "fields": {
      "first_name": "Paul",
      "last_name": "McCartney"
    }
  }
]

And here's that same fixture as YAML:

- model: myapp.person
  pk: 1
  fields:
    first_name: John
    last_name: Lennon
- model: myapp.person
  pk: 2
  fields:
    first_name: Paul
    last_name: McCartney

You'll store this data in a fixtures directory inside your app.

Loading data is easy: just call manage.py loaddata fixturename, where fixturename is the name of the fixture file you've created. Every time you run loaddata the data will be read from the fixture and re-loaded into the database. Note that this means that if you change one of the rows created by a fixture and then run loaddata again you'll wipe out any changes you've made.
Automatically loading initial data fixtures

If you create a fixture named initial_data.[xml/yaml/json], that fixture will be loaded every time you run syncdb. This is extremely convenient, but be careful: remember that the data will be refreshed every time you run syncdb. So don't use initial_data for data you'll want to edit.

See also

Fixtures are also used by the testing framework to help set up a consistent test environment.
Providing initial SQL data¶

Django provides a hook for passing the database arbitrary SQL that's executed just after the CREATE TABLE statements when you run syncdb. You can use this hook to populate default records, or you could also create SQL functions, views, triggers, etc.

The hook is simple: Django just looks for a file called sql/.sql, in your app directory, where is the model's name in lowercase.

So, if you had a Person model in an app called myapp, you could add arbitrary SQL to the file sql/person.sql inside your myapp directory. Here's an example of what the file might contain:

INSERT INTO myapp_person (first_name, last_name) VALUES ('John', 'Lennon');
INSERT INTO myapp_person (first_name, last_name) VALUES ('Paul', 'McCartney');

Each SQL file, if given, is expected to contain valid SQL statements which will insert the desired data (e.g., properly-formatted INSERT statements separated by semicolons).

The SQL files are read by the sqlcustom, sqlreset, sqlall and reset commands in manage.py. Refer to the manage.py documentation for more information.

Note that if you have multiple SQL data files, there's no guarantee of the order in which they're executed. The only thing you can assume is that, by the time your custom data files are executed, all the database tables already will have been created.
Database-backend-specific SQL data¶

There's also a hook for backend-specific SQL data. For example, you can have separate initial-data files for PostgreSQL and MySQL. For each app, Django looks for a file called /sql/..sql, where is your app directory, is the model's name in lowercase and is the value of DATABASE_ENGINE in your settings file (e.g., postgresql, mysql).

Backend-specific SQL data is executed before non-backend-specific SQL data. For example, if your app contains the files sql/person.sql and sql/person.postgresql.sql and you're installing the app on PostgreSQL, Django will execute the contents of sql/person.postgresql.sql first, then sql/person.sql.

link:http://docs.djangoproject.com/en/dev/howto/initial-data/

Posted in PYTHON学习 | Tagged , | Leave a comment

查看py文档

import pydoc
pydoc.serve(888 8)

Posted in 生活纪实 | 1 Comment