On Feb 4, 8:41 pm, Marco Nawijn <naw...@gmail.com> wrote: > On Feb 4, 3:43 am, Martin De Kauwe <mdeka...@gmail.com> wrote: > > > > > > > Hi, > > > I am translating some c++ code to python and just wanted to ask some > > advise on structure. The original has everything declared globally and > > nothing passed via function (I assume, but don't know, that this isn't > > just standard c++ practice!). So given this, I have a pretty much > > clean slate as I can't quite just copy the functions over. I was > > thinking something like this > > > class Params: > > > def __init__(self, fname): > > self.set_inital_condtions() > > self.read_input_file(fname) > > > def set_inital_conditons(self): > > self.some_parm = 0.0 > > > def read_input_file(fname): > > > #read file, change initial params if specified > > > then I thought I could pass this as an object to the model class > > > class Model(Params): > > > def __init__(self): > > # blah > > > def some_func(self): > > if (Params.some_param == something): > > foo > > > OR this just a very bad way to structure it? > > > The other thing I can't decide on is how to pass the parameters and > > variables through the class. So because of the way the original is > > written (everything is global), I could just inherit things, but it > > does means there is a lot of self. syntax. So I wondered if it might > > be better to pass things as function arguments? Any thoughts? I am > > also half considering other users from non-python backgrounds and what > > might seem very alien (syntax) to them. > > > thanks in advance > > > (ps. I am cross posting this on comp.lang.python as I am not sure > > where is more appropriate). > > I would structure it in three classes/functions: > 1. A parser class/function that reads an input file and returns a > Parameters object > 2. A Parameters class > 3. A Model class that uses the Parameters > > You would end up with something like the following: > > class MyParser(object): > > def __init__(self, filename=None): > self.filename = filename > > def parse(self, filename): > > params = Parameters() > > ...read info from filename and update parameters > > return params > > class Parameters(object): > > def __init__(self): > self.myAttribute1 = 0 > self.myAttribute2 = "A string" > > class MyModel(object): > > def __init__(self, parameters): > self.parameters = parameters > > def solve(self): > ...solve the problem > > The driver program would look something like this: > > parser = MyParser() > > params = parser.parse('inputfile') > > model = MyModel(params) > model.solve() > > I hope this is helpfull for you. > > Regards, > > Marco
thanks for that. I decided to read the params straight into a set of dictionaries and then pass these dictionaries around the various model methods. -- http://mail.python.org/mailman/listinfo/python-list