python解决经典算法八皇后问题,在棋盘上放置8个皇后,而不让她们之间互相攻击。 import sys, itertools from sets import Set NUM_QUEENS = 8 MAX
python操作mysql数据库的相关操作实例 # -*- coding: utf-8 -*- #python operate mysql database import MySQLdb #数据库名称
#! /usr/bin/python # -*- coding:utf-8 -*- ''' Created on 2013-12-18 @author: Java ''' import re from
def URLtoUTF8(string): """""" g_code_type = ['utf-8', 'utf8', 'gb18030', 'gb2312', 'gbk', 'ISO-8859-2'] try: tmp = urllib.unquote(str(string)) code = chardet.detect(tmp)['encoding'] try: g_code_type.i
from PIL import Image, ImageDraw, ImageFont import os def water_work(img): x, y = img.size textx = int(x*1.5) texty = int(y*1.5) blank = Image.new('RGB', (textx, texty), 'white') draw = ImageDraw.Draw
使用非阻塞加多线程的方式,轻松实现python网络服务器框架。 工作总结 由于Python天生的优点,特别适用于快速实现功能。 #!/usr/bin/python2.7 import sys import
import datetime # Get a date object today = datetime.date.today() # General functions print "Year: %d" % today.year print "Month: %d" % today.month print "Day: %d" % today.day print "Weekday: %d" % to
当我们遇到大量小文件的传输时,一般会涉及到文件的压缩和解压,下面对zip的压缩解压直接上代码 压缩: def zip_dir(dirname,zipfilename): """ | ##@函数目的: 压缩指定目录为zip文件 | ##@参数说明:dirname为指定的目录,zipfilename为压缩后的zip文件路径 | ##@返回值:无 | ##@函数逻辑: """ filelist = []
#!/usr/bin/env python #coding=utf-8 from Tkinter import * import time import random class App: def __init__(self
python统计cpu的利用率 #-*-coding=utf-8-*- import win32pdh import time # Counter paths PROCESSOR_PERCENT =
SimpleXMLRPCServer import xmlrpclib def python_logo(): handle = open("python_logo.jpg",'rb') return xmlrpclib
#!/usr/bin/python # -*- coding: iso-8859-1 -*- import ConfigParser # Open a configuration file config
from PILimport Image, ImageDraw, ImageFont import random def generate_authenticode(): letters = random.sample('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',4) width = 100 height = 4
from PIL import Image def change_image_size(image_path,size=(600,600)): img = Image.open(image_path) img = img.resize(size) #img.thumbnail(size,Image.ANTIALIAS) //缩放 img.save('./result.jpg') change_im
__author__ = 'Administrator' #/usr/env/bin python ''' this is document ''' class Event(object): ''' 事件初始化的一个方式
#coding=utf-8 import os import urllib import httplib import requests import json def get_page_content(baseUrl, path, params, headers, type = 'get'): try: httpClient = httplib.HTTPConnection(baseUrl, 8
python的urllib2包的timeout异常需要通过socket.timeout异常来捕获,如下实例: import urllib2 import socket class MyException(Exception):
import smtplib import getpass msg = MIMEText('这是一封来自python的纯文本邮件','plain','utf-8') from_addr = '发件人邮箱地址' password
# class Critter(object): '''A virtual pet''' def __init__(self): print 'A new critter has been born!' def talk(self): print '\nHi. I'm an instance of class Critter.' # main crit1 = Critter() crit2 = C
class Super: def method(self): print 'in Super.method' def delegate(self): self.action() class Inheritor(Super): pass class Replacer(Super): def method(self): print 'in Replacer.method' class Extender