Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1757443
  • 博文数量: 335
  • 博客积分: 4690
  • 博客等级: 上校
  • 技术积分: 4341
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-08 21:38
个人简介

无聊之人--除了技术,还是技术,你懂得

文章分类

全部博文(335)

文章存档

2016年(29)

2015年(18)

2014年(7)

2013年(86)

2012年(90)

2011年(105)

分类: Python/Ruby

2016-06-25 17:09:31

flask环境搭建完成以后,就要开始flask开发了。为了学以致用,参考网上推荐的STEP BY STEP教程:,进行,整个过程,大约持续了2周,基本上每天1-2的速度,敲代码,测试效果。虽然作者很用心的进行了翻译,但是还是推荐英文原版:http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world。这篇教程的特点是大而全,随之带来的问题,博而不专,杂而不精,对所有模块缺乏深度。对于初学者,可能很有成就感,但是一旦遇到问题,调试起来比较痛苦。当然粘贴代码的风险可能较小,但是那样自己动手动能力短时间很难得到迅速的提升。

这里根据自己手动敲代码遇到的一些问题,同时【编写高质量代码:改善Python程序的91个建议】进行了部分整理。如有不当之处,欢迎指正。

 1 理解CGI,FASTCGI,WSGI的大体概念。大体了解

CGI标准,它提供了WEB通信的标准,由于其实现方式为one request one process方式,导致其高并发发现存在严重的瓶颈

FASTCGI通过事件驱动或是多线程、多进程的方式实现了池的概念,实现了server与应用程序的解耦

而WSGI 通过定义接口规范,进一步实现了WEB SERVER与应用程序之间的解耦和。

1 强烈推荐使用virtualenv进行环境搭建,pip 包管理工具的使用

点击(此处)折叠或打开

  1. pip list
  2. pip show
  3. pip install
  4. pip freeze
  5. pip download
  6. pip search
  7. pip wheel


2 字符串的处理。

字符串的格式化还是推荐使用FORMAT函数

占位符的格式化

点击(此处)折叠或打开

  1. >>> a=1.034
  2. >>> "value of {0} after value format is:{1:<8.4f}".format(a,a)
  3. 'value of 1.034 after value format is:1.0340 '
  4. >>> "value of {0} after value format is:{1:^8.4f}".format(a,a)
  5. 'value of 1.034 after value format is: 1.0340 '
  6. >>> "value of {0} after value format is:{1:>8.4f}".format(a,a)
  7. 'value of 1.034 after value format is: 1.0340'


多行字符串的编写,通常我们熟悉使用docstring,,但是还有更巧妙的trick


点击(此处)折叠或打开

  1. >>> s1=('select '
  2. ... ' abc '
  3. ... ' from '
  4. ... ' dual ')
  5. >>> s2='''
  6. ... select
  7. ... abc
  8. ... from
  9. ... dual
  10. ... '''
  11. >>> s2
  12. '\nselect \nabc\nfrom \ndual\n'
  13. >>> s1
  14. 'select abc from dual '

相比较而言,S1更符合我们的理解,S2便于书写。这里利用技巧就是python遇到未闭合的小括号时,会自动将多行代码拼接为一行和把相邻的两个字符串自动拼接在一起的特性。别问我出处,我也不知道。

3 包的引入,管理,与使用

本质上每一个python文件都是一个模块,同时还含有__init__.py文件,这样就构成了一个python。init在包被导入的使用执行。

