English 中文(简体)
配置知道其文件名的python脚本
原标题:Config python script which knows its filename
  • 时间:2011-05-29 20:11:11
  •  标签:
  • python

我想通过编写一些带有变量赋值和函数的Python代码来保存复杂的配置,稍后我将通过ConfigReader类导入这些代码和函数。

基本上到目前为止,我已经编写了我的配置文件:

a=1

一个像

c=ConfigReader("C:my_file")
print(c.a)

为此,我使用了exec(),但现在我也希望配置文件知道自己的文件名(因为目录提供了一些变量的信息)

所以我需要一个配置文件,比如:

a=parse_project_number_from_dir(__file__)

似乎我需要用Python 3中的一些imp模块加载来代替exec()?这是执行一个简单文件使其知道其路径的最简单方法吗?

此外,我希望我的ConfigReader类将所有变量读入字典。(对于exec,我只是查看了locals())。我现在应该做什么?我可以不导入配置文件中的辅助导入(如parse_project_number_from_dir函数),而是导入我在那里实际定义的内容(即a=)吗?

问题回答

当<code>exec</code>一个Python脚本时,没有文件名,因为您给<code>xec</code〕一个字符串或编译对象。exec命令无法知道这个字符串是从哪里得到的。

但是有一种方法可以与要传递给exec的配置文件共享知识。例如

假设您有一个配置文件

print myname

然后运行以下Python脚本:

 ns = {}
 ns["myname"] = "sample.cfg"
 s = open("sample.cfg","r")
 exec s in ns # in 3.x use exec(s,ns) instead

这将创建一个新的命名空间,并在该命名空间中创建一个名为myname的变量。由于脚本知道要打开的文件的名称,因此可以将其分配给变量。效果与ns中的exec myname=“sample.cfg”相同。

然后,当您执行配置文件时,您通过告诉脚本使用ns命名空间来共享脚本的知识。如果这看起来有点令人困惑,请阅读一下全局本地以及命名空间。

import sys
sys.path.insert(0,"path to your file")
from yourFile import *




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...