代码改变世界

随笔分类 -  python(原创)

python标准库学习4

2011-11-26 12:56 by Rollen Holt, 869 阅读, 收藏, 编辑
摘要: >>> os.environ["HOME"]'C:\\Users\\Administrator'>>> os.getcwd() #获得当前的目录'D:\\new'>>> os.getenv("QTDIR") #获取环境变量的值'D:\\vs2010-qt-src-4.7.4\\qt-src-4.7.4'os.putenv(varname, value) #设置环境变量的值os.mkdir(path[, mode])>>> os.mkdi 阅读全文

os.path学习

2011-11-25 23:48 by Rollen Holt, 1904 阅读, 收藏, 编辑
摘要: #coding=utf-8import osprint os.path.abspath("d:\\new\\test.txt")print os.path.basename("d:\\new\\test.txt")print os.path.dirname("d:\\new\\test.txt") print os.path.exists("d:\\new")print os.path.lexists("d:\\new")print os.path.expanduser("d:\\ne 阅读全文

python内建函数(不完全)

2011-11-25 19:07 by Rollen Holt, 1328 阅读, 收藏, 编辑
摘要: 各位还是参考官方文档吧,我这些是自己感觉重要和常用的abs()all(iterable) 如果迭代序列中所有的元素都为真,或者迭代序列为空的时候返回True。等价于:def all(iterable): for element in iterable: if not element: return False return Trueall(iterable) 如果迭代序列中所有的元素都为真,返回True。等价于def any(iterable): for element in iterable: if element: ... 阅读全文

python异常继承树

2011-11-25 19:05 by Rollen Holt, 1794 阅读, 收藏, 编辑
摘要: BaseException +-- SystemExit +-- KeyboardInterrupt +-- GeneratorExit +-- Exception +-- StopIteration +-- StandardError | +-- BufferError | +-- ArithmeticError | | +-- FloatingPointError | | +-- OverflowError | | +-- ZeroDivisionError | ... 阅读全文

Python中的string模块的学习

2011-11-25 19:04 by Rollen Holt, 30554 阅读, 收藏, 编辑
摘要: 学习资料:http://docs.python.org/library/string.html#string.Formatter感觉学习任何东西,官方的东西总是最好的,呵呵。个人总结(代码为主,相信有python基础的都能看懂):>>> import string>>> string.ascii_letters'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'>>> string.ascii_lowercase'abcdefghijklmnopqrstuvwx 阅读全文

PyQt基本操作

2011-11-16 22:27 by Rollen Holt, 47952 阅读, 收藏, 编辑
摘要: PyQt的简介和基本安装方法读者可以自行google解决。先声明,本文章教基础,参考《征服Python》相关章节。不过不得不说,pyQt的教程真的好少,╮(╯▽╰)╭,悲催,大家有什么好的资料推荐一下,谢谢了。先建立一个基本的界面看看:import sysfrom PyQt4 import QtCore, QtGuiclass MyWindow( QtGui.QMainWindow ): def __init__( self ): QtGui.QMainWindow.__init__( self ) self.setWindowTitle( "PyQt" )... 阅读全文

python核心编程--笔记(不定时跟新)

2011-11-09 23:12 by Rollen Holt, 44480 阅读, 收藏, 编辑
摘要: 的解释器options:1.1 –d 提供调试输出1.2 –O 生成优化的字节码(生成.pyo文件)1.3 –S 不导入site模块以在启动时查找python路径1.4 –v 冗余输出(导入语句详细追踪)1.5 –m mod 将一个模块以脚本形式运行1.6 –Q opt 除法选项(参阅文档)1.7 –c cmd 运行以命令行字符串心事提交的python脚本1.8 file 以给定的文件运行python脚本2 _在解释器中表示最后一个表达式的值.3 print支持类c的printf格式化输出: print “%s is number %d!” % (“python”, 1)4 print的输入内 阅读全文

python多线程学习

2011-08-09 09:17 by Rollen Holt, 5287 阅读, 收藏, 编辑
摘要: 今天在学习尝试学习python多线程的时候,突然发现自己一直对super的用法不是很清楚,所以先总结一些遇到的问题。当我尝试编写下面的代码的时候:class A(): def __init__( self ): print "A"class B( A ): def __init__( self ): super( B, self ).__init__( )# A.__init__( self ) print "B"b = B()出现:super( B, self ).__init__()TypeError: must be type, not class 阅读全文

python标准库学习3

