本文執行個體講述了Python實現的最近最少使用演算法。分享給大家供大家參考。具體如下:# lrucache.py -- a simple LRU (Least-Recently-Used) cache class # Copyright 2004 Evan Prodromou # Licensed under the Academic Free License 2.1 # Licensed for ftputil under the revised BSD license # with
本文執行個體講述了Python簡單計算檔案夾大小的方法。分享給大家供大家參考。具體如下:import os, re """查看檔案夾下的所有檔案及檔案夾 join為拼接函數"""def Look_File(path): for root , dirs, files in os.walk(path, True): print root #主目錄 for item in files: #主目錄下的檔案夾 print os.path.join(root, item)""
HTML被直接寫入程式碼在 Python 代碼之中。def current_datetime(request): now = datetime.datetime.now() html = "It is now %s." % now return HttpResponse(html)儘管這種技術便於解釋視圖是如何工作的,但直接將HTML寫入程式碼到你的視圖裡卻並不是一個好主意。 讓我們來看一下為什麼: 對頁面設計進行的任何改變都必須對 Python 代碼進行相應的修改。
開啟current_datetime 視圖。 以下是其內容:from django.http import HttpResponseimport datetimedef current_datetime(request): now = datetime.datetime.now() html = "It is now %s." % now return HttpResponse(html)讓我們用 Django 模板系統來修改該視圖。 第一步,你可能已經想到了要做下面這樣的修改:from
為了減少模板載入調用過程及模板本身的冗餘代碼,Django 提供了一種使用方便且功能強大的 API ,用於從磁碟中載入模板,要使用此模板載入API,首先你必須將模板的儲存位置告訴架構。 設定的儲存檔案就是settings.py。如果你是一步步跟隨我們學習過來的,馬上開啟你的settings.py設定檔,找到TEMPLATE_DIRS這項設定吧。 它的預設設定是一個空元組(tuple),加上一些自動產生的注釋。TEMPLATE_DIRS = ( # Put strings here, like "
本文執行個體講述了python基本進位轉換的方法。分享給大家供大家參考。具體如下:# Parsing string with base into a number is easynum = int(str, radix)# We have to write our own function for outputting to string with arbitrary basedef itoa(num, radix): result = "" while num > 0: result =