用python代码做configure文件

yipeiwu_com5年前Python基础

(在lua中通过loadfile, setfenv实现)

python当然也可以:

cat config.py

bar = 10
foo=100
cat python_as_config.py:

ns = {}
execfile('config.py', ns)

print "\n".join(sorted(dir(ns)))
print "*"*80
print ns['foo']
print ns['bar']

缺点是不像lua那么可以以成员的方式访问table中的变量,如ns.foo, ns.bar...

例子: https://github.com/baiyanhuang/blog/tree/master/arena/python/python_as_config

相关文章

详解js文件通过python访问数据库方法

详解js文件通过python访问数据库方法

我来教你 js文件怎么通过python访问数据库,希望能够为你带来帮助。 1、如果是要提交表单内容给 服务器的 python 处理,那么只需要在表单 <form> 里面的 a...

pip 错误unused-command-line-argument-hard-error-in-future解决办法

在我的Mac Air上,用pip安装一些Python库时,偶尔就会遇到一些报错,关于“unused-command-line-argument-hard-error-in-future”...

python datetime中strptime用法详解

python 中datetime中strptime用法,具体代码如下所示: import datetime day20 = datetime.datetime.strptime('2...

Python 解码Base64 得到码流格式文本实例

我就废话不多说了,直接上代码吧! # coding:utf8 import base64 def BaseToFlow(): while True: str =...

六个窍门助你提高Python运行效率

不喜欢Python的人经常会吐嘈Python运行太慢。但是,事实并非如此。尝试以下六个窍门,来为你的Python应用提速。 窍门一:关键代码使用外部功能包 Python简化了许多编程任务...