1、python自動連接ssh的代碼
#!/usr/bin/python#-*- coding:utf-8 -*-import sys, time, ostry:import pexpectexcept ImportError:Word:')server.sendline(host[1])server.interact()2、有沒有一個工具可以幫助查找python的bug和進行靜態的代碼分析? 有,PyChecker是一個python代碼的靜態分析工具,它可以幫助查找python代碼的bug, 會對代碼的復雜度和格式提出警告 Pylint是另外一個工具可以進行coding standard檢查。 3、 如何在一個function里面設置一個全局的變量? 解決方法是在function的開始插入一個global聲明: def f() global x 4、Tkinter的ToolTip控件
from Tkinter import *from time import time, localtime, strftimeclass ToolTip( Toplevel ):"""Provides a ToolTip widget for Tkinter.To apply a ToolTip to any Tkinter widget, simply pass the widget to theToolTip constructor""" def __init__( self, wdgt, msg=None, msgFunc=None, delay=1, follow=True ):"""Initialize the ToolTipArguments:wdgt: The widget this ToolTip is assigned tomsg: A static string message assigned to the ToolTipmsgFunc: A function that retrieves a string to use as the ToolTip textdelay: The delay in seconds before the ToolTip appears(may be float)follow: If True, the ToolTip follows motion, otherwise hides"""self.wdgt = wdgtself.parent = self.wdgt.master # The parent of the ToolTip is the parent of the ToolTips widgetToplevel.__init__( self, self.parent, bg='black', padx=1, pady=1 ) # Initalise the Toplevelself.withdraw() # Hide initiallyself.overrideredirect( True ) # The ToolTip Toplevel should have no frame or title barself.msgVar = StringVar() # The msgVar will contain the text displayed by the ToolTip if msg == None: self.msgVar.set( 'No message provided' )else:self.msgVar.set( msg )self.msgFunc = msgFuncself.delay = delayself.follow = followself.visible = 0self.lastMotion = 0Message( self, textvariable=self.msgVar, bg='#FFFFDD',aspect=1000 ).grid() # The test of the ToolTip is displayed in a Message widgetself.wdgt.bind( '<Enter>', self.spawn, '+' ) # Add bindings to the widget. This will NOT override bindings that the widget already hasself.wdgt.bind( '<Leave>', self.hide, '+' )self.wdgt.bind( '<Motion>', self.move, '+' )def spawn( self, event=None ):"""Spawn the ToolTip. This simply makes the ToolTip eligible for display.Usually this is caused by entering the widgetArguments:event: The event that called this funciton"""self.visible = 1self.after( int( self.delay * 1000 ), self.show ) # The after function takes a time argument in milisecondsdef show( self ):"""Displays the ToolTip if the time delay has been long enough"""if self.visible == 1 and time() - self.lastMotion > self.delay:self.visible = 2if self.visible == 2:self.deiconify()def move( self, event ):"""Processes motion within the widget.Arguments:event: The event that called this function"""self.lastMotion = time()if self.follow == False: # If the follow flag is not set, motion within the widget will make the ToolTip dissapearself.withdraw()self.visible = 1self.geometry( '+%i+%i' % ( event.x_root+10, event.y_root+10 ) ) # Offset the ToolTip 10x10 pixes southwest of the pointertry:self.msgVar.set( self.msgFunc() ) # Try to call the message function. Will not change the message if the message function is None or the message function failsexcept:passself.after( int( self.delay * 1000 ), self.show )def hide( self, event=None ):"""Hides the ToolTip. Usually this is caused by leaving the widgetArguments:event: The event that called this function"""self.visible = 0self.withdraw()def xrange2d( n,m ):"""Returns a generator of values in a 2d rangeArguments:n: The number of rows in the 2d rangem: The number of columns in the 2d rangeReturns:A generator of values in a 2d range"""return ( (i,j) for i in xrange(n) for j in xrange(m) )def range2d( n,m ):"""Returns a list of values in a 2d rangeArguments:n: The number of rows in the 2d rangem: The number of columns in the 2d rangeReturns:A list of values in a 2d range"""return [(i,j) for i in range(n) for j in range(m) ]def print_time():"""Prints the current time in the following format:HH:MM:SS.00"""t = time()timeString = 'time='timeString += strftime( '%H:%M:', localtime(t) )timeString += '%.2f' % ( t%60, )return timeStringdef main():root = Tk()btnList = []for (i,j) in range2d( 6, 4 ):text = 'delay=%i/n' % idelay = iif j >= 2:follow=Truetext += '+follow/n'else:follow = Falsetext += '-follow/n'if j % 2 == 0:msg = NonemsgFunc = print_timetext += 'Message Function'else:msg = 'Button at %s' % str( (i,j) )msgFunc = Nonetext += 'Static Message'btnList.append( Button( root, text=text ) )ToolTip( btnList[-1], msg=msg, msgFunc=msgFunc, follow=follow, delay=delay)btnList[-1].grid( row=i, column=j, sticky=N+S+E+W )root.mainloop()if __name__ == '__main__':main()5、介紹一下Python中webbrowser的用法? webbrowser模塊提供了一個高級接口來顯示基于Web的文檔,大部分情況下只需要簡單的調用open()方法。 webbrowser定義了如下的異常 exception webbrowser.Error, 當瀏覽器控件發生錯誤是會拋出這個異常 webbrowser有以下方法: webbrowser.open(url[, new=0[, autoraise=1]]) 這個方法是在默認的瀏覽器中顯示url, 如果new = 0, 那么url會在同一個瀏覽器窗口下打開,如果new = 1, 會打開一個新的窗口,如果new = 2, 會打開一個新的tab, 如果autoraise = true, 窗口會自動增長。 webbrowser.open_new(url) 在默認瀏覽器中打開一個新的窗口來顯示url, 否則,在僅有的瀏覽器窗口中打開url webbrowser.open_new_tab(url) 在默認瀏覽器中當開一個新的tab來顯示url, 否則跟open_new()一樣 webbrowser.get([name]) 根據name返回一個瀏覽器對象,如果name為空,則返回默認的瀏覽器 webbrowser.register(name, construtor[, instance]) 注冊一個名字為name的瀏覽器,如果這個瀏覽器類型被注冊就可以用get()方法來獲取。 6、Python里面search()和match()的區別? match()函數只檢測RE是不是在string的開始位置匹配, search()會掃描整個string查找匹配, 也就是說match()只有在0位置匹配成功的話才有返回,如果不是開始位置匹配成功的話,match()就返回none 例如: print(re.match(‘super’, ‘superstition’).span())會返回(0, 5) 而print(re.match(‘super’, ‘insuperable’))則返回None search()會掃描整個字符串并返回第一個成功的匹配 例如:print(re.search(‘super’, ‘superstition’).span())返回(0, 5) print(re.search(‘super’, ‘insuperable’).span())返回(2, 7)
新聞熱點
疑難解答