Python 网页编程- Pyramid 安装测试
http://docs.pylonsproject.org/projects/pyramid/en/1.4-branch/narr/install.html
是我在csdn的博客:http://blog.csdn.net/spaceship20008/article/details/8767884
放在cnblogs做備份
按照介紹操作。
我用的是mint13, python 3.2.3版本。
使用的是virtualenv 開發工具
?
在一個虛擬的python環境下開發web app
這樣很不錯。請按照其步驟來。
在python3.2中,有一些東西需要記住:
4.6.2. Old String Formatting Operations
關于這個的解釋地址:http://docs.python.org/3.2/library/stdtypes.html#old-string-formatting-operationsNote
?The formatting operations described here are modelled on C’s printf() syntax. They only support formatting of certain builtin types. The use of a binary operator means that care may be needed in order to format tuples and dictionaries correctly. As the new?String Formatting?syntax is more flexible and handles tuples and dictionaries naturally, it is recommended for new code. However, there are no current plans to deprecate printf-style formatting.
String objects have one unique built-in operation: the?%?operator (modulo). This is also known as the string?formatting?or?interpolationoperator. Given?format?%?values?(where?format?is a string),?%?conversion specifications in?format?are replaced with zero or more elements of?values. The effect is similar to the using?sprintf()?in the C language.
If?format?requires a single argument,?values?may be a single non-tuple object.?[5]?Otherwise,?values?must be a tuple with exactly the number of items specified by the format string, or a single mapping object (for example, a dictionary).
A conversion specifier contains two or more characters and has the following components, which must occur in this order:
When the right argument is a dictionary (or other mapping type), then the formats in the string?must?include a parenthesised mapping key into that dictionary inserted immediately after the?'%'?character. The mapping key selects the value to be formatted from the mapping. For example:
這個東西在地一個helloworld歷程里面非常重要
看看為什么是%,意思是取得的后面字典的相應index的值。
>>> >>> print('%(language)s has %(number)03d quote types.' % ... {'language': "Python", "number": 2}) Python has 002 quote types.In this case no?*?specifiers may occur in a format (since they require a sequential parameter list).
The conversion flag characters are:
| '#' | The value conversion will use the “alternate form” (where defined below). |
| '0' | The conversion will be zero padded for numeric values. |
| '-' | The converted value is left adjusted (overrides the?'0'?conversion if both are given). |
| '?' | (a space) A blank should be left before a positive number (or empty string) produced by a signed conversion. |
| '+' | A sign character ('+'?or?'-') will precede the conversion (overrides a “space” flag). |
A length modifier (h,?l, or?L) may be present, but is ignored as it is not necessary for Python – so e.g.?%ld?is identical to?%d.
The conversion types are:
| 'd' | Signed integer decimal. | ? |
| 'i' | Signed integer decimal. | ? |
| 'o' | Signed octal value. | (1) |
| 'u' | Obsolete type – it is identical to?'d'. | (7) |
| 'x' | Signed hexadecimal (lowercase). | (2) |
| 'X' | Signed hexadecimal (uppercase). | (2) |
| 'e' | Floating point exponential format (lowercase). | (3) |
| 'E' | Floating point exponential format (uppercase). | (3) |
| 'f' | Floating point decimal format. | (3) |
| 'F' | Floating point decimal format. | (3) |
| 'g' | Floating point format. Uses lowercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. | (4) |
| 'G' | Floating point format. Uses uppercase exponential format if exponent is less than -4 or not less than precision, decimal format otherwise. | (4) |
| 'c' | Single character (accepts integer or single character string). | ? |
| 'r' | String (converts any Python object using?repr()). | (5) |
| 's' | String (converts any Python object using?str()). | (5) |
| 'a' | String (converts any Python object using?ascii()). | (5) |
| '%' | No argument is converted, results in a?'%'?character in the result. | ? |
Python3.2 --- Print函數用法
1. 輸出字符串
>>> strHello = 'Hello World' >>> print (strHello) Hello World2. 格式化輸出整數
支持參數格式化,與C語言的printf類似
>>> strHello = "the length of (%s) is %d" %('Hello World',len('Hello World')) >>> print (strHello) the length of (Hello World) is 113. 格式化輸出16進制,十進制,八進制整數
#%x --- hex 十六進制 #%d --- dec 十進制 #%o --- oct 八進制>>> nHex = 0xFF >>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex)) nHex = ff,nDec = 255,nOct = 3774.格式化輸出浮點數(float)
import math >>> print('PI=%f'%math.pi) PI=3.141593 >>> print ("PI = %10.3f" % math.pi) PI =????? 3.142 >>> print ("PI = %-10.3f" % math.pi) PI = 3.142???? >>> print ("PI = %06d" % int(math.pi)) PI = 0000035. 格式化輸出浮點數(float)?
>>> precise = 3 >>> print ("%.3s " % ("python")) pyt >>> precise = 4 >>> print ("%.*s" % (4,"python")) pyth >>> print ("%10.3s " % ("python"))pyt6.輸出列表(List)
輸出列表
>>> lst = [1,2,3,4,'python'] >>> print (lst) [1, 2, 3, 4, 'python'] 輸出字典 >>> d = {1:'A',2:'B',3:'C',4:'D'}>>> print(d)
{1: 'A', 2: 'B', 3: 'C', 4: 'D'}
7. 自動換行
print 會自動在行末加上回車,如果不需回車,只需在print語句的結尾添加一個逗號”,“,就可以改變它的行為。
>>> for i in range(0,6):print (i,) ??? 0 1 2 3 4 5或直接使用下面的函數進行輸出:
>>> import sys >>> sys.stdout.write('Hello World') Hello WorldHello World
Here’s one of the very simplest?Pyramid?applications:
| 123456789 10 11 12 13 14 15 16 | from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Responsedef hello_world(request):return Response('Hello %(name)s!' % request.matchdict)if __name__ == '__main__':config = Configurator()config.add_route('hello', '/hello/{name}')config.add_view(hello_world, route_name='hello')app = config.make_wsgi_app()server = make_server('0.0.0.0', 8080, app)server.serve_forever() |
?
When this code is inserted into a Python script named?helloworld.py?and executed by a Python interpreter which has the?Pyramidsoftware installed, an HTTP server is started on TCP port 8080.
On UNIX:
$ /path/to/your/virtualenv/bin/python helloworld.pyOn Windows:
C:\> \path\to\your\virtualenv\Scripts\python.exe helloworld.py?通過以上代碼,我們可以看到:
這是在virtualenv下面的環境,裝載的pyramid。
進入./bin/python3
可以查看是否能載入import pyramid
在本機python3編譯器上直接使用import pyramid,發現沒有這個模塊。因為是只在虛擬環境python下裝的。沒有在實際運行環境中裝。
下面,在任務欄里面輸入
http://localhost:8080/hello/world
瀏覽器里面就輸出
Hello world!
如果是
http://localhost:8080/hello/China
就是
Hello China!
?
效果如下
再來看看代碼:
from wsgiref.simple_server import make_server from pyramid.config import Configurator from pyramid.response import Responsedef hello_world(request):return Response('Hello %(name)s!' % request.matchdict) #這里面是將 后面的一個字典{name:value} 按照索引name反給%(name)s處。 %s是字符串的意思if __name__ == '__main__':config = Configurator() #創建一個Configurator 實例,config.add_route('hello', '/hello/{name}') #config.add_view(hello_world, route_name='hello') #當route_name是hello的時候,保證hello_world是callable的。http://localhost:8080/hello #保證在目錄http://localhost:8080/hello 下運行這個hello_world 函數 #這里當route_name='hello'下傳來一個request的時候,config.add_view會把這個request傳入hello_world函數,然后hello_world就執行了app = config.make_wsgi_app() #對于這個實例,創造一個wsgi協議的appserver = make_server('0.0.0.0', 8080, app) #創造一個關于這個app的一個serverserver.serve_forever() #確定這個server的運行狀態,一直運行Adding Configuration?
| 1 2 | config.add_route('hello', '/hello/{name}')config.add_view(hello_world, route_name='hello') |
First line above calls the?pyramid.config.Configurator.add_route()?method, which registers a?route?to match any URL path that begins with?/hello/?followed by a string.
The second line,?config.add_view(hello_world,?route_name='hello'), registers the?hello_world?function as a?view callable?and makes sure that it will be called when the?hello?route is matched.
分享到:?轉載于:https://www.cnblogs.com/spaceship9/archive/2013/04/08/3006930.html
總結
以上是生活随笔為你收集整理的Python 网页编程- Pyramid 安装测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视频专辑:Servlet视频教程
- 下一篇: python识别文字软件_使用Pytho