Time of Update: 2015-06-29
標籤:上一篇表建好後開始對資料進行CURD操作dos輸入:>>>python manage.py shell以下的命令都是在shell中測試(C)增:1 >>>import myLesson import Blog2 >>>b = Blog(name = ‘Frist Blog‘, tagline = ‘All‘)3 >>>b.save()4 >>>b.id5
Time of Update: 2015-06-29
標籤: python中的名稱空間是名稱(標識符)到對象的映射。具體來說,python為模組、函數、類、對象儲存一個字典(__dict__),裡面就是重名稱到對象的映射。可以參看下面python程式的輸出:print(‘globals:‘)print(globals().keys())print(‘‘)x = 1print(‘globals after definition of x:‘)print(globals().keys())print(‘‘)if x == 1: y =
Time of Update: 2015-06-29
標籤: 最近在使用自己研究效能測試工具的時候想到,使用python向伺服器不斷髮送資料以作為並發測試。大概情況如下:#coding=utf-8import urllib2import urllibimport cookielibimport osimport threadingimport timeclass HB:def add_cookie_login(self,username): self.user = username cookiejar =
Time of Update: 2015-06-29
標籤:自己最近學習了Regex,整理了一些關於Python的優秀博文,大家可以拿來參考學習:1. google 搜尋引擎排名第一的 ”PythonRegex“http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html但作為初學者來說,此文比較硬骨頭,不建議。2. 深入淺出學習Python
Time of Update: 2015-06-29
標籤:在C++的類中,有兩種函數:普通成員函數和靜態成員函數,差別是成員函數通過類執行個體調用,而靜態成員函數通過類名調用。本質上,成員函數在調用的時候會預設把this指標作為第一個參數傳入,而靜態成員函數不需要綁定this指標。在python的類設計中,可以說將C++的這種隱式行為顯式的表達出來了。class Test(object): def __init__(self): pass def func1(self): pass
Time of Update: 2015-06-29
標籤:#coding=utf-8class Employee: ‘所有員工的基類‘ empCount = 0 def __init__(self, name, salary): self.name = name self.salary = salary Employee.empCount += 1 def displayCount(self): print ("Total Employee %d" % Employee.empCount) def
Time of Update: 2015-06-29
標籤:寫這篇日誌的目的,只是記錄一下學習Python的一些問題,收穫。今天,也是新入職第一天,也記錄下日常生活的點滴。看看我能堅持多久吧。 早上,起床,洗漱,急忙趕往公司,結果還是遲到了,但是老闆不在,然後就是裝機,裝機的過程中知道了公司早上上班時間很靈活,從8點半到9點半都可以,只是影響你下班的時間,心中暗爽。但是遲到一次扣50,還是有點嚇人的。然後開始一天的扯淡。下午5點半準時溜。 回來開始安裝Python(本來昨晚上就在下的,可是弄了半天,
Time of Update: 2015-06-29
標籤:1. python中的selfpython中的self就相當於C++中的this指標也就是指向對象本身的指標self.name = name 就是當前對象的成員變數name賦值為name。 2.python的self.__class__表示當前執行個體對象的類.例如:if hasattr(self.__class__, ‘fields‘) and len(self.__class__.fields) > 0: 3.
Time of Update: 2015-06-29
標籤:python 登陸 login#!/bin/bash/env python while True: user_name=raw_input(‘what is your name?‘).strip() if len(user_name) == 0: print "please input your user_name.if you not,please register!"
Time of Update: 2015-06-29
標籤:python開閉原則開閉原則(OCP)是物件導向設計中“可複用設計”的基石,是物件導向設計中最重要的原則之一,其它很多的設計原則都是實現開閉原則的一種手段。 1988年,勃蘭特·梅耶(Bertrand Meyer)在他的著作《物件導向軟體構造(Object Oriented Software Construction)》中提出了開閉原則,它的原文是這樣:“Software entities should be open for extension,but closed for
Time of Update: 2015-06-29
標籤:1. Python的字典的items(), keys(), values()都返回一個list>>> dict = { 1 : 2, ‘a‘ : ‘b‘, ‘hello‘ : ‘world‘ } >>> dict.values() [‘b‘, 2, ‘world‘] >>> dict.keys() [‘a‘, 1, ‘hello‘] >>> dict.items() [(‘a‘, ‘b‘), (1, 2),
Time of Update: 2015-06-29
標籤:應用情境假設有這樣一個字典結構test_dict = {‘a‘:{‘b‘:{‘c‘:1}},‘d‘:2},test_dict其實可以看作是一種樹狀結構,其中每個葉子節點深度不一定相同,如果我們希望輸出根節點到所有葉子節點的路徑,也就是a->b->c->1;d->2,該如何解決?代碼#encoding=utf-8import sysdef recurPrintPath(dic): for key in dic.keys(): print key
Time of Update: 2015-06-29
標籤:Lambda運算式是Python中一類特殊的定義函數的形式,使用它可以定義一個匿名函數。Python的Lambda運算式的函數體只能有唯一的一條語句,也就是傳回值運算式語句。文法:lambda 參數:傳回值參數個數不限使用lambda運算式可以簡化函數定義過程,也不必考慮函數命名的問題,增強代碼可讀性,一箭三雕過濾器filter文法 filter(function
Time of Update: 2015-06-29
標籤: 1.方式1__file__ = open(r‘log.txt‘, ‘a‘)print >>__file__, "hello, world!"__file__.close() 2.方式2__stdout__ = sys.stdoutsys.stdout = open(r‘log.txt‘, ‘a‘)print "hello, world!"sys.stdout.close()sys.stdout = __stdout__ 3.方式3#-*-
Time of Update: 2015-06-29
標籤:描述Python join() 方法用於將序列中的元素以指定的字元串連產生一個新的字串。文法join()方法文法:str.join(sequence)參數sequence -- 要串連的元素序列。傳回值返回通過指定字元串連序列中元素後產生的新字串。執行個體以下執行個體展示了join()的使用方法:#!/usr/bin/pythonstr = "-";seq = ("a", "b", "c"); # 字串序列print str.join( seq
Time of Update: 2015-06-29
標籤:一、前奏:熟悉Python記憶體管理在Python中,變數在第一次賦值時自動聲明,在建立---也就是賦值的時候,解譯器會根據文法和右側的運算元來決定新對象的類型。引用計數器:一個內部跟蹤變數引用計數:每一個對象各有多少個引用當對象被建立並(將其引用)賦值給變數時,該對象的引用計數就被設定為 1>>> x = 3.14語句
Time of Update: 2015-06-29
標籤:一 、xml.dom 解析XML的API描述minidom.parse(filename) 載入讀取XML檔案doc.documentElement 擷取XML文檔對象node.getAttribute(AttributeName) 擷取XML節點屬性值node.getElementsByTagName(TagName) 擷取XML節點對象集合node.childNodes
Time of Update: 2015-06-29
標籤:python 異常處理 引入異常處理機制,使得啟動並執行程式發生錯誤時,不至於崩潰。常見格式:try: command 1except: command 2當command 1 執行出錯時,就會執行command 2。command 2
Time of Update: 2015-06-29
標籤: 對於Python中靜態、類、抽象方法的使用,我是一直很迷糊的。最近看到一篇技術文章對這方面解釋的很好,在此翻譯一下,加深印象,也為有需要的同學提供一個方便。 Python中方法是如何工作的: 方法即函數,作為一個類的屬性儲存區。你能像如下申明和訪問一個函數:>>> class
Time of Update: 2015-06-29
標籤:簡單的閉包的栗子:def counter(statr_at = 0):count = 1def incr():nonlocal count #注意由於count類型為immutable,所以需要聲明清楚在此局部範圍內引用的是外部範圍那個countcount += 1return countreturn incr>>> count = counter(4)>>> count()2>>> count()3>>>