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

FlagMeaning
'#'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:

ConversionMeaningNotes
'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)

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/433724.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

qt能使用logback_X04Logback的配置

现如今,基于Java的网站开发明显过于复杂,远超实际工作需要。在Java领域中,大部分的网站开发框架也没有严格的遵循“可重用”原则。动态框架如Rails,Django和TurboGears等的出现,为Java网站开发提供了新思路&#xff0c…

A20 配置

TN92屏的显示配置: screen0_output_type1; screen0_output_mode5; lcd_x800; lcd_y480; lcd_swap0; lcd_dclk_freq33; lcd_hbp46; lcd_ht1055; lcd_vbp23; lcd_vt1050; lcd_lvds_ch1; lcd_lvds_mode0; lcd_lvds_bitwidth0; lcd_lvds_io_cross0; lcd_if0; N101BCG-L…

excel 2007 vba与宏完全剖析_Excel怎么保护自己的劳动成果?强制用户启用宏,再加上这一步...

知识改变命运,科技成就未来。当Excel工作簿中含有VBA代码时,用户在使用时需要启用宏,否则工作簿的某些功能就会失效。或者是编辑的VBA代码含有定期删除指令,为了保证工作簿的安全性,和防止他人禁用宏造成知识产权法受到…

Java学习笔记—Lambda表达式

1、Lambda表达式 Lambda表达式是Java8的新特性。 组成Lambda表达式的三个要素:形式参数,箭头操作符,代码块 Lambda表达式的格式:(形式参数) -> {代码块} //如:(int a, int b) -> {return a b;}形式参数&…

旅途的意义- 献给二十五岁

献给25岁你看过了许多美景你看过了许多美女你迷失在都市里每一分钟的光阴你品尝过夜的都市你看过飘着雨雪的各地你渐渐明白了书本里那些曾经不屑的道理却也在青春的激情里多出了那份胆怯的心情你看到了老板坐在办公室飘窗前,悠然的抽着烟你看到了以前的同学娶妻生子…

Asterisk标准通道变量

在asterisk中,定义了许多变量,或是有些变量能够被其读取。下面给出了它们的列表。在每一个application的帮助文档中,你可以获得更多的信息。所有这些变量都是大写的。 被*标记的变量是内建函数,不能在拨号方案中被设置&#xff0…

angular4输入有效性_Angular 2 用户输入

Angular 2 用户输入用户点击链接、按下按钮或者输入文字时,这些用户的交互行为都会触发 DOM 事件。本章中,我们将学习如何使用 Angular 事件绑定语法来绑定这些事件。以下Gif图演示了该实例的操作:源代码可以在文章末尾下载。绑定到用户输入事…

用python画国旗的程序_用Python的Turtle模块绘制五星红旗

Turtle官方文档 turtle的基本操作 # 初始化屏幕 window turtle.Screen() # 新建turtle对象实例 import turtle aTurtle turtle.Turtle() # 海龟设置 aTurtle.hideturtle() # 隐藏箭头 aTurtle.speed(10) # 设置速度 # 前进后退,左转右转 aTurtle.fd(100) # 前进10…

Java学习笔记——函数式接口

一、函数式接口概述 函数式接口:有且仅有一个抽象方法的接口。 Java中的函数式编程体现就是Lambda表达式,所以函数式接口就是可以适用于 Lambda表达式使用的接口。只有确保接口中有且仅有一个抽象方法,Java中的 Lambda表达式才能顺利地进行推…

windows快捷键

F1 显示当前程序或者windows的帮助内容。 F2 当你选中一个文件的话,这意味着“重命名” F3 当你在桌面上的时候是打开“查找:所有文件” 对话框 F10或ALT 激活当前程序的菜单栏 windows键或CTRLESC 打开开始菜单 CTRLALTDELETE 在win9x中打开关闭程序对话…

最简单的拨号方案

