Python2.x
文章平均质量分 62
GhostFromHeaven
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
Python 多线程简单例子
Python代码 import threading import time class MyThread(threading.Thread): def __init__(self, threadnum, max): threading.Thread.__init__(self) s原创 2011-12-04 14:14:26 · 3262 阅读 · 0 评论 -
Python使用TCPServer编写(多线程)Socket服务
SocketServer包对socket包进行了包装(封装),使得创建socket服务非常简单。TCPServer+BaseRequestHandler使用TCPServer和BaseRequestHandler编写socket服务的样例。#-*- coding:utf-8 -*-from SocketServer import TCPServer, BaseRequest原创 2013-03-09 12:03:00 · 43640 阅读 · 3 评论 -
【Python】日志格式logging format
%(name)sLogger的名字%(levelno)s数字形式的日志级别%(levelname)s文本形式的日志级别%(pathname)s调用日志输出函数的模块的完整路径名,可能没有%(filename)s调用日志输出函原创 2012-12-02 21:10:19 · 9416 阅读 · 1 评论 -
【Python】同时向控制台和文件输出日志logging
#-*- coding:utf-8 -*-import logging# 配置日志信息logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%原创 2012-12-02 20:39:42 · 19176 阅读 · 0 评论 -
【Python】修改文件并立即写回到原始位置(inplace读写)
很多应用多需要处理文件,而处理文件有一个固定的模式:打开文件,读入一些数据,处理这些数据,打印到屏幕上或写入另一个文件。那么,如果我们想修改之后立即写回文件,该怎么做呢?用什么模式打开?又怎么读写?我个人尝试了很多中方法,不是无法实现,就是操作非常麻烦。最终放弃。幸运的是,Python内置模块fileinput就可以轻松完成。代码如下:import fileinputf原创 2012-07-30 23:16:07 · 5634 阅读 · 0 评论 -
【Python】二进制文件与Base64编码文本文件转换
前面的话Python内置的base64模块,在这里http://docs.python.org/library/base64.html?highlight=base64#base64,包括b64encode,b64decode,urlsafe_b64decode等,可以满足包括URL在内的文本编码需要。但是在用base64.encode编码二进制文件的时候,发现编码不完整,只有部分文件被编原创 2012-07-29 17:31:10 · 20115 阅读 · 3 评论 -
python判断对象是否为文件对象(file object)
方法1:比较type第一种方法,就是判断对象的type是否为file,但该方法对于从file继承而来的子类不适用: >>> f = open(r"D:\2.zip")>>> type(f)>>> type(f) == fileTrue>>> class MyFile(file): pass>>> mf = MyFile(r"D:\2.txt")>>> type(mf)原创 2012-07-20 01:24:26 · 10462 阅读 · 1 评论 -
Python二进制文件与十六进制文本文件转换
Python有一个binhex模块,在http://docs.python.org/library/binhex.html,用来Encode and decode binhex4 files。我没搞懂binhex4格式,搜索了很久,找到一个讲的相对比较好的http://www.5dmail.net/html/2006-3-2/200632222823.htm。控制欲强的人,对未知或不可控充原创 2012-07-24 23:16:35 · 12609 阅读 · 1 评论 -
Python字典按值排序、包含字典的列表按字典值排序的方法
#-*- encoding=utf-8 -*-import operator#按字典值排序(默认为升序)x = {1:2, 3:4, 4:3, 2:1, 0:0}sorted_x = sorted(x.iteritems(), key=operator.itemgetter(1))print sorted_x#[(0, 0), (2, 1), (1, 2), (4, 3), (3,原创 2012-06-19 00:50:28 · 18785 阅读 · 0 评论 -
Python单例模式的4种实现方法
#-*- encoding=utf-8 -*-print '----------------------方法1--------------------------'#方法1,实现__new__方法#并在将一个类的实例绑定到类变量_instance上,#如果cls._instance为None说明该类还没有实例化过,实例化该类,并返回#如果cls._instance不为None,直接返回c原创 2012-06-17 22:49:21 · 54512 阅读 · 9 评论 -
Python单例模式终极版
如果你真的想使用其他编程语言中类似的“单例模式”,你需要看:http://blog.csdn.net/ghostfromheaven/article/details/7671853http://ghostfromheaven.iteye.com/blog/1562618但是,我要问的是,Python真的需要单例模式吗?我指像其他编程语言中的单例模式。答案是:不需要!原创 2012-06-17 23:18:04 · 15950 阅读 · 2 评论 -
Python 编写Windows服务程序:将Python作为Windows服务启动
Python程序作为Windows服务启动,需要安装pywin32包。下载路径:http://sourceforge.net/projects/pywin32/files/pywin32/ #-*- coding:utf-8 -*-import win32serviceutil import win32service import win32event clas原创 2013-02-23 12:24:28 · 30559 阅读 · 6 评论
分享