2011-08-08 15:53 by Rollen Holt, 1242 阅读, 收藏, 编辑
摘要: import operatorsequence = 1, 2, 4print "add", "=>", reduce(operator.add, sequence)print "sub", "=>", reduce(operator.sub, sequence)print "mul", "=>", reduce(operator.mul, sequence)print "concat", "=>", operat 阅读全文

python标准库学习2

2011-08-08 13:18 by Rollen Holt, 840 阅读, 收藏, 编辑
摘要: 列出指定目录中的所有文件:import osfor file in os.listdir( "../src" ): print file获得,修改当前的目录import os# where are we?cwd = os.getcwd()print "1", cwd# go downos.chdir( "../" )print "2", os.getcwd()# go back upos.chdir( os.pardir )print "3", os.getcwd()创建目录,删除目录impor 阅读全文

python标准库学习1

2011-08-08 00:26 by Rollen Holt, 1522 阅读, 收藏, 编辑
摘要: 本系列是本人学习python标准库的一些笔记,呵呵,此处由于时间的关系,只给出代码,至于运行结果我没有放上去,大家见谅。因为那个太麻烦了。#-----使用字典或者元祖中的参数调用元素def function( a, b ): print a, bapply( function, ( 1, 2 ) )apply( function, ( 1 , ), {"b":2} ) #注意这里的","apply( function, (), {"a":1, "b":2} )#apply 函数的一个常见用法是把构造函数参数从子类传 阅读全文

windows下面,MySQLdb的安装出错问题以及解决办法

2011-08-06 20:14 by Rollen Holt, 3654 阅读, 收藏, 编辑
摘要: 今天在电脑上安装MySQLdb的时候,出现错误,是什么导入setuptools错误,然后又去下载了那个包,安装之后,还是错误,这次的错误是:[b]C:\Python25\MySQL-python-1.2.3c1>setup.py build[/b]Traceback (most recent call last): File "C:\Python25\MySQL-python-1.2.3c1\setup.py", line 15, in <module> metadata, options = get_config() File "C:\Pyth 阅读全文

python的静态方法

2011-08-05 13:20 by Rollen Holt, 767 阅读, 收藏, 编辑
摘要: staticmethod Found at: __builtin__staticmethod(function) -> method Convert a function to be a static method. A static method does not receive an implicit first argument. To declare a static method, use this idiom: class C: def f(arg1, arg2, ...): ... f = staticmethod(f) It can be called either on 阅读全文

python中的继承和抽象类的实现

2011-08-05 11:31 by Rollen Holt, 2767 阅读, 收藏, 编辑
摘要: #!/usr/local/bin/python# Fig 9.9: fig09_09.py# Creating a class hierarchy with an abstract base class.class Employee: """Abstract base class Employee""" def __init__(self, first, last): """Employee constructor, takes first name and last name. NOTE: Cannot 阅读全文

python实现的列表操作

2011-08-04 19:09 by Rollen Holt, 1656 阅读, 收藏, 编辑
摘要: 纯属练手呵呵。class Node: """Single node in a data structure""" def __init__(self, data): """Node constructor""" self._data = data self._nextNode = None def __str__(self): """Node data representation""" return str(sel 阅读全文

python的图形化界面(1)

2011-08-04 15:37 by Rollen Holt, 10005 阅读, 收藏, 编辑
摘要: 今天学习了python的图形化界面。遇到的问题,就是导入Tklinter类的时候,出现了错误,提示说模块不存在,但是最后在文件的头部添加一行代码之后解决:#! /usr/bin/env python 先创建一个空的界面窗口吧:#! /usr/bin/env python#coding=utf-8from Tkinter import *class LabelDemo( Frame ): """Demonstrate Labels""" def __init__( self ): """Create thr 阅读全文

Python中使用中文

2011-08-01 15:06 by Rollen Holt, 88937 阅读, 收藏, 编辑
摘要: python的中文问题一直是困扰新手的头疼问题,这篇文章将给你详细地讲解一下这方面的知识。当然,几乎可以确定的是,在将来的版本中,python会彻底解决此问题,不用我们这么麻烦了。先来看看python的版本:>>> import sys>>> sys.version'2.5.1 (r251:54863, Apr 18 2007, 08:51:08) [MSC v.1310 32 bit (Intel)]'(一)用记事本创建一个文件ChineseTest.py,默认ANSI:s = "中文"print s测试一下瞧瞧:E:/ 阅读全文