这段代码相对比较简单,通过time.sleep每隔一秒钟让计数器递减即可#!/usr/bin/env pythonimport timeimport syscount = 0while (count < 10): ncount = 10 - count sys.stdout.write("\r%d " ……继续阅读 » 6年前 (2021-03-05) 2542浏览 2290个赞
python通过Tkinter库实现的一个简单的文本编辑器代码## {{{ http://code.activestate.com/recipes/578568/ (r1)from Tkinter import * from tkSimpleDialog import askstringfrom tkFileDialog import ask……继续阅读 » 6年前 (2021-03-05) 3050浏览 1300个赞
对于中文的参数如果不进行编码的话,python的urllib2直接处理会报错,我们可以先将中文转换成utf-8编码,然后使用urllib2.quote方法对参数进行url编码后传递。content = u'你好 75271.com'content = content.encode('utf-8')conten……继续阅读 » 6年前 (2021-03-05) 2332浏览 1584个赞
这段代码非常简单,从命令行输入参数a和b,输出axb的值import sysdef main(argv): if len(argv) != 2: sys.exit('Usage: simple_multi.py <a> <b>') a = int(sys.argv[1]) b = int……继续阅读 » 6年前 (2021-03-05) 2327浏览 387个赞
python捕获和抛出异常的范例代码try:  #试运行python代码except Exception as e:   #捕获异常 注意这里用到as关键字,在3.1版本中使用as来得到对象……继续阅读 » 6年前 (2021-03-05) 2789浏览 2275个赞
这段代码定义了一个python装饰器,通过此装饰器可以用来检查指定函数的参数是否是指定的类型,在定义函数时加入此装饰器可以非常清晰的检测函数参数的类型,非常方便,75271.com强烈推荐。def accepts(exception,**types): def check_accepts(f): assert len(types……继续阅读 » 6年前 (2021-03-05) 2638浏览 1731个赞
简单的代码来创建和使用公共/私人密钥对# coding=utf-8from __future__ import division, absolute_import, print_functionfrom base64 import b64encodefrom fractions import gcdfrom random import rand……继续阅读 » 6年前 (2021-03-05) 2741浏览 561个赞
python开发简单socket程序在两台电脑之间传输消息,分为客户端和服务端,分别在两台电脑上运行后即可进行简单的消息传输,也可以在一台电脑上测试,设置两个不同的端口即可。# Save as server.py 服务端代码 - 75271.com# Message Receiverimport osfrom socket import *ho……继续阅读 » 6年前 (2021-03-05) 1555浏览 424个赞
xapian通过python实现的一个简单的范围查询的例子#!/usr/bin/env python## Simple command-line ValueRangeProcessor example. Takes as an argument# the value number to apply numeric ranges to.## C……继续阅读 » 6年前 (2021-03-05) 2842浏览 1059个赞
这段python代码通过smtp发送邮件,系需要修改相应的邮件服务器地址、用户名和密码,代码里面使用的是Gmail#account setup,75271.com 提醒您修改成自己的用户名、密码和服务器地址username = '***';password = '***';server = 'sm……继续阅读 » 6年前 (2021-03-05) 2588浏览 2292个赞
这个配方展示了如何创建一个可以提供PDF阅读的一个基本的Python WSGI服务器。需要用到 my xtopdf toolkit 和 Reportlab toolkit, v1.21# basic_wsgi_pdf_server.py# Basic WSGI PDF server in Python.# Adapted from:# htt……继续阅读 » 6年前 (2021-03-05) 2334浏览 1417个赞
python生成不重复的随机数代码下面的python代码生了1000-2000之间的不重复随机数import randomb_list = range(1000,2000)result = random.sample(b_list, 3)print result 输出……继续阅读 » 6年前 (2021-03-05) 2125浏览 2229个赞
python通过shutil实现快速文件拷贝,shutil使用起来非常方便,可以通过pip install shutil安装from shutil import *from glob import globprint 'BEFORE:', glob('shutil_copyfile.*')copyfile……继续阅读 » 6年前 (2021-03-05) 1668浏览 2267个赞
xapian通过python实现的简单排序代码James Aylett: Xapian examples: simplesorter#!/usr/bin/env python## Simple command-line sorter example; takes as an argument a# comma-separated list o……继续阅读 » 6年前 (2021-03-05) 2844浏览 631个赞
python数组过滤方法,这段代码可以按照指定的条件过滤数组内的元素,返回过滤后的数组li = ["a", "mpilgrim", "foo", "b", "c", "b", "d", "d"]……继续阅读 » 6年前 (2021-03-05) 3068浏览 920个赞
python计算一个序列的平局值def average(seq, total=0.0): num = 0 for item in seq: total += item num += 1 return total / num ……继续阅读 » 6年前 (2021-03-05) 2506浏览 593个赞
通过随机数和md5算法生成一个随机图片名字,在上传图片时重命名图片名可以用到$image_name = md5(uniqid(rand())).".jpg";……继续阅读 » 6年前 (2021-01-15) 2673浏览 0评论2802个赞
一个基本的php查询mysql数据库的函数if (!function_exists('mysql_search')) { function mysql_search($table, $columns, $query = '', $options = Array()) { if (empt……继续阅读 » 6年前 (2021-01-15) 2458浏览 0评论2340个赞
简单的把开始时间放在页面头部,结束时间放在页面尾部,计算页面加载时间$start = time(); // put a long operation in heresleep(2); $diff = time() - $start; print "This page needed $diff seconds to load ……继续阅读 » 6年前 (2021-01-15) 1903浏览 0评论1502个赞
此代码包含了php通过print输出不同格式的字符串的方法// report all errors:error_reporting(E_ALL); // the full path to the current fileprint __FILE__; // print the current lineprint __LINE__; /……继续阅读 » 6年前 (2021-01-15) 2349浏览 0评论2035个赞
本代码演示了php中如何通过glob查找指定的文件$dir = './'; foreach(glob($dir.'*.txt') as $file) { print $file . "\n";} /* returns: ./dummy.txt./foo.txt./i……继续阅读 » 6年前 (2021-01-15) 1651浏览 0评论723个赞
一个php查找文件的函数,指定要查找的目录和文件扩展名function get_files_by_ext($path, $ext){ $files = array(); if (is_dir($path)){ $handle = opendir($path); while ($file = re……继续阅读 » 6年前 (2021-01-15) 2573浏览 0评论1566个赞
php读取和写入tab分割的文件,包含两个独立的函数,一个读,一个写,例如cvs文件等//// save an array as tab seperated text file// function write_tabbed_file($filepath, $array, $save_keys=false){ $content = ……继续阅读 » 6年前 (2021-01-15) 3473浏览 0评论160个赞
本代码演示了php中如何通过min和max函数输出数组的最大值和最小值/* find the highest value */ print max(100, 70, 101, 50);// returns --> 101 $array = array(100, 701, 2, 4, 202);print max($array);//……继续阅读 » 6年前 (2021-01-15) 2898浏览 0评论2878个赞
本代码演示了php中如何通过cURL函数抓取和下载网页function curl_download($Url){ // is cURL installed yet? if (!function_exists('curl_init')){ die('Sorry cURL is not in……继续阅读 » 6年前 (2021-01-15) 1762浏览 0评论632个赞
php计算连接的mysql数据库的大小,用MB,KB或者GB的格式返回function CalcFullDatabaseSize($database, $db) { $tables = mysql_list_tables($database, $db); if (!$tables) { return -1; } $tab……继续阅读 » 6年前 (2021-01-15) 3188浏览 0评论111个赞
本范例演示了php中如何把一个HEX格式(如:#FF00FF)的颜色代码转换成RGB格式的颜色代码(如:Array(179,218,245))function Hex2RGB($color){ $color = str_replace('#', '', $color); if (strlen($c……继续阅读 » 6年前 (2021-01-15) 3334浏览 0评论2112个赞
给定开始和结束值,再给定一个步进值,就可以生成一个等差数组function array_range($from, $to, $step=1){ $array = array(); for ($x=$from; $x <= $to; $x += $step){ $array[] = $x; } ……继续阅读 » 6年前 (2021-01-15) 2993浏览 0评论2182个赞
以一种可读性比较好的方式显示已经过去多长时间,比如:距离现在10秒,距离现在1天等等function time_is_older_than($t, $check_time){ $t = strtolower($t); $time_type = substr(preg_replace('/[^a-z]/', ……继续阅读 » 6年前 (2021-01-15) 2197浏览 0评论772个赞
php获取指定路径的最后一个参数,可能是文件名或者最后一层文件夹function path_get_last_arg($path){ $path = str_replace('\\', '/', $path); $path = preg_replace('/\/+$/', &……继续阅读 » 6年前 (2021-01-15) 2639浏览 0评论2426个赞
通过预定义一些单词,让php随机从这些单词中选择进行组合生成密码function random_readable_pwd($length=10){ // the wordlist from which the password gets generated // (change them as you like) $wor……继续阅读 » 6年前 (2021-01-15) 1934浏览 0评论2488个赞
php限制文件下载速度,这个方案可能有一些缺陷,但可以参考一下// local file that should be send to the client$local_file = 'test-file.zip'; // filename that the user gets as default$download_fil……继续阅读 » 6年前 (2021-01-15) 2860浏览 0评论2768个赞
本代码演示了如何从php数组中随机选择一个元素显示function random_array_element(&$a){ mt_srand((double)microtime()*1000000); // get all array keys: $k = array_keys($a); // fi……继续阅读 » 6年前 (2021-01-15) 2443浏览 0评论936个赞
把两个字符串进行分割合并,例如str1=aaaa,str2=bbbb,合并后生成abababab/** * Merges two strings in a way that a pattern like ABABAB will be * the result. * * @param string $str1 String A……继续阅读 » 6年前 (2021-01-15) 1713浏览 0评论2902个赞
本代码演示了如何通过php的 similar_text函数比较两个字符串的相似性$word2compare = "stupid"; $words = array( 'stupid', 'stu and pid', 'hello', ……继续阅读 » 6年前 (2021-01-15) 3294浏览 0评论2022个赞
例如:将5M或者400K这样的字符串转换成字节数字显示function StringSizeToBytes($Size){ $Unit = strtolower($Size); $Unit = preg_replace('/[^a-z]/', '', $Unit); $Value ……继续阅读 » 6年前 (2021-01-15) 2919浏览 0评论141个赞
这是一个php专用的类,用于产生随机整数,随机字符串,随机颜色等,同时你可以对这个类进行扩展,产生自己的随机数据/// <summary> /// Helper class for generating random values /// </summary> public static class RandomHelpe……继续阅读 » 6年前 (2021-01-15) 3415浏览 0评论2174个赞
本范例演示了php中如何通过array_walk调试数组,输出一个可读性良好的格式function debug_val($val, $key='', $depth=0) { if (is_array($val)){ // call this function again with the "su……继续阅读 » 6年前 (2021-01-15) 2521浏览 0评论449个赞
本范例演示了php如何链接微软的sql server数据库并做简单的查询/*** Connect to database:*/ // Connect to the database (host, username, password)$con = mssql_connect('localhost','admin&……继续阅读 » 6年前 (2021-01-15) 1627浏览 0评论2347个赞
这个php函数可以把指定的颜色变得更深一些function ColorDarken($color, $dif=20){ $color = str_replace('#', '', $color); if (strlen($color) != 6){ return '000000……继续阅读 » 6年前 (2021-01-15) 2085浏览 0评论135个赞
去除php数组里的重复的值function remove_duplicated_values($arr){ $_a = array(); while(list($key,$val) = each($arr)){ $_a[$val] = 1; } return array_keys($_a);}//演示……继续阅读 » 6年前 (2021-01-15) 3331浏览 0评论2389个赞
php把文件大小转换成易于阅读的格式,如GB,MB,KB// A much better and accurate version can be found// in Aidan's PHP Repository: // http://aidanlister.com/repos/v/function.size_readable.php ……继续阅读 » 6年前 (2021-01-15) 3165浏览 0评论1629个赞
16进制数据和字符串相互转换的函数function String2Hex($string){ $hex=''; for ($i=0; $i < strlen($string); $i++){ $hex .= dechex(ord($string[$i])); } return $……继续阅读 » 6年前 (2021-01-15) 2082浏览 0评论2662个赞
获得php类包含的所有元素以key-value的形式输出class MyTestClass{ const TESTVAR1 = 1001; const TESTVAR2 = 1002; const TESTSTR1 = 'hello';} $rc = new ReflectionClass('M……继续阅读 » 6年前 (2021-01-15) 3053浏览 0评论2478个赞
php把数组的key值恢复成类似于0,1,2,3,4,5…这样的数字序列function restore_array($arr){ if (!is_array($arr)){ return $arr; } $c = 0; $new = array(); while (list($key, $value) = each……继续阅读 » 6年前 (2021-01-15) 2908浏览 0评论2456个赞