python获取当前运行程序所在的路径import os.pathprint os.path.realpath(__file__)……继续阅读 » 6年前 (2019-07-11) 2472浏览 2862个赞
python判断当前用户是否是root,这段代码只能在unix核心的系统运行(unix,linux,mac等),不能在windows下运行import osif os.geteuid() != 0: print "This program must be run as root. Aborting." sys.ex……继续阅读 » 6年前 (2019-07-11) 2446浏览 2046个赞
这是一个python通过urllib直接登陆网站,并处理网站的session和cookieimport cookielib, urllib, urllib2login = 'ismellbacon123@yahoo.com'password = 'login'# Enable cookie support ……继续阅读 » 6年前 (2019-07-11) 3340浏览 208个赞
这段代码不用google api,而是直接抓取Google的搜索结果页,找到需要的链接后存储在links.txt, Google的搜索页面可能会变化,这段代码也需要修改import re,urllib,urllib2class GoogleHarvester: re_links = re.compile(r'<a class……继续阅读 » 6年前 (2019-07-11) 2809浏览 799个赞
python并不提供抽象类的写法,但是如果你非要严格实现抽象类,可以使用下面的代码,实际上就是不允许用户直接调用父类的方法,如果用户调用了,则给出错误提示。class myAbstractClass: def __init__(self): if self.__class__ is myAbstractClass: ……继续阅读 » 6年前 (2019-07-11) 1481浏览 1350个赞
wxpython用来创建窗口应用程序,但有时候我们希望能在控制台输出一些异常或者错误提示,这段代码能够帮助你实现import sysSTDERR = sys.stderr # Keep stderr because wxPyhon will redirect it.import wx[...your wxPython program goes ……继续阅读 » 6年前 (2019-07-11) 2704浏览 310个赞
python 读取tar文件#!/usr/bin/env python# [代码名字: Open a tar file]# [代码分类: tarfile]# [代码描述: Open's a tar file and list the entries]# [代码作者: Tim Voet <tim.voet@gmail.com>……继续阅读 » 6年前 (2019-07-11) 2257浏览 1891个赞
用Python实现一个简单的算术游戏来源:http://blog.csdn.net/buaa_shang/article/details/8315829#!/usr/bin/env pythonfrom operator import add, sub from random import randint, choiceops = {'……继续阅读 » 6年前 (2019-07-11) 1282浏览 1558个赞
python可以非常容易的捕获程序的输出信息,同时也可以传递给命令行程序信息#捕获程序输出信息#!/usr/bin/pythonimport subprocessmyprocess = subprocess.Popen(['net','statistics','workstation']……继续阅读 » 6年前 (2019-07-11) 1995浏览 379个赞
我们知道python中有个range函数用来产生一个范围内的数字数组,但是浮点数没有,我们来定义一个#python中的range函数支持步进,如下:>>> print range(2,15,3)[2, 5, 8, 11, 14]#但是浮点数不支持range函数,自己定义一个类似的def floatrange(start,sto……继续阅读 » 6年前 (2019-07-11) 1878浏览 2435个赞
python编写简单抽奖系统#!/usr/bin/env python#coding=utf-8from Tkinter import *import timeimport randomclass App: def __init__(self,master): frame = Frame(master) frame.pack() ……继续阅读 » 6年前 (2019-07-11) 1892浏览 1332个赞
通过python的feedparse模块可以很容易的分析出rss的内容#!/usr/bin/env python# [代码名字: Parse an RSS feed]# [代码分类: feedparser] # [代码描述: Parse and iterate over the items in an RSS feed]# [代码作者: Ti……继续阅读 » 6年前 (2019-07-11) 1268浏览 1625个赞
python通过给定的url抓取远程xml文件def get_xml(self, remote_addr): remote_file = urllib.urlopen(remote_addr) remote_data = remote_file.read() remote_file.close() self.REQUEST.RESPON……继续阅读 » 6年前 (2019-07-11) 2246浏览 2686个赞
python判断字符串包含的方法,所有语言中,python的这种判断方法可以说是最直观简单的。#!/usr/bin/env python## [代码名字: String contains string test]# [代码分类: Python Core]# [代码描述: Test if a string contains another str……继续阅读 » 6年前 (2019-07-11) 3081浏览 745个赞
C语言多种方法求解字符串编辑距离问题编辑距离:通过插入、删除、替换一个字符(和交换相邻字符)的操作,使得字符串A和字符串B相同,而最少的操作次数就是编辑距离。如字符串abcd和aca的距离是2/* 递归搜索 */int calDistance1(char *ptrX, int xbeg, int xend, char *ptrY, int ybeg, i……继续阅读 » 6年前 (2019-07-11) 1341浏览 2495个赞
javascript为数字添加逗号千分符function CommaFormatted(amount) { var delimiter = ","; // replace comma if desired var a = amount.split('.',2) var d = a[1]; var i = pa……继续阅读 » 6年前 (2019-07-11) 1605浏览 2356个赞
php查询mysql数据库prepare语句用法,使用prepare可以优化数据库查询$mysql = new mysqli('localhost', 'root', 'root', 'databaseName') or die('There was a proble……继续阅读 » 6年前 (2019-07-11) 2143浏览 2791个赞
调用JavaScript数组元素的多个属性或者方法,应该先把数组元素放到变量里,然后依靠变量调用,不要每次都直接使用数组元素调用//直接使用数组元素调用,慢myArray[myIndex].myMethod1();myArray[myIndex].myMethod2();myArray[myIndex].my_variable;//放到变量里再通过……继续阅读 » 6年前 (2019-07-11) 1507浏览 918个赞
通过SSH在本地控制远程服务器执行命令,如果每次都要登录到远程服务器真的好麻烦,起始通过SSH就可以远程执行在本地使用 ssh $RemoteNode 可以在执行远程机器上的命令,例如 ssh user@node ls /local 会执行远程机器上的 ls /local 命令,如果想在远程机器上连续执行多条命令,可以用单引号或者双引号将这些命令……继续阅读 » 6年前 (2019-06-27) 2712浏览 863个赞
执行Go代码时可以附加参数,包括要执行的命令和给命令的参数package mainimport ( "os" "os/exec" "fmt" "flag" "strings")func main() { command := f……继续阅读 » 6年前 (2019-05-24) 2220浏览 538个赞
滚动条滚动到页面底部附近时动态加载内容显示var loading = false;$(window).scroll(function(){ if((($(window).scrollTop()+$(window).height())+250)>=$(document).height()){ if(loading == fal……继续阅读 » 6年前 (2019-02-27) 1478浏览 506个赞
本代码演示了python如何接受命令行参数##Chris Hall 9/13/11def main(argv): sys.path.append('models') #Usage will display when the user asks for help (-h) or when something ……继续阅读 » 6年前 (2018-10-24) 2628浏览 2739个赞
Python 获得当前程序所在路径import osprint(os.path.abspath('.'))……继续阅读 » 6年前 (2018-10-24) 1712浏览 2461个赞
py-leveldb 是 Google 的 K/V 数据库 LevelDB 的 Python 客户端开发包。import leveldbdb = leveldb.LevelDB('./db')# single putdb.Put('hello', 'world')print db.Ge……继续阅读 » 6年前 (2018-10-24) 2500浏览 1019个赞
python没有像C++那样的函数指针,但是python提供了更为简洁方便的函数赋值方式def foo(): print "foo" f = foo f() ……继续阅读 » 6年前 (2018-10-24) 1965浏览 2970个赞
Python 实现的插入法排序代码#-*- coding: utf-8 -*-# the insertation sort# 2012-07-29 noona = [3, 2, 4, 1, 5, 4, 2, 3, 6, 5, 7, 4, 1, 0]for j in range(1, len(a)): i = 0 while a[……继续阅读 » 6年前 (2018-10-24) 1467浏览 2672个赞
python修改文件名、目录名os.rename("/path/to/old/file", "/path/to/new/file") ……继续阅读 » 6年前 (2018-10-24) 1779浏览 1379个赞
下面列举了TextCtrl的可用样式'''wx.TE_CENTER:控件中的文本居中。wx.TE_LEFT:控件中的文本左对齐。默认行为。wx.TE_NOHIDESEL:文本始终高亮显示,只适用于Windows。wx.TE_PASSWORD:不显示所键入的文本,代替以星号显示。180 / 565wx.TE_PR……继续阅读 » 6年前 (2018-10-24) 3061浏览 1162个赞
wxpython中的所有文本框都是TextCtrl,不同的形式的文本框通过样式来实现,下面创建了一个密码输入框pwdText = wx.TextCtrl(panel, -1, ”password”, size=(175, -1), style=wx.TE_PASSWORD)……继续阅读 » 6年前 (2018-10-24) 3103浏览 707个赞
wxpython的文本框可以在创建时设置样式,下面的例子给文本框设置为制度、富文本编辑、不自动换行等样式style = (wx.TE_MULTILINE | wx.TE_READONLY | wx.TE_RICH2 | wx.TE_DONTWRAP)wx.TextCtrl.__init__(self, parent, id, style=style)……继续阅读 » 6年前 (2018-10-24) 2084浏览 2409个赞
python在控制台输入密码但是不显示import console;namespace console{ //控制台读取密码,并显示星号 getPassword = function(){ var tstr = {}; var input = kbRead(true); while( i……继续阅读 » 6年前 (2018-10-24) 2216浏览 779个赞
运行这段代码,将生成一个简单的左右结果的框架,非常好用,需要wx.aui模块# -*- coding: cp936 -*-# 2010-04-20 18:40 中国广州天河# 如何实现动态布局# source:http://stackoverflow.com/questions/523363/how-do-i-layout-a-3-pane-wi……继续阅读 » 6年前 (2018-10-24) 1915浏览 2333个赞
wxpython自带一个列表选择对话框SingleChoiceDialog,可以用一个数组定义要选择的元素列表dlg = wx.SingleChoiceDialog(None,’What version of Python are you using?’, ’Single Choice’,[‘1.5.2’, ’2.0’, ’2.1.3’, ’2.2’,……继续阅读 » 6年前 (2018-10-24) 2344浏览 182个赞
python实现的代码行数统计代码''' Author: liupengfei Function: count lines of code in a folder iterativelyShell-format: cmd [dir] Attention: default file encode ……继续阅读 » 6年前 (2018-10-24) 2983浏览 1969个赞
用go计算一个人的年龄,生肖,星座,输入参数为用户的出生年月日(类型string ,格式”2006-05-04″).package main import ( "fmt" "time") func GetTimeFromStrDate(date string) ……继续阅读 » 6年前 (2018-10-24) 1661浏览 981个赞
JavaScript生成随机颜色var randomColor = Math.floor(Math.random()*16777215).toString(16); ……继续阅读 » 6年前 (2018-10-24) 2843浏览 2906个赞
怎么使用JavaScript让Textarea支持tab按键HTMLTextAreaElement.prototype.getCaretPosition = function () { //return the caret position of the textarea return this.selectionStart;};HTML……继续阅读 » 7年前 (2018-04-08) 3172浏览 1958个赞
python过滤字符串中不属于指定集合的字符# -*- coding: utf-8 -*-import string# 生成所有字符的可复用的字符串,它还可以作为# 一个翻译表,指明“无须翻译”allchars = string.maketrans('', '')def makefilter(ke……继续阅读 » 7年前 (2018-01-22) 2873浏览 2340个赞
用Python提取url链接中的域名与端口import urllib proto, rest = urllib.splittype("http://www.75271.com/11/12.htm") host, rest = urllib.splithost(rest) print host host, port……继续阅读 » 7年前 (2018-01-21) 2417浏览 791个赞
冒泡法排序Go语言版func BubbleSort(nums []int) { unsorted := true for unsorted { unsorted = false for i := len(nums) - 1; i > 0; i-- { if nums[i……继续阅读 » 7年前 (2018-01-21) 2597浏览 2748个赞
go语言实现的简单http服务代码package mainimport ( "flag" "log" "net/http" "text/template")var addr = flag.String("addr&q……继续阅读 » 7年前 (2018-01-20) 2386浏览 1722个赞
go语言编写的猜数字的小游戏代码随机生成一个数字,输入一个数字看是否匹对,匹配则结速,反之提示是大了还是小了转自:http://www.waylau.compackage mainimport ( "bufio" "fmt" "math/rand" "os"……继续阅读 » 7年前 (2018-01-20) 2954浏览 1342个赞
python自动修改本机网关的代码#!/usr/bin/python#auto change gateway Created By mickelfengimport osimport random,reg='gateway 192.168.1.'rand=random.randint(1,3)test='……继续阅读 » 7年前 (2018-01-20) 1558浏览 1661个赞
Go语言官方带了一个工具叫cgo,可以很方便的在Go语言代码中内嵌C代码或做C和Go代码的集成。下面是一段简单的在Go中内嵌C的实验代码:package main/*#include #include void say_hello() { printf("Hello World!\n");}*/……继续阅读 » 7年前 (2018-01-20) 2809浏览 856个赞
凡是线性回溯都可以归结为右递归的形式,也即是二叉树,因此对于只要求一个解的问题,采用右递归实现的程序要比回溯法要优美的多。def Test(queen,n): '''这个就不用说了吧,就是检验第n(下标,0-7)行皇后的位置是否合理''' q=queen[n] for i in xrange……继续阅读 » 7年前 (2018-01-20) 1258浏览 1943个赞