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
隊列為空返回True否則返回False full(): 隊列已滿返回True,負責返回False #!/usr/bin/python import Queue import threading import time exitFlag
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