随笔分类 -  Python

摘要:从语言的使用上来看,python类似于shell,或者javascript,是个脚本语言,所以纷繁的类就比较乱,没有java那样直观。1. 命名方式可以稍微改善一点。比如所有对象都以类名打头,比如genHtml_obj = genHtml()2. 尽量把一个类写一个文件或者一个模块 阅读全文
posted @ 2013-04-17 13:16 Me.thinking 阅读(178) 评论(0) 推荐(0)
摘要:把自己写的模块加入到python默认就有的搜索路径:在python/lib/python2.5/site-packages目录下建立一个 xxx.pth的文件,写入自己写的模块所在的路径即可sys.path.remove("路径")或者方法一:临时加入模块载入目录:sys.path.append('/storage01')import post_wap=================方法二:(1)主程序与模块程序在同一目录下:如下面程序结构:`-- src |-- mod1.py `-- test1.py 若在程序test1.py中导入模块mod1, 则直 阅读全文
posted @ 2013-04-10 17:37 Me.thinking 阅读(365) 评论(0) 推荐(0)
摘要:Project Overview:This purpose of this project is to write the contents of a csv file to an html file in the form of a table. The first row of the csv file is assumed to be a header row. The table will have predefined css styles applied to it. The styles will customize the header as well as alternate 阅读全文
posted @ 2013-04-07 21:52 Me.thinking 阅读(1137) 评论(0) 推荐(0)
摘要:在 Python 中, 当需要对一个 list 排序时, 一般可以用 list.sort() 或者 sorted(iterable[, cmp[, key[, reverse]]]).其中:cmp(e1, e2) 是带两个参数的比较函数, 返回值: 负数: e1 < e2, 0: e1 == e2, 正数: e1 > e2. 默认为 None, 即用内建的比较函数.key 是带一个参数的函数, 用来为每个元素提取比较值. 默认为 None, 即直接比较每个元素.reverse 是一个布尔值, 表示是否反转比较结果.我以前在做比较复杂的排序时, 喜欢写一个定制的 cmp 函数. 当我 阅读全文
posted @ 2013-04-07 12:43 Me.thinking 阅读(286) 评论(0) 推荐(0)
摘要:python的中文问题一直是困扰新手的头疼问题,Python的发行版至今尚未包括任何中文支持模块。当然,几乎可以确定的是,在将来的版本中,python会彻底解决此问题,不用我们这么麻烦了。 笔者使用的是2.5版本。Python的版本可以通过调用sys模块的sys.version查看。在几个月的学习中,主要遇到以下问题:1. print打印中文的问题:在编辑器中输入一段测试代码:s=’测试’print s运行结果如下:Non-ASCII character '\xb2' in file c:\Documents and Settings\Administrator\桌面\2.py 阅读全文
posted @ 2013-04-06 21:17 Me.thinking 阅读(2944) 评论(0) 推荐(0)
摘要:In this post I’m going to explain how to create a simple Python CGI Server. To illustrate the concepts involved more clearly I will not attempt to make this server extensible, rather I will try to keep the code as simple and clear as possible.To start with we need to create a directory for our serve 阅读全文
posted @ 2013-04-04 19:09 Me.thinking 阅读(1239) 评论(0) 推荐(0)
摘要:在mkdir -p cgi/cgi-bin下面新建time.py#!/usr/bin/env python# -*- coding utf8 -*- import cgitbcgitb.enable()import timeprint "Content-type: text/html" print print time.strftime('%Y-%m-%d %X', time.localtime() )cgi 服务器建立cd cgipython -m CGIHTTPServer到 firefox 中http://127.0.0.1:8000/cgi-bin/ 阅读全文
posted @ 2013-04-04 18:40 Me.thinking 阅读(365) 评论(1) 推荐(0)
摘要:1. 最近有段时间经常用c来模拟面向对象。然后发现Python跟这个很像。this 与self2. 因为实例化对象的时候不声明,所以感觉python使用起来比较乱,这个怎么办?3. 我们至少要学习一门语言,才知道语言里面有些什么东西。所以没有精于一门别的语言的时候,那python就需要一点一滴的学习。 阅读全文
posted @ 2013-04-03 21:46 Me.thinking 阅读(145) 评论(0) 推荐(0)
摘要:1. 作为一种动态语言,随时随地创建和使用变量是Python给我们带来的巨大的便利。但是它也会使得程序不严谨,某些错误只有在运行中才可能出现。所以,使用Python编程的时候,要对类型做到心里有数。这也使得Python的IDE工具无法提供便利的自动完成等功能。2. 编程的时候一定要注意类的命名,很难知道一个类是什么,这点不方便。比如:1 class observeStoReportData:2 def __init__ (self, stoName, stoId, currentPrice, expectBuyPrice, origP, inhouse):3 self.... 阅读全文
posted @ 2013-04-03 09:49 Me.thinking 阅读(277) 评论(0) 推荐(0)
摘要:python中类的声明使用关键词class,可以提供一个可选的父类或者说基类,如果没有合适的基类,那就用object作为基类。定义格式:1 class 类名(object):2 "类的说明文档"3 属性4 初始方法__init__5 其他方法和其他面向对象语言不太一样的地方:属性没有公有和私有的说法。没有构造方法,初始方法只是在实例化时会执行的方法。定义方法时必须带上self参数,类似java里的this。 1 class User(object): 2 "This is user class." 3 4 name = "tom" 5 阅读全文
posted @ 2013-04-02 21:16 Me.thinking 阅读(363) 评论(0) 推荐(0)
摘要:1. 见下面红色字体2. 其他同gdb相同#!/usr/local/bin/pythonimport test_DEBUG = True def main(): if _DEBUG == True: import pdb pdb.set_trace() test.Go()main()>.\main.py(Pdb) 阅读全文
posted @ 2013-03-30 18:36 Me.thinking 阅读(146) 评论(0) 推荐(0)
摘要:某些原因使得项目可能常常会被来回的迁移。如果没有事先规划好这种可能性的话这绝对是一个棘手的问题。Rob Hudson 有一个极好的技巧能够确保你的Django项目在部署过程中能够轻松的来回迁移。仅仅只要编写几行代码在你的配置文件(settings.py)中。1 import os2 BASE_DIR = os.path.dirname(os.path.abspath(__file__))3 4 TEMPLATE_DIRS = (5 BASE_DIR + '/templates',6 ) 阅读全文
posted @ 2013-03-28 21:55 Me.thinking 阅读(1602) 评论(0) 推荐(0)
摘要:版本:1.0版本说明:1) 大部分来源于http://www.cnblogs.com/cacique/archive/2012/09/29/2707976.html2) 自己测试用,使用svn比较方便以下详细步骤1. create foldermysite22. cmd to mysite2django-admin.py startproject blog#F:\tools\Django-1.5\build\scripts-2.7\django-admin.py 是Django 安装路径此时目录结构如下blog/ manage.py blog/ __init__.py settings.py 阅读全文
posted @ 2013-03-27 22:52 Me.thinking 阅读(672) 评论(0) 推荐(0)
摘要: 阅读全文
posted @ 2013-03-27 21:00 Me.thinking 阅读(459) 评论(0) 推荐(0)
摘要:1. 通信: HTTP, URL, 请求和响应2. 数据存储: 数据库3. 表示: 将模板渲染成HTML或其他格式然后把这3部分组合在一起,即WEB框架。比如Django。也就是传说中的MVC。这样的好处是1) 灵活2) 清晰3) 代码重用4) 好替换,比如要替换数据库,替换视图等成功实施MVC架构的关键在于要正确地分割应用程序的不同层次。 阅读全文
posted @ 2013-03-27 20:23 Me.thinking 阅读(170) 评论(0) 推荐(0)