Time of Update: 2016-06-06
面對對象(oop)中的對象,是一個非常重要的知識點,我們可以把它簡單看做是資料以及由存取、操作這些資料的方法所組成的一個集合。我們在學習函數(function)之後,知道了如果重用代碼,那為什麼還要用類來取代函數呢?類有這樣一些的優點1)
Time of Update: 2016-06-06
代碼如下:#!/usr/bin/python#coding:utf-8#write:JACK#info:ftp exampleimport ftplib, socket, osfrom time import sleep, ctimedef LoginFtp(self): ftps = ftplib.FTP() ftps.connect(self.host,self.port)
Time of Update: 2016-06-06
編寫tasks.py 代碼如下:from celery import Celeryfrom tornado.httpclient import HTTPClientapp = Celery('tasks')app.config_from_object('celeryconfig')@app.taskdef get_html(url): http_client = HTTPClient() try: response =
Time of Update: 2016-06-06
代碼如下:import timedef block(file,size=65536): while True: nb = file.read(size) if not nb: break yield nbdef getLineCount(filename): with open(filename,"r",encoding="utf-8") as f: return sum(line.count("\n")
Time of Update: 2016-06-06
mdb格式檔案可以通過mdbtools工具將內中包含的每張表匯出到csv格式檔案。由於access資料庫和postgresQL資料庫格式上會存在不通性,所以使用python的檔案處理,將所得csv檔案修改成正確、能識別的格式。匯入指令碼說明(此指令碼運行於linux):1.apt-get install mdbtools,安裝mdbtools工具2.將mdb 檔案拷貝到linux虛擬機器中,修改指令碼中mdb檔案目錄‘dir'3.修改伺服器及資料庫配置4.執行指令碼 代碼如下:# -*-
Time of Update: 2016-06-06
測試組態檔案test.conf內容如下: 代碼如下:[first]w = 2v: 3c =11-3[second]sw=4test: hello測試組態檔案中有兩個地區,first和second,另外故意添加一些空格、換行。下面解析: 代碼如下:>>> import ConfigParser>>> conf=ConfigParser.ConfigParser()>>> conf.read('test.conf')['test.conf']>>> conf.sections()
Time of Update: 2016-06-06
基數排序法又稱桶子法(bucket sort)或bin sort,顧名思義,它是透過索引值的部份資訊,將要排序的元素分配至某些"桶"中,藉以達到排序的作用,基數排序法是屬於穩定性的排序,其時間複雜度為O (nlog(r)m),其中r為所採取的基數,而m為堆數,在某些時候,基數排序法的效率高於其它的比較性排序法。 代碼如下:# -*- coding: utf-8 -*-def _counting_sort(A, i): """計數排序,以i位進行排序,以適用於基數排序。 Args:
Time of Update: 2016-06-06
python演算法學習之計數排序執行個體 代碼如下:# -*- coding: utf-8 -*-def _counting_sort(A, B, k): """計數排序,偽碼如下: COUNTING-SORT(A, B, k) 1 for i ← 0 to k // 初始化儲存區的值 2 do C[i] ← 0 3 for j ← 1 to length[A] // 為各值計數 4 do C[A[j]] ← C[A[j]] + 1 5
Time of Update: 2016-06-06
ipad的goodreader對JS檔案支援不太好,雖然可以讀取它但總是無法退出,回不了goodreader的主介面,因此我需要把js檔案批量變成純文字。基於這個目的,我搞了下面一個小應用: 代碼如下:# -*- coding:utf-8 -*-import osdef rename(): path = raw_input("請輸入要處理的檔案夾路徑") print path old_ext = "."+raw_input("請輸入要處理的檔案類型") print
Time of Update: 2016-06-06
代碼如下:from sgmllib import SGMLParserimport urllib2class sgm(SGMLParser): def reset(self): SGMLParser.reset(self) self.srcs=[] self.ISTRUE=True def start_div(self,artts): for k,v in artts: if v=="author":
Time of Update: 2016-06-06
分析數字中經常是3個數字一組,之後跟一個逗號,因此規律為:***,***,***正則式 代碼如下:[a-z]+,[a-z]? 代碼如下:import resen = "abc,123,456,789,mnp"p = re.compile("\d+,\d+?")for com in p.finditer(sen): mm = com.group() print "hi:", mm print "sen_before:", sen sen = sen.replace(mm,
Time of Update: 2016-06-06
Beautiful Photo!: http://www.beautifulphoto.net/ 代碼如下:import urllib2import re_random_url = r'http://www.beautifulphoto.net/plugin/RndArticle/'_img_patt = re.compile(r'')def random(timeout=3, more=False): try: html = urllib2.urlopen(_random_
Time of Update: 2016-06-06
代碼如下:import urllib.requestimport reimport timedef movie(movieTag): tagUrl=urllib.request.urlopen(url) tagUrl_read = tagUrl.read().decode('utf-8') return tagUrl_readdef subject(tagUrl_read): ''' 這裡還存在問題:
Time of Update: 2016-06-06
代碼如下:# -*- coding:utf-8 -*-# python3.3.3import
Time of Update: 2016-06-06
代碼如下:import os ## for os.path.isfile()def dealline(line) : print(line) ## 針對line我可以做很多事情def getfilename() : return input('Please input file name(input exit() for exit):').strip()class more : ## MORE功能 linenum = 0 size = 10 def
Time of Update: 2016-06-06
代碼如下:# -*- coding: utf-8 -*-class Heap(object): @classmethod def parent(cls, i): """父結點下標""" return int((i - 1) >> 1); @classmethod def left(cls, i): """左兒子下標""" return (i @classmethod def right(cls, i):
Time of Update: 2016-06-06
代碼如下:from creepy import Crawlerfrom BeautifulSoup import BeautifulSoupimport urllib2import jsonclass MyCrawler(Crawler): def process_document(self, doc): if doc.status == 200: print '[%d] %s' % (doc.status, doc.url)
Time of Update: 2016-06-06
上個月就打算開發個還算好玩的項目,但是一直沒時間。這篇是此項目用到的一部分,處理好此部分基本還差通訊等方面的了。首先類比滑鼠鍵盤按下釋放的動作,本人利用X11這個庫,所以要瞭解X11編程;其次,本身用c或者c++就可以實現了,但是由於本人是py粉,所以總想把代碼搬進python,所以本人就要實現python模組,本篇用的ctypes,以後會把python的c擴充模組附上來的。 1.X11編程
Time of Update: 2016-06-06
代碼如下:# 比較兩個字串,如果不同返回第一個不相同的位置# 如果相同返回0def cmpstr(str1, str2): col = 0 for c1, c2 in zip(str1, str2): if c1 == c2: col += 1 continue else : break #判斷是怎樣退出迴圈的,還有一種情況是串長度不同 if c1 != c2 or
Time of Update: 2016-06-06
考慮下述Python程式碼片段。對檔案中的資料進行某些操作,然後將結果儲存迴文件中: 代碼如下:with open(filename) as f: input = f.read()output = do_something(input)with open(filename, 'w') as f: