code structure advise for a model

  • Thread starter Martin De Kauwe
  • Start date
M

Martin De Kauwe

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).
 
M

Marco Nawijn

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
 
P

Peter Otten

Martin said:
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?

Your question is too low-level, and too technical. If you give a short
description of what the program does and what options you see to structure
it *in* *plain* *english* you'll get a clearer picture yourself and better
answers.

In general use functions, and switch to classes only if you can demonstrate
that they make the program's structure easier to understand. Rather than
use inheritance to split a "big beast that does it all" into smaller chunks
aggregate smaller classes.
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.

Yes, passing data to functions/methods/classes explicitly is better than
global state and also preferable over instance state.
 
M

Martin De Kauwe

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top