-1

For example, I have a config file named rule1.conf like this:

[Basis]
user = "sunhf"
time = "2012-12-31"

[Bowtie]
path = "/usr/bin/bowtie"
index = "/mnt/Storage/sync/hg19"

And a models.py like this(using a package named magic.py..):

from magic import Section
class Conf:
    __confname__ = None
    basis = Section(["user", "time"])
    bowtie = Section(["path", "index"])

At last, a viewer.py like this:

from models import Conf as my_conf
my_conf.__confname__ = "rule1.conf"  // bind to the config file, I have no ideas how to do this
print my_conf.basis.user    // output: `sunhf`
print my_conf.bowtie.index  // output: `/mnt/Storage/sync/hg19`

When I run viewer.py in command line:

$ python viewer.py
sunhf
/mnt/Storage/sync/hg19

Does anyone have any ideas about how to implement the magic.py? Thanks!

Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237
  • It strikes me as weird that you'd want to work with the class itself rather than an instance of it in your viewer.py. Shouldn't you rather have a Conf.__init__(confname) which lets you create a configuration instance with a specific configuration file? – vicvicvic Nov 09 '12 at 03:53
  • @vicvicvic In SQLAlchemy, I remembered that classes in `models.py` are just used for declaring and there's no need to make instance of them: http://packages.python.org/Flask-SQLAlchemy/models.html. – Hanfei Sun Nov 09 '12 at 04:02
  • @vicvicvic And in my opinion, the `Conf` classes will only have one instance (Singleton), would it possible that I use the `Conf` class directly in `viewer.py` instead of use its instance? – Hanfei Sun Nov 09 '12 at 04:04
  • 1
    I disagree with your understanding of how SQLAlchemy works. You do have to create instances of your model classes, e.g. you do `u = User("Firegun", "fire@gun.com")` when you use them. A singleton is when you only have one instance of a class, but here you're using the class itself as an instance. – vicvicvic Nov 09 '12 at 04:13

1 Answers1

1

I put my solution here:

https://github.com/hanfeisun/meta_model

Use

python  test.py

to see the result

Hanfei Sun
  • 45,281
  • 39
  • 129
  • 237