分享一个简单的python模板引擎
#coding:utf-
8
__author__="sdm"
__author_email='sdmzhu3@gmail.com'
__date__ ="$2009-8-25 21:04:13$"
''
'
pytpl 类似
php的模板类
''
'
import
sys
import
StringIO
import
os.path
import
os
#模
板的缓存
_tpl_cache={}
class
Pytpl:
def __init__(self,tpl_path='./'
):
self.tpl_path=tpl_path
self.data={}
self.output = StringIO.StringIO()
pass
def set(self,name,value):
''
'
设
置模板变量
''
'
self.data[name]=value;
pass
def get(self,name):
''
'
得
到模板变量
''
'
t={}
return
t.get(name,
''
)
pass
def tpl(self,tplname):
相关文档:
很长的一段代码,但很清楚。哈哈。
import os
from time import strftime
stamp=strftime("%Y-%m-%d %H:%M:%S")
logfile = 'F:\\test\\m-php-framework\\tmp\logs\\error_report.log'
path = 'F:\\test\\'
files = os.listdir(path)
bytes = 0
numfiles = 0
for f in files:
if f.startswith('t'): ......
import types
type(x) is types.IntType # 判断是否int 类型
type(x) is types.StringType #是否string类型
.........
--------------------------------------------------------
超级恶心的模式,不用记住types.StringType
import types
type(x) == types(1) # ......
[原创]Python代码模块热更新机制实现(reload)
by AKara 2009-05-17 @ http://blog.csdn.net/akara @ akaras@163.com
---------------------------------------------------------------------
对一个游戏来说,无论是client或server都非常需要一套代码热更新的机制。
它能大大提高开发效率,又能超乎玩 ......
python cookbook
Recipe 2.5. Counting Lines in a File
,
今日发现一个新函数
enumerate
。一般情况下对一个列表或数组既要遍历索引又要遍历元素时,会这样写:
for
i
in
range
(0
,
len
(list
)):
&n ......
初学Python,这么做好像有点2,凑合能用:
class MyClass():
def __init__(self, n = 10):
self._Field = n
def __getitem__(self, range):
return MyClass(self._Field)
obj1 = MyClass()
obj2 = obj1
obj3 = obj1[:]
obj1._Field = 100
obj4 = MyClass(123)
print obj1._Field, obj2. ......