[SciPy-User] [SciPy-user] code structure advise for a model
mdekauwe
mdekauwe at gmail.com
Mon Feb 7 06:09:57 EST 2011
Hi,
thanks if you could explain that a little further it would be appreciated.
Martin
Yung-Yu Chen-2 wrote:
>
> On Mon, Feb 7, 2011 at 00:27, mdekauwe <mdekauwe at gmail.com> wrote:
>
>>
>> OK given the above perhaps my latest implementation is wrong then?
>>
>> So to recap I have a model, lets say it predicts the photosynthesis by a
>> plant. There are some parameters which are read in from a file, for
>> example
>> size of leaf, efficiency at converting sunlight etc. So my current
>> implementation is to group all the functions of the model in one class
>> and
>> pass the parameters to this class.
>>
>> (1).
>>
>> Based on previous suggestions I used ConfigParser to read a file
>> containing
>> various model parameters into a set of different dictionaries which I
>> then
>> passed around the code (for example control parameters: e.g. out_fname,
>> log_fname, model_number and initial model parameters: eg. leaf size)
>>
>> However I was finding using the dictionary syntax made the code a little
>> hard to read, so now I have packaged these dictionaries into classes
>>
>> e.g.
>>
>> class Dict2Class:
>> def __init__(self, d):
>> self.__dict__ = d
>>
>> so now instead of control_param['logfile']
>>
>> I can just do control_param.logfile.
>>
>>
> For your use case, code like:
>
> class AttributeDict(dict):
> def __getattr__(self, name):
> return self[name]
> def __setattr__(self, name, value):
> if name in self:
> self[name] = value
> else:
> super(AttributeDict, self).__setattr__(name, value)
>
> should be more straightforward. What you wanted is a dictionary that
> supports accessing its content through attribute access (x.y). What you
> did
> is replacing the name space of an object. Semantically it's not what you
> want.
>
>
>> Does that sound OK? I think it makes the code easier to read personally,
>> but
>> perhaps this is not great?
>>
>>
> It should be OK. But overriding the name space of an object, i.e.,
> __dict__, could surprise you when you want further extension to your
> class.
>
>
>> (2).
>>
>> [deleted]
>>
>
> with regards,
> Yung-Yu Chen
>
> --
> Yung-Yu Chen
> PhD candidate of Mechanical Engineering
> The Ohio State University, Columbus, Ohio
> +1 (614) 859 2436
> http://solvcon.net/yyc/
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user
>
>
--
View this message in context: http://old.nabble.com/code-structure-advise-for-a-model-tp30841514p30862532.html
Sent from the Scipy-User mailing list archive at Nabble.com.
More information about the SciPy-User
mailing list