易截截图软件、单文件、免安装、纯绿色、仅160KB

关于Python中一种回调方式的实现

#关于回调功能的测试
#Functor是这种回调功能的关键对象
class Functor:
    """Simple functor class."""
    def __init__( self, fn, *args ):
        self.fn = fn
        self.args = args
    def __call__( self, *args ):
        self.fn( *(self.args + args) )
#想对该函数进行回调操作       
def test_callback1(arg1, arg2):
    print "test_callback1", arg1, arg2
#先进行简单地测试
obj_call1 = Functor(test_callback1, 1111, 'qweqwe111111111')
obj_call1()
#结果:
#test_callback1 1111 qweqwe111111111
#看看过程中带入参数的方式
def test_callback2(arg1, arg2, call_arg):
    print "test_callback2", arg1, arg2, call_arg
obj_call2 = Functor(test_callback2, 2222, 'qweqwe22222222')
obj_call2(222)#过程中输入参数,并且使回调函数得到这个参数
#结果:
#test_callback2 2222 qweqwe22222222 222
#再来看看对象中的方法被用来回调
#基本原理与上面两个例子相同,但可以引入对象本身的函数
#并且也可引入其他对象进行回调,那么它的用法将会非常丰富
class Test:
    def __init__(self):
        pass
   
    def test_callback1(self, arg1, arg2):
        print "Test.test_callback 1111", arg1, arg2
   
    def test_callback2(self, arg1, arg2, call_arg):
        print "Test.test_callback 2222", arg1, arg2, call_arg
       
    def test_callback_arg(self):
        obj_call1 = Functor(self.test_callback1, 1111, 'qweqwe1111')
        print "obj_call1 = ",obj_call1
     


相关文档:

Python学习札记

Ubuntu平台下的Python操作Mysql
1.安装Ubuntu,安装Msql.
2.打开终端,输入 python
import MySQLdb
con = MySQLdb.connect(db="python")
cur = con.cursor()
count = cur.execute("select * from test")
print count
data = cur.fetchall()
print data
for d in data:
print d
import os
os.system('clear') ......

BeautifulSoup Python抓网页小例子

# -*- coding: utf-8 -*-
import urllib2
from BeautifulSoup import BeautifulSoup, Tag
import re
page = urllib2.urlopen("http://bj.ganji.com/piao/zz_%E5%8C%97%E4%BA%AC-%E5%8D%97%E6%98%8C/20100210/")
soup = BeautifulSoup(page)
#ss = soup.findAll('a', href=re.compile(r"^/piao/100.&qu ......

python下的web开发框架 Django,url配置

url配置
我们在polls这个app下创建一个
helloworld.py
from django.http import HttpResponse
def index(request):
    return HttpResponse("Hello, Django.")
修改 urls.py
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
# from django.contrib ......

python下的web开发框架 Django,创建项目

解压django,然后到其目录下安装
前提是你安装好python.并将其配置到环境变量中,然后去django的压缩文修的下,执行以下倒命令
python setup.py install
1.创建project
首先我们打开cmd, 定位到希望新建工程的目录下, 任意目录均可. 然后键入如下命令:
django-admin.py startproject hello其中hello为新工程目录文件名 ......

Python日期和字符串的互转

用的分别是time和datetime函数
'''
Created on 2009-9-2
@author: jiangqh
'''
import time,datetime
# date to str
print time.strftime("%Y-%m-%d %X", time.localtime())
#str to date
t = time.strptime("2009 - 08 - 08", "%Y - %m - %d")
y,m,d = t[0:3]
print datetime.datetime(y,m,d)

输出当前时间 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号