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-operations

Note

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:

  1. The '%' character, which marks the start of the specifier.
  2. Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)).
  3. Conversion flags (optional), which affect the result of some conversion types.
  4. Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.
  5. Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual precision is read from the next element of the tuple in values, and the value to convert comes after the precision.
  6. Length modifier (optional).
  7. Conversion type.

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:

Flag Meaning
'#' 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 (hl, 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:

Conversion Meaning Notes
'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 World

2. 格式化输出整数

支持参数格式化,与C语言的printf类似

>>> strHello = "the length of (%s) is %d" %('Hello World',len('Hello World'))
>>> print (strHello)
the length of (Hello World) is 11

3. 格式化输出16进制,十进制,八进制整数

#%x --- hex 十六进制
#%d --- dec 十进制
#%o --- oct 八进制>>> nHex = 0xFF
>>> print("nHex = %x,nDec = %d,nOct = %o" %(nHex,nHex,nHex))
nHex = ff,nDec = 255,nOct = 377

4.格式化输出浮点数(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 = 000003

5. 格式化输出浮点数(float)

>>> precise = 3
>>> print ("%.3s " % ("python"))
pyt
>>> precise = 4
>>> print ("%.*s" % (4,"python"))
pyth
>>> print ("%10.3s " % ("python"))pyt

6.输出列表(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 World

Hello 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()

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.py

On 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 安装测试相关推荐

  1. python 网页编程_通过Python编程检索网页

    python 网页编程 The internet and the World Wide Web (WWW), is probably the most prominent source of info ...

  2. Python gui编程pyQt5安装步骤

    Python gui编程pyQt5安装步骤 =============================== -m PyQt5.uic.pyuic  $FileName$ -o $FileNameWit ...

  3. Python:Python语言编程软件安装的几大姿势之详细攻略

    Python:Python语言编程软件安装的几大姿势之详细攻略 目录 Python软件安装的几大姿势 Python原生安装 Python3.6安装 Python流行安装--完美搭配Pycharm

  4. python网页编程测试_李亚涛:python编写友情链接检测工具

    原标题:李亚涛:python编写友情链接检测工具 友情链接是网站外链的非常重要的来源,作为一个网站运营推广人员,需要定期对网站的友链进行更新与检查,如果有人下掉你的链接,这样你可以及时的检测并清除掉. ...

  5. Python魔术世界 1 如何使用Visual Studio在WIN10中一键安装Python3入门编程环境并测试Django...

    本文通过VS安装Python和Django的环境,创建了一个Web程序,前后5分钟的操作,让你快速入门Python的编程世界,各种Python和Django的概念会在实战中给你娓娓道来. Django ...

  6. python编程需要什么软件-《》 学习python编程需要安装哪些软件?

    python安装教程有没有?最好是视频的 廖雪峰的python教程这里有教程,还是不错的. 学Python要安装哪些软件? 一. 安装python 1. 从python下载相应的python安装包,打 ...

  7. python网页运行环境_Python小牛叔Web开发1:安装Visual Studio Code配置Python运行环境...

    本系列是Python小牛叔Web开发系列,以Django为框架介绍如何使用Python来开发Web应用,本教程适合对于Web开发有基本知识的入门者. 1.安装Visual Studio Code编辑器 ...

  8. 编程软件python-零基础学Python编程需要安装什么软件?

    前言 Python现在非常火,语法简单而且功能强大,很多同学都想学Python!所以小的给各位看官们准备了高价值Python学习视频教程及相关电子版书籍,都放在了文章结尾,欢迎前来领取! 今天想要跟大 ...

  9. linux环境Mechanize安装,Python 爬虫:Mechanize 安装与测试

    原标题:Python 爬虫:Mechanize 安装与测试 Mechanize是Python的一个模块,用于模拟浏览器.Mechanize的易用性和实用性比较平衡,功能强大而又简单易用. 1.安装Me ...

最新文章

  1. python映射类型-Python中的映射数据类型 dict
  2. 集成学习-Bagging集成学习算法随机森林(Random Forest)
  3. java中的运算符_java中的运算符
  4. vue实现时间选择器,精确到秒
  5. Boost:字符串分割Split的测试程序
  6. Mysql连接显示1130_***远程连接MYSQL提示1130 - Host is not allowed to connect to this MySQL server...
  7. python开发笔记软件_图解Python编程神器Jupyter Notebook
  8. 《Python入门到精通》Python基础语法
  9. php api接口怎么写,php如何写api接口?
  10. ural 1024. Permutations
  11. cad卸载工具_Adobe软件卸载与常见问题解决方案
  12. WIN7,WIN10,WIN11怎么查看电脑操作系统位数
  13. 如何修复硬盘的分区表
  14. 实习僧的字体加密破解
  15. pma连接,报错10061
  16. 用密钥激活win10显示无法连接到你的组织的激活服务器0xc004f074
  17. 甲骨文中间件与主数据管理平台
  18. 拓扑容差如何修改_如何在CAD中通过设置容差参数对多段线进行自动修复重构
  19. 身份证号码验证(转)
  20. OsWorkFlow工作流简介

热门文章

  1. 8号团队-团队任务三:每日立会(2018-11-27)
  2. Angular路由——子路由
  3. Redis在windows下安装过程
  4. 【网络流24题】餐巾计划问题(最小费用最大流)
  5. HOJ 2678 Stars
  6. hihoCoder 1116 计算 (线段树)
  7. 对lIKE语句的优化
  8. C#开发终端式短信的原理和方法
  9. Microsoft Updater Application Block 1.5.3 服务器端manifest文件设计 [翻译]
  10. Alibaba 开源工具 Arthas 使用