使用模式匹配和通道变量可以构建一个最简单的最通用的拨号方案。 如下: exten > _XXXX,1,Dial(SIP/${EXTEN}) exten > _XXXX,2,Hangup() 模式匹配规则: 模式总是用一个下划线 _开始,它告诉 Asterisk 要做模式匹配,这不是…

tortoisegit图标消失_TortoiseGit文件夹和文件图标不显示解决方法

今天下载一个demo导入之后发现一个问题,提示导入的R包只有系统默认的 没有项目的,可以看下图 这种情况出现呢不多,但是出现了我就记录下,这个先看看R文件是不是还在 点击Gen包 查看 看看R文件是不是还在,打开一看果然不…

丰田pcs可以关闭吗_丰田新款卡罗拉变化这么大 让老车主陷入沉思

【太平洋汽车网 导购频道】小胖是一名95后的汽车编辑,年纪轻轻又从事汽车编辑这一岗位,大家可能会觉得他肯定是一位充满热血、喜欢驾驶、热爱汽车的年轻人,那如果我告诉你小胖的座驾是一辆老卡罗拉(询底价|查参配),你还会觉得小胖…

Java学习笔记——Stream流

一、Stream流的生成方式 1、collection集合可以用默认方法stream生成流。 如 ArrayList<String> list new ArrayList<String>();list.stream().forEach(); 2、Map集合间接生成 3、数组通过Stream接口的静态方法of(T... values)生成流。二、Stream流的中间操作方法…

Excel VBA遍历文件

休息日&#xff0c;无聊的上Excel Home看看有啥东东可学习&#xff0c;有啥问题能帮帮忙。看到很多帖子都是求助遍历特定文件夹下文件的实现方法。有朋友说03版Excel有FileSearch对象可以遍历文件夹下文件&#xff0c;07版中没有了FileSearch对象&#xff0c;不知如何遍历文件。…

C++中的explicit关键字

在C程序中很少有人去使用explicit关键字&#xff0c;不可否认&#xff0c;在平时的实践中确实很少能用的上。再说C的功能强大&#xff0c;往往一个问题可以利用好几种C特性去解决。但稍微留心一下就会发现现有的MFC库或者C标准库中的相关类声明中explicit出现的频率是很高的。了…

pline加点lisp_用Autolisp 在AutoCAD中实现多种曲线的绘制

用Autolisp 在AutoCAD中实现多种曲线的绘制一、引言&#xff1a;AutoCAD自1982年由Autodesk公司推出以来&#xff0c;经历了20年的发展更新&#xff0c;目前&#xff0c;已深入到包括机械、建筑、服装、航天航空、地质气象等等的众多领域中。AutoCAD已成为众多工程设计人员的首…

python从小白到大牛pdf 下载 资源共享_Kotlin从小白到大牛 (关东升著) 中文pdf高清版[12MB]...

本书是一本Kotlin语言学习立体教程&#xff0c;主要内容包括&#xff1a;Kotlin语法基础、Kotlin编码规范、数据类型、字符串、运算符、程序流程控制、函数、面向对象基础、继承与多态、抽象类与接口、高阶函数、Lambda表达式、数组、集合、函数式编程API、异常处理、线程、协程…

MySQL——基本配置

一、新建配置文件 在MySQL的安装目录下D:\Mysql\mysql-8.0.28-winx64\bin中新建一个文本文件&#xff0c;文件内容如下&#xff1a; [mysql] default-character-setutf8[mysqld] character-set-serverutf8 default-storage-engineINNODB sql_modeSTRICT_TRANS_TABLES,NO_ZERO_…

iphone win7无法识别_win7系统电脑不能识别iphone苹果设备的解决方法

win7系统使用久了&#xff0c;好多网友反馈说win7系统电脑不能识别iphone苹果设备的问题&#xff0c;非常不方便。有什么办法可以永久解决win7系统电脑不能识别iphone苹果设备的问题&#xff0c;面对win7系统电脑不能识别iphone苹果设备故障问题&#xff0c;我们只需要1)你的苹…