点击(此处)折叠或打开

  1. H:\webabc\testpkg 的目录
  2. 2016/06/25 15:36 91 mod1.py
  3. 2016/06/25 15:31 90 mod2.py
  4. 2016/06/25 15:59 90 __init__.py
  5. code:
  6. testpkg/__init__.py
  7. print "file is {0},__name__ is:{1} ".format(__file__,__name__)
  8.  H:\webabc\test.py
  9. import testpkg
  10. print "file is {0},__name__ is:{1} ".format(__file__,__name__)
  11. (venv) H:\webabc>python test.py
  12. file is H:\webabc\testpkg\__init__.pyc,__name__ is:testpkg
  13. file is test.py,__name__ is:__main__
  14. 说明__INIT__在被导入地时候被执行
  15. >>> dir()
  16. ['__builtins__', '__doc__', '__name__', '__package__', 'testpkg']
  17. 说明包中的模块并没有被导入
  18. >>> testpkg.mod1
  19. Traceback (most recent call last):
  20.   File "", line 1, in <module>
  21. AttributeError: 'module' object has no attribute 'mod1'
  22. 手动导入mod1,包中的资源可以访问,即早执行导入动作的时候,module注册到了包中
  23. >>> from testpkg import mod1
  24. file is testpkg\mod1.pyc,__name__ is:testpkg.mod1
  25. >>> dir()
  26. ['__builtins__', '__doc__', '__name__', '__package__', 'mod1', 'testpkg']
  27. >>> testpkg.mod1.module_name
  28. 'module1 one'
  29. 效果和下面一样
  30. >>> from testpkg import mod1
  31. file is testpkg\__init__.pyc,__name__ is:testpkg
  32. file is testpkg\mod1.pyc,__name__ is:testpkg.mod1
  33. >>> dir()
  34. ['__builtins__', '__doc__', '__name__', '__package__', 'mod1']

  35. 注意二者之间的差别:
  36. >>> from testpkg import *
  37. file is testpkg\__init__.pyc,__name__ is:testpkg
  38. >>> dir()
  39. ['__builtins__', '__doc__', '__name__', '__package__']
  40. >>> import testpkg
  41. >>> dir()
  42. ['__builtins__', '__doc__', '__name__', '__package__', 'testpkg']
  43. import * 啥也没有干,
  44. import xx导入了对应的package
  45. __all__=[] 控制了可以导入module的清单
  46. testpkg/__init__.py
  47. __all__=['mod1','mod2']
  48. >>> import testpkg
  49. file is testpkg\__init__.py,__name__ is:testpkg
  50. >>> dir(testpkg)
  51. ['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__
  52. _path__']
  53. >>> testpkg.__all__
  54. ['mod1', 'mod2']
  55. 上面说到__INIT__在导入包的过程中执行,__all__可以对导入的模块进行控制。当我们偷懒import *
  56. 发现包中的模块并没有被导入,由此我们想到可以在__init__中执行包的导入
  57. testpkg/__init__.py
  58. import mod1
  59. >>> import testpkg
  60. file is testpkg\__init__.py,__name__ is:testpkg
  61. file is testpkg\mod1.pyc,__name__ is:testpkg.mod1
  62. >>> dir(testpkg)
  63. ['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '
  64. mod1']
  65. >>> dir()
  66. ['__builtins__', '__doc__', '__name__', '__package__', 'testpkg']
  67. >>> dir(testpkg.mod1)
  68. ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'module_name'
  69. ]

需要了解python为了合理的组织包资源,设计了__INIT__,__ALL__,以及通过控制__INIT__来达到导入module的目的。

同时不推荐使用IMPORT *

4  flask的大体框架:

H:\WEBABC\FLASK
|   app.db
|   babel.cfg
|   config.py
|   config.pyc
|   db_create.py
|   db_downgrade.py
|   db_migrate.py
|   delete.py
|   insert.py
|   myapp.py
|   run.py
|   test.db
|   tests.py
|  
+---app
|   |   forms.py
|   |   forms.pyc
|   |   models.py
|   |   models.pyc
|   |   momentjs.py
|   |   momentjs.pyc
|   |   views-chaper4.py
|   |   views-T1.py
|   |   views.py
|   |   views.pyc
|   |   __init__.py
|   |   __init__.pyc
|   |  
|   +---static
|   |   +---css
|   |   |       bootstrap-responsive.css
|   |   |       bootstrap-responsive.min.css
|   |   |       bootstrap.css
|   |   |       bootstrap.min.css
|   |   |      
|   |   +---img
|   |   |       glyphicons-halflings-white.png
|   |   |       glyphicons-halflings.png
|   |   |      
|   |   \---js
|   |           bootstrap.js
|   |           bootstrap.min.js
|   |           moment.min.js
|   |          
|   +---templates
|   |       404.html
|   |       505.html
|   |       base-chapter4.html
|   |       base.html
|   |       edit.html
|   |       index.html
|   |       login.html
|   |       post.html
|   |       search_results.html
|   |       user.html
|   |      
|   \---views
+---db_repository
|   |   manage.py
|   |   migrate.cfg
|   |   README
|   |   __init__.py
|   |   __init__.pyc
|   |  
|   \---versions
|           001_migration.py
|           001_migration.pyc
|           002_migration.py
|           002_migration.pyc
|           003_migration.py
|           003_migration.pyc
|           __init__.py
|           __init__.pyc
|          
+---search.db
|   \---Post
|           MAIN_6srgjvvmhe4gvw3z.seg
|           MAIN_ebdgmzwjvvyhzel7.seg
|           MAIN_qgap332avjvc5um7.seg

5 debug工具的使用

S/N/C/bt/l/w/u/的使用

6 FLASK的模块,这里重点看一下SQLALCHEMY模块


点击(此处)折叠或打开

  1. >>> from flask.ext.sqlalchemy import SQLAlchemy
  2. >>> from flask import Flask
  3. >>> flaskmidware=Flask('test.py')
  4. >>> db=SQLAlchemy(flaskmidware)
  5. >>> db.get_app()
  6. <Flask 'test.py'>



>>> print flaskmidware.__doc__
The flask object implements a WSGI application and acts as the central
    object.  It is passed the name of the module or package of the
    application.  Once it is created it will act as a central registry for
    the view functions, the URL rules, template configuration and much more.

    The name of the package is used to resolve resources from inside the
    package or the folder the module is contained in depending on if the
    package parameter resolves to an actual python package (a folder with
    an `__init__.py` file inside) or a standard module (just a `.py` file).

阅读(2528) | 评论(0) | 转发(0) |
0

上一篇:WEB项目-FLASK开发手记-环境搭建

下一篇:没有了

给主人留下些什么吧!~~