Re: subprocess svn checkout password issue

2019-03-15 Thread Martin De Kauwe
On Saturday, 16 March 2019 16:50:23 UTC+11, dieter  wrote:
> Martin De Kauwe  writes:
> 
> > I'm trying to write a script that will make a checkout from a svn repo and 
> > build the result for the user. However, when I attempt to interface with 
> > the shell it asks the user for their filename and I don't know how to 
> > capture this with my implementation. 
> >
> > user = "XXX578"
> > root="https://trac.nci.org.au/svn/cable";
> > repo_name = "CMIP6-MOSRS"
> >
> > cmd = "svn checkout %s/branches/Users/%s/%s" % (root, user, repo_name)
> > p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
> >   stdout=subprocess.PIPE, 
> > stderr=subprocess.PIPE)
> > error = subprocess.call(cmd, shell=True)
> > if error is 1:
> > raise("Error downloading repo"
> >
> > I tried adding .wait(timeout=60) to the subprocess.Popen command but that 
> > didn't work.
> >
> > Any advice on whether there is an augmentation to the above, or a better 
> > approach, would be much appreciated. I need to solve this with standard 
> > python libs as I'm trying to make this as simple as possible for the user.
> 
> That is non-trivial.
> 
> Read the "svn" documentation. You might be able to pass in the
> required information by other means, maybe an option, maybe
> an envvar, maybe via a configuration file.
> 
> Otherwise, you must monitor what it written to the subprocess'
> "stdout" and "stderr", recognized the interaction request
> perform the interaction with the user and send the result
> to the subprocess' stdin.

Thanks, I think this solution will work.

import subprocess
import getpass

user = "XXX578"
root="https://trac.nci.org.au/svn/cable";
repo_name = "CMIP6-MOSRS"

pswd = "'" + getpass.getpass('Password:') + "'"
cmd = "svn checkout %s/branches/Users/%s/%s --password %s" %\
 (root, user, repo_name, pswd)
error = subprocess.call(cmd, shell=True)
if error is 1:
raise("Error checking out repo")
-- 
https://mail.python.org/mailman/listinfo/python-list


subprocess svn checkout password issue

2019-03-15 Thread Martin De Kauwe
Hi,

I'm trying to write a script that will make a checkout from a svn repo and 
build the result for the user. However, when I attempt to interface with the 
shell it asks the user for their filename and I don't know how to capture this 
with my implementation. 

user = "XXX578"
root="https://trac.nci.org.au/svn/cable";
repo_name = "CMIP6-MOSRS"

cmd = "svn checkout %s/branches/Users/%s/%s" % (root, user, repo_name)
p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
  stdout=subprocess.PIPE, 
stderr=subprocess.PIPE)
error = subprocess.call(cmd, shell=True)
if error is 1:
raise("Error downloading repo"

I tried adding .wait(timeout=60) to the subprocess.Popen command but that 
didn't work.

Any advice on whether there is an augmentation to the above, or a better 
approach, would be much appreciated. I need to solve this with standard python 
libs as I'm trying to make this as simple as possible for the user.

The full script is here if that helps:

https://github.com/mdekauwe/CABLE_benchmarking/blob/master/scripts/get_cable.py

Thanks
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: splitting numpy array unevenly

2012-09-17 Thread Martin De Kauwe
On Tuesday, September 18, 2012 8:31:09 AM UTC+10, Wanderer wrote:
> I need to divide a 512x512 image array with the first horizontal and vertical 
> division 49 pixels in. Then every 59 pixels in after that. hsplit and vsplit 
> want to start at the edges and create a bunch of same size arrays. Is there a 
> command to chop off different sized arrays?
> 
> 
> 
> Thanks

I don't know that I follow completely, but can't you just slice what you are 
after?

x = np.random.rand(512*512).reshape(512,512)
xx = x[0,:49]
And put the rest of the slices in a loop...?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to print a module?

2011-09-05 Thread Martin De Kauwe
Trying to follow the suggestion this would be the alternate
implementation.

import sys
sys.path.append("/Users/mdekauwe/Desktop/")
import params

#params.py contains
#apples = 12.0
#cats = 14.0
#dogs = 1.3

fname = "test.asc"
try:
ofile = open(fname, 'w')
except IOError:
raise IOError("Can't open %s file for write" % fname)

attributes = [attr for attr in dir(params) if not
attr.startswith('__')]
attributes.sort()

try:
ofile.write("[params]\n")
for i in attributes:
ofile.write("%s = %s\n" % (i, getattr(params, i)))
except IOError:
raise IOError("Error writing params files, params section")

Is that a better version? I honestly don't know.

thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Best way to print a module?

2011-09-05 Thread Martin De Kauwe
Hi,

Tim yes I had a feeling my posting might be read as ambiguous! Sorry I
was trying to quickly think of a good example. Essentially I have a
set of .ini parameter files which I read into my program using
configobj, I then replace the default module parameters if the user
file is different (in my program). When it comes to writing the data
back out I need to potentially loop over 5 module files and I need to
ignore the superfluous information. My guess is that the way I am
doing it might be a little on the slow side, hence the posting. Like
you said it might be that the way I am doing it is "fine", I just
wanted to see if there was a better way that is all.

Rantingrick I did actually try the dir() to start with, I can't
remember why I changed back. I will try your suggestion and see
(thanks)

So instead of sys as per my example my module more realistically looks
like this:

params.py

apples = 12.0
cats = 14.0
dogs = 1.3

so my fuller example then

import sys
sys.path.append("/Users/mdekauwe/Desktop/")
import params

#params.py contains
#apples = 12.0
#cats = 14.0
#dogs = 1.3

fname = "test.asc"
try:
ofile = open(fname, 'w')
except IOError:
raise IOError("Can't open %s file for write" % fname)

data = []
for attr in params.__dict__.keys():
if not attr.startswith('__') and not attr.endswith('__'):
attr_val = getattr(params, attr)
data.append((attr, attr_val))

data.sort()
try:
ofile.write("[params]\n")
for i in data:
ofile.write("%s = %s\n" % (i[0], i[1]))
except IOError:
raise IOError("Error writing params files, params section")


etc, etc
thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Best way to print a module?

2011-09-05 Thread Martin De Kauwe
Hi,

If I wanted to print an entire module, skipping the attributes
starting with "__" is there an *optimal* way? Currently I am doing
something like this. Note I am just using sys here to make the point

import sys

data = []
for attr in sys.__dict__.keys():
if not attr.startswith('__') and not attr.endswith('__'):
attr_val = getattr(sys, attr)
data.append((attr, attr_val))
data.sort()
for i in data:
print "%s = %s" % (i[0], i[1])

Clearly this would be quicker if I didn't store it and sort the
output, i.e.

for attr in sys.__dict__.keys():
if not attr.startswith('__') and not attr.endswith('__'):
attr_val = getattr(sys, attr)
print "%s = %s" % (attr, attr_val)

Anyway if there is a better way it would be useful to hear it...

Many thanks,

Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: working with raw image files

2011-06-14 Thread Martin De Kauwe
what is a .raw file, do you mean a flat binary?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: replace regex in file using a dictionary

2011-04-05 Thread Martin De Kauwe
yes thanks both work nicely, I will ponder the suggestions.
-- 
http://mail.python.org/mailman/listinfo/python-list


replace regex in file using a dictionary

2011-04-05 Thread Martin De Kauwe
Hi,

So i want to replace multiple lines in a text file and I have reasoned
the best way to do this is with a dictionary. I have simplified my
example and broadly I get what I want however I am now printing my
replacement string and part of the original expression. I am guessing
that I will need to use a more complicated expression than I am
currently am to achieve what I want?

my aim is to match the expression before the "=" and replace the
string after the equals...

My failed example...

def replace_keys(text, replacements_dict):
for key, var in replacements_dict.iteritems():
replacement = "%s = %s" % (key, var)
text = text.replace(key, replacement)
return text

# example of two lines of the file
str = \
"""structcn = 150.0
metfrac0 = 0.85
"""

# replacements
replacement_dict = {"structcn": "999.0", "metfrac0": "0.85"}
new_str = replace_keys(str, replacement_dict)

print str
print
print new_str

which produces...

structcn = 150.0
metfrac0 = 0.85

structcn = 999.0 = 150.0
metfrac0 = 0.85 = 0.85

thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Fun python 3.2 one-liner

2011-03-30 Thread Martin De Kauwe
what is the character limit on a one liner :P. Very interesting
jesting apart, any more?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bounds checking

2011-03-21 Thread Martin De Kauwe
On Mar 21, 9:43 pm, Jean-Michel Pichavant 
wrote:
> Martin De Kauwe wrote:
> >> Sorry, are you trying to say that it is not practical to write correct
> >> code that isn't buggy? Well, you're honest, at least, still I can't help
> >> but feel that you're admitting defeat before even starting.
>
> > No. What I am saying is the code is written has been well tested and
> > *appears* to be working well. However the code is complicated and
> > there is potential for bugs. I think I am just been practical here,
> > evidently I can't think of everything, but there are some clear and
> > obvious errors that would be worth checking for. I can only explain
> > this in the terms of the code (sorry)...but for example the model
> > estimates plant photosynthesis and then allocates the carbon. So one
> > clear example is that the model cuts back carbon production if there
> > is water stress for the plant. This involves "removing" carbon from
> > the state. Clearly if you ended up in a situation where there is
> > negative carbon in a leaf, i.e. the leaf doesn't exist well this is
> > not physically possible and would be a code issue. Whilst this is
> > unlikely I think it would be nice to have a catch for it. Another
> > example would be the amount of soil water available to the plant,
> > again there can be zero but not negative soil water. It wouldn't be
> > meaningful. I hope that makes sense?
>
> > thanks
>
> Not that much. You'll spot bugs where negative numbers will be set to
> some attribute but what if 42 is put instead of 43 in one of your
> attribute ? Same consequence, it will mess up with your model but none
> of your check will spot that error.
>
> Try to identify those complicated functions you mentioned, and write
> unitary tests for them. Use a set of input parameters with the expected
> function return value. That way you'll be able to spot errors, whether
> the attribute is negative or not.
>
> JM
>
> PS : writing unitary tests takes time, a lot of time.

Hi,

Yes you make a good point. I think what I was trying to get across is
there are certain scenarios which can't physically happen and they are
straight forward to check for. I just thought it might be nice to add
a check for these. However you make a good case and I definitely take
your point. I guess I should look into unitary tests then (any
suggestions?)

ta.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bounds checking

2011-03-20 Thread Martin De Kauwe
On Mar 19, 8:40 pm, Steven D'Aprano  wrote:
> On Sat, 19 Mar 2011 01:38:10 -0700, Martin De Kauwe wrote:
> >> Why don't you do the range check *before* storing it in state? That way
> >> you can identify the calculation that was wrong, instead of merely
> >> noticing that at some point some unknown calculation went wrong.
>
> > I guess no reason really. I suppose in my mind I was thinking it was an
> > unlikely safeguard but I liked the idea of adding so would just do it at
> > the end of a time step. In reality I think there is practically no
> > difference and this way it is done once, in a single location vs.
> > potential 10 separate checks? I don't see the advantage?
>
> You should always aim to fail as close as possible to the source of the
> error as is practical. That decreases the amount of debugging required
> when something fails: instead of searching your entire program, you only
> have to search a very small amount of code.
>
> --
> Steven

OK I take your point and can see the superior logic! I shall amend
what I was planning
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bounds checking

2011-03-19 Thread Martin De Kauwe

> Sorry, are you trying to say that it is not practical to write correct
> code that isn't buggy? Well, you're honest, at least, still I can't help
> but feel that you're admitting defeat before even starting.

No. What I am saying is the code is written has been well tested and
*appears* to be working well. However the code is complicated and
there is potential for bugs. I think I am just been practical here,
evidently I can't think of everything, but there are some clear and
obvious errors that would be worth checking for. I can only explain
this in the terms of the code (sorry)...but for example the model
estimates plant photosynthesis and then allocates the carbon. So one
clear example is that the model cuts back carbon production if there
is water stress for the plant. This involves "removing" carbon from
the state. Clearly if you ended up in a situation where there is
negative carbon in a leaf, i.e. the leaf doesn't exist well this is
not physically possible and would be a code issue. Whilst this is
unlikely I think it would be nice to have a catch for it. Another
example would be the amount of soil water available to the plant,
again there can be zero but not negative soil water. It wouldn't be
meaningful. I hope that makes sense?

thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bounds checking

2011-03-19 Thread Martin De Kauwe

> assert all(x >= 0 for x in (a, b, c, d, e, f, g, h, i, j))

yep neat!



> Why don't you do the range check *before* storing it in state? That way
> you can identify the calculation that was wrong, instead of merely
> noticing that at some point some unknown calculation went wrong.

I guess no reason really. I suppose in my mind I was thinking it was
an unlikely safeguard but I liked the idea of adding so would just do
it at the end of a time step. In reality I think there is practically
no difference and this way it is done once, in a single location vs.
potential 10 separate checks? I don't see the advantage?

> You're looking at every single attribute, including those of super
> classes, when you only want to check "10 or so" attributes. That's
> probably not wise. At the very least, dir() will be a fairly expensive
> call.

yes your right, sorry I was just trying to post a quick example to go
with my question. I would use a list of the attributes to check.

>
> If you insist on checking state *after* the value is stored, instead of
> preventing it from being stored in the first place, it is better to make
> the state object responsible for doing it's own bounds checking. That way
> only the state object needs to be updated when you change those ten
> attributes, instead of some arbitrary number of places scattered all
> throughout your code.

yes I think this is what I was getting at, I will look at your
suggestion thanks.


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bounds checking

2011-03-19 Thread Martin De Kauwe

> dir() has to do a bit a computation. I would be tempted to give 'state'
> a set of attributes to check. Call it 'nonnegatives'.
>     for attr in nonnegatives:
>        if ...
>
> This allows for attributes not subject to that check.
>
> --
> Terry Jan Reedy

Agreed. I was trying to just write a dummy example quickly to go with
my question and that was just the way that came to mind so that I
could loop over them to test. It wasn't a great example sorry! In
reality I would just have like you said a subset a list I guess of the
ones I would check.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bounds checking

2011-03-18 Thread Martin De Kauwe

> Offhand, my only quibble is that sys.exit is not helpful for debugging.  
> Much better to raise an error:
>
>         if not self.funcTable.get(attribute, lambda x: True)(value):
>             raise ValueError ('error out of bound')
>
> or define a subclass of ValueError just for this purpose.  On error, the
> program will stop just as dead, but you'll get a trace.
>
>         Mel.

I think generally I prefer my code to die and as long as I know where
(from the statement) the error occurred I know generally what point I
have to debug up until. Can you explain how your solution would be
easier with a trace as I don't tend to use the raise/assert
functionality so I am interested.

thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Bounds checking

2011-03-18 Thread Martin De Kauwe

> Don't check for bounds, fix any bug in the code that would set your
> values out of bounds and use asserts while debugging.
>

whilst that is a nice idea in practice this just is not a practical
solution.


> Otherwise if you really need dynamic checks, it will cost you cpu, for
> sure.

Yes I agree and I hadn't decided whether to add it or not as there
aren't any current issues. However I can see that the check would
overall be safer. I was just wondering if there was some super smartie
pants solution :P


Howeverver you could for instance override the __setatttr__ of
> state object, and call the attribute's associated function.
>
> class State(object):
>     funcTable = {
>        'foo': lambda x: x >= 0.0
>     }
>
>     def __init__(self):
>        self.foo = 0
>
>     def __setattr__(self, attribute, value):
>        if not self.funcTable.get(attribute, lambda x: True)(value):
>            sys.exit('error out of bound')
>        return object.__setattr(self, attribute, value)
>
> Untested, however it's just an idea. I'm not even sure that would be
> less cpu consuming :D

thanks I will look at what you suggested.


-- 
http://mail.python.org/mailman/listinfo/python-list


Bounds checking

2011-03-18 Thread Martin De Kauwe
Hi,

if one has a set of values which should never step outside certain
bounds (for example if the values were negative then they wouldn't be
physically meaningful) is there a nice way to bounds check? I
potentially have 10 or so values I would like to check at the end of
each iteration. However as the loop is over many years I figured I
probably want to be as optimal as possible with my check. Any
thoughts?

e.g. this is my solution

# module contain data
# e.g. print state.something might produce 4.0
import state as state

def main():
for i in xrange(num_days):
# do stuff

# bounds check at end of iteration
bounds_check(state)


def bounds_check(state):
""" check state values are > 0 """
for attr in dir(state):
if not attr.startswith('__') and getattr(state, attr) < 0.0:
print "Error state values < 0: %s" % (attr)
sys.exit()

if __name__ == "__main__":
sys.exit(main())

thanks

Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Code structure help

2011-03-09 Thread Martin De Kauwe
Hi,

I have been working on re-writing a model in python and have been
trying to adopt some of the advise offered on here to recent
questions. However I am not sure how easy on the eye my final
structure is and would appreciate any constructive comments/
suggestions. So broadly the model estimates plant growth using a
number of related sub functions which I have grouped into classes and
they all live in separate files. My main issue at the moment is I
think I have a lot of verbose class instances but I really can't see a
better way to do it. I have left some of the bones of the code out but
this is basically what would it looks like.

thanks.


import constants as const
from file_parser import ConfigFileParser
from plant_growth import PlantGrowth
from print_outputs import PrintOutput
from litter import LitterFlows
from decomp import DecompFactors
from soil_cnflows import CarbonFlows, NitrogenFlows
from nmineralisation import Mineralisation
from update_pools import CarbonPools, NitrogenPools
...etc...

class ModelName(object):
def __init__(self, cfg_fname=None):
""" Set everything up

Read meterological forcing file, any user adjusted files. If
there
is anything in the user file then adjust the model parameters,
control
or initial state attributes that are used within the code.
"""

# sweep the cmd line
options, args = cmdline_parser()

# quit if asked only to dump default paramater files
if options.DUMP_INPUT == True:
...call some stuff...

# read in user defined variables (stored in dictionaries)
pars = ConfigFileParser(cfg_fname=cfg_fname)
(control, params, state, files, fluxes, met_data) =
pars.main()

# objects holding model state, fluxes, parameters and control
flags
self.met_data = met_data
self.control = control
self.params = params
self.state = state
self.fluxes = fluxes

# instances of other model parts..
self.pr = PrintOutput(self.params, self.state, self.fluxes,
self.control, self.files, dump=False)

# start date of simulation
self.date = self.simulation_start_date()


...etc

def run_sim(self):

mi = Mineralisation(self.control, self.params, self.state,
self.fluxes)
cf = CarbonFlows(self.control, self.params, self.state,
self.fluxes)
nf = NitrogenFlows(self.control, self.params, self.state,
self.fluxes)
de = Derive(self.control, self.params, self.state,
self.fluxes)
dc = DecompFactors(self.control, self.params, self.state,
self.fluxes)
lf = LitterFlows(self.control, self.params, self.state,
self.fluxes)
pg = PlantGrowth(self.control, self.params, self.state,
self.fluxes,
self.met_data)
cpl = CarbonPools(self.control, self.params, self.state,
self.fluxes)
npl = NitrogenPools(self.control, self.params, self.state,
self.fluxes)

for i in self.met_data.doy:
project_day = i - 1

# N:C ratios
leafnc, rootnc = self.leaf_root_ncratio()

# litterfall rate: C and N fluxes
lf.flows(leafnc, rootnc)

# plant growth
pg.grow(project_day, self.date, leafnc)

# calculate model decay rates
dc.decay_rates()

# soil model fluxes
cf.calculate_cflows()
nf.calculate_nflows()

# N uptake and loss
mi.calculate_mineralisation()

# carbon sink or source?
self.fluxes.nep = (self.fluxes.npp -
self.fluxes.hetero_resp -
self.fluxes.ceaten *
(1. - self.params.fracfaeces))

# soil model - update pools
cact, cslo, cpas = cpl.calculate_cpools()
npl.calculate_npools(cact, cslo, cpas)

if self.control.print_options == 1:
self.pr.print_state()
self.pr.print_fluxes()

self.increment_date()

if self.control.print_options == 2:
self.pr.print_state()
self.pr.print_fluxes()

# house cleaning, close ouput files
self.pr.tidy_up()



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Defining class attributes + inheritance

2011-03-08 Thread Martin De Kauwe
On Mar 9, 12:53 pm, "Rhodri James" 
wrote:
> On Wed, 09 Mar 2011 01:00:29 -0000, Martin De Kauwe   
> wrote:
>
> > class BaseClass(object):
> >    def __init__(self, a, b, c, d):
> >         self.a = a
> >         self.b = b
> >         self.c = c
> >         self.d = d
>
> > class NewClass(BaseClass):
> >     def __init__(self):
> >         super(NewClass, self).__init__(new)
> >         self.new = new
> >         print self.new
>
> Two things leap out immediately.  First, BaseClass.__init__ takes four  
> parameters besides `self`, but when you call it you only give it one  
> parameter, `new`.  It's not going to like that.  Second, where did `new`  
> come from?  It's not a parameter to NewClass.__init__, and it doesn't seem  
> to be a global.  That's not going to work well either.
>
> However neither of these things are what the traceback is complaining  
> about.
>
> >  if __name__ == "__main__":
>
> >     A = PreviousClass(1, 2, 3, 4, 5)
> >     B = NewClass(1, 2, 3, 4, 5)
>
> > $ python test.py
> > Traceback (most recent call last):
> >   File "model_data.py", line 29, in 
> >     B = NewClass(1, 2, 3, 4, 5)
> > TypeError: __init__() takes exactly 1 argument (6 given)
>
> When you create your NewClass, you give it five parameters (1, 2, 3, 4, 5)  
> plus the implicit `self`, which is the "(6 given)" part of the message.  
> However NewClass.__init__ takes no parameters aside from `self` (i.e. it  
> "takes exactly 1 argument").  There's a mismatch between what you've told  
> NewClass.__init__ to expect and what you actually deliver to it.
>
> The point that may be confusing you is that NewClass.__init__ knows  
> nothing at all about BaseClass.__init__.  It doesn't inherit parameters  
>  from it or anything magical like that; you have to tell it everything.  If  
> you want parameters to pass from NewClass.__init__ to BaseClass.__init__  
> you will have to provide them somehow.  In this case, I think what you  
> meant was something like this:
>
> class NewClass(BaseClass):
>      def __init__(self, a, b, c, d, new):
>          super(NewClass, self).__init__(a, b, c, d)
>         self.new = new
>          # etc
>
> --
> Rhodri James *-* Wildebeest Herder to the Masses

Yep that was it, I think I misunderstood and assumed it knew about
stuff in the BaseClass constructor. All makes sense now!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Defining class attributes + inheritance

2011-03-08 Thread Martin De Kauwe
On Mar 9, 11:50 am, "Rhodri James" 
wrote:
> On Wed, 09 Mar 2011 00:29:18 -0000, Martin De Kauwe   
> wrote:
>
>
>
>
>
>
>
> > On Mar 9, 10:20 am, Ethan Furman  wrote:
> [snip]
> >> Just make sure and call the parent's constructor, either with
>
> >> class NewClass(BaseClass):
> >>      def __init__(self, ):
> >>          BaseClass.__init__(self, other_params)
>
> >> or
>
> >> class NewClass(BaseClass):
> >>      def __init__(self, ):
> >>          super(NewClass, self).__init__()
>
> >> ~Ethan~
>
> > Hi thanks, but I think I am implementing it wrong then?
>
> > BaseClass has 4 attributes and when I tried what you said
>
> > class NewClass(BaseClass):
> >     def __init__(self):
> >         super(NewClass, self).__init__(new_thing)
>
> > I get the error
>
> > TypeError: __init__() takes exactly 1 argument (6 given)
>
> Please give us either the rest of the code or the rest of the
> traceback, or preferably both.  Without one or the other we have
> little hope of guessing what you've typed.
>
> --
> Rhodri James *-* Wildebeest Herder to the Masses

OK

class BaseClass(object):

def __init__(self, a, b, c, d):
self.a = a
self.b = b
self.c = c
self.d = d


class NewClass(BaseClass):
def __init__(self):
super(NewClass, self).__init__(new)
self.new = new
print self.new

class PreviousClass:
def __init__(self, a, b, c, d, new):
self.a = a
self.b = b
self.c = c
self.d = d
self.new = new
print self.new


if __name__ == "__main__":

A = PreviousClass(1, 2, 3, 4, 5)
B = NewClass(1, 2, 3, 4, 5)

$ python test.py
Traceback (most recent call last):
  File "model_data.py", line 29, in 
B = NewClass(1, 2, 3, 4, 5)
TypeError: __init__() takes exactly 1 argument (6 given)



So NewClass is my attempt to implement what I was shown and
PreviousClass was how I was originally solving the issue, i.e. I
wouldn't inherit the BaseClass.

thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Defining class attributes + inheritance

2011-03-08 Thread Martin De Kauwe
On Mar 9, 10:20 am, Ethan Furman  wrote:
> Martin De Kauwe wrote:
> > Hi,
>
> > I think this might be obvious? I have a base class which contains X
> > objects which other classes inherit e.g.
>
> > class BaseClass(object):
> >     def __init__(self, something, something_else):
> >         self.something = something
> >         self.something_else = something_else
> >         # etc
>
> > Typically I would use this like this
>
> > from some_module_name import BaseClass
>
> > class NewClass(BaseClass):
> >     def do_something(self):
> >          print self.something
> >          # etc
>
> > Which is fine. However if I need to inherit additional attributes (to
> > NewClass) at the constructor step it means I have to completely
> > redefine the constructor [...]
>
> Just make sure and call the parent's constructor, either with
>
> class NewClass(BaseClass):
>      def __init__(self, ):
>          BaseClass.__init__(self, other_params)
>
> or
>
> class NewClass(BaseClass):
>      def __init__(self, ):
>          super(NewClass, self).__init__()
>
> ~Ethan~

Hi thanks, but I think I am implementing it wrong then?

BaseClass has 4 attributes and when I tried what you said

class NewClass(BaseClass):
def __init__(self):
super(NewClass, self).__init__(new_thing)

I get the error

TypeError: __init__() takes exactly 1 argument (6 given)
-- 
http://mail.python.org/mailman/listinfo/python-list


Defining class attributes + inheritance

2011-03-08 Thread Martin De Kauwe
Hi,

I think this might be obvious? I have a base class which contains X
objects which other classes inherit e.g.

class BaseClass(object):
def __init__(self, something, something_else):
self.something = something
self.something_else = something_else
# etc

Typically I would use this like this

from some_module_name import BaseClass

class NewClass(BaseClass):
def do_something(self):
 print self.something
 # etc

Which is fine. However if I need to inherit additional attributes (to
NewClass) at the constructor step it means I have to completely
redefine the constructor and therefore can't inherit in this way,
which defeats the purpose of defining a default base class. Am I being
slow is there a nice solution to this or is that the way it works?

thanks,

Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT: Code Examples

2011-03-02 Thread Martin De Kauwe
On Mar 2, 3:30 am, Robert Kern  wrote:
> On 2/28/11 10:03 AM, Fred Marshall wrote:
>
> > I'm interested in developing Python-based programs, including an engineering
> > app. ... re-writing from Fortran and C versions. One of the objectives 
> > would to
> > be make reasonable use of the available structure (objects, etc.). So, I'd 
> > like
> > to read a couple of good, simple scientific-oriented programs that do that 
> > kind
> > of thing.
>
> You may want to take a look at Clear Climate Code. It is a rewrite of an old,
> hairy FORTRAN climate analysis program with the goal of making it clearer and
> easier to understand. That's somewhat different from having an example of what
> you would write de novo, but it might help give you strategies for the process
> of translating your own FORTRAN and C engineering codes.
>
>    http://clearclimatecode.org/
>
> --
> Robert Kern
>
> "I have come to believe that the whole world is an enigma, a harmless enigma
>   that is made terrible by our own mad attempt to interpret it as though it 
> had
>   an underlying truth."
>    -- Umberto Eco

that link is really useful helps that it is the same field! Does
anyone have any more like that?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: OT: Code Examples

2011-03-01 Thread Martin De Kauwe
On Mar 1, 3:03 am, Fred Marshall 
wrote:
> I'm interested in developing Python-based programs, including an
> engineering app. ... re-writing from Fortran and C versions.  One of the
> objectives would to be make reasonable use of the available structure
> (objects, etc.).  So, I'd like to read a couple of good, simple
> scientific-oriented programs that do that kind of thing.
>
> Looking for links, etc.
>
> Fred

you could try searching here http://pypi.python.org/pypi. I had a look
for models, but didn't find anything in particular. But would be
really useful if anyone does know of some good examples
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-14 Thread Martin De Kauwe
I managed to get it to work like it explained, apologies not sure what
I did wrong earlier, odd.

Anyway thanks a lot for all of the suggestions + help
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-14 Thread Martin De Kauwe
On Feb 14, 8:57 pm, Martin De Kauwe  wrote:
> On Feb 14, 8:51 pm, Dave Angel  wrote:
>
>
>
>
>
> > On 01/-10/-28163 02:59 PM, Martin De Kauwe wrote:
>
> > > 
>
> > > from other_model import OtherSubModel
> > > class Model:
>
> > >      def __init__(self):
>
> > >          # included other external modules (i.e. in other files)
> > >          om =therSubModel()
>
> > >      def run(self):
> > >          # call other submodel and pass params
> > >          om.run(params)
>
> > What's om supposed to be?  You define a local variable in one method,
> > and try to use it in a different one 
>
> > Perhaps you meant  self.om =
>
> >  > class OtherSubModel:
> >  >     def __init__(self):
> >  >         #some stuff
> >  >
> >  >     def run(params):
>
> > You're missing a self here in the formal parameter list.
>
> > DaveA
>
> I was trying to write a smaller version of broadly what I think i am
> doing, perhaps I am not implementing it correctly. I have one model
> which I have coded as a class, there were a few parts which could be
> used separately (sub-models). So I decided to make them separate
> classes in separate files which I *thought* I could just join up into
> one package at run time. This way I could also use these modules
> separately.
>
> So in the dummy example
>
> the other file is imported
>
> from other_model import OtherSubModel
>
> amd om = OtherSubModel was me making an instance of it? And then when
> I called the *run* part of this sub-model I was passing it the
> parameter file. I am sensing I have this wrong?

oh and yes i did mean self.om.run(params), sorry!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to create a dict based on such a file?

2011-02-14 Thread Martin De Kauwe
On Feb 14, 6:10 pm, aspineux  wrote:
> On 14 fév, 06:47, Wang Coeus  wrote:
>
> > Hi all,
> > I am new to python. Currently I encountered a problem, please help me to
> > solve this. Thanks in advance!
> > I have a file like below:
>
> ConfigParser Library does exacly what you want but with .ini file
> format
> [block1]
> key1=value1
> key2=value2
> ...
>
> Can you change the format of your file ? If so
>
> import ConfigParser
> config=ConfigParser.RawConfigParser(config_default)
> try:
>     config.readfp(open(filename, 'r'))
> except Exception, e:
>     logging.error('error reading configuration file %s: %s', filename,
> e)
>     sys.exit(1)
>
> def func(config, key1):
>     result={}
>     for section in config.sections():
>         if config.has_option(section, key1):
>             result[section]=config.get(section, key1)
>     return result
>
> If not, you need to parse youre file, and the some question :
> How or what generate this file, is it always the same format ? Could
> it chnage, for exemple for
>
> block1 { key1=value1 key2=value2 }
>
> or at least
>
> block1 {
>
> key1=value1
> key2=value2
>
> }
>
> Is-it big, too big to keep in memory ?
>
>
>
> > ++
> > block1
> > {
> >   key1=value1
> >   key2=value2
> >   key3=value3}
>
> > block2
> > {
> >   key1=value4
> >   key2=value5
> >   key4=value6}
>
> > ...
> > blockn
> > {
> >   key1=value7
> >   key2=value8
> >   keyn=valuen}
>
> > +++
> > Different block may have different keys and even same key in different
> > blocks may have different values.
>
> > Now I want to get a function, which like this:
> > func(key)
> > and it will return a dictionary as below:
> > func(key1) = [block1:value1,block2:value4,...,blockn:value7]
> > and if one block has no "key1" parameter, it will not include in this
> > dict.
>
> > Thanks a lot!
> > --
> > Coeus
> > In the middle of every difficulty lies opportunity.
> >                 -- Albert Einstein

configobj is even better, very similar usage.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-14 Thread Martin De Kauwe
On Feb 14, 8:51 pm, Dave Angel  wrote:
> On 01/-10/-28163 02:59 PM, Martin De Kauwe wrote:
>
> > 
>
> > from other_model import OtherSubModel
> > class Model:
>
> >      def __init__(self):
>
> >          # included other external modules (i.e. in other files)
> >          om =therSubModel()
>
> >      def run(self):
> >          # call other submodel and pass params
> >          om.run(params)
>
> What's om supposed to be?  You define a local variable in one method,
> and try to use it in a different one 
>
> Perhaps you meant  self.om =
>
>  > class OtherSubModel:
>  >     def __init__(self):
>  >         #some stuff
>  >
>  >     def run(params):
>
> You're missing a self here in the formal parameter list.
>
> DaveA

I was trying to write a smaller version of broadly what I think i am
doing, perhaps I am not implementing it correctly. I have one model
which I have coded as a class, there were a few parts which could be
used separately (sub-models). So I decided to make them separate
classes in separate files which I *thought* I could just join up into
one package at run time. This way I could also use these modules
separately.

So in the dummy example

the other file is imported

from other_model import OtherSubModel

amd om = OtherSubModel was me making an instance of it? And then when
I called the *run* part of this sub-model I was passing it the
parameter file. I am sensing I have this wrong?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-14 Thread Martin De Kauwe
On Feb 14, 7:12 pm, Terry Reedy  wrote:
> On 2/13/2011 8:33 PM, Martin De Kauwe wrote:
>
> > Cool! Thanks this seems straight forward, however if I do it this way
> > then once I change it (i.e. after reading user param file) I can't
> > pass the changed version to another module can i? Or am I being
> > stupid? Unless I do it my way I think
>
> > import cons
> >>>> print cons.kg_as_g
> >>>> 1000.0
> >>>> cons.kg_as_g = 23.4
> >>>> print cons.kg_as_g
>
> > is fine.
>
> > But if I call import cons in another module it would be back to 1000.
> > right?
>
> Wrong. There is one and only one module object, imported into (which
> means bound to a name in) as many modules as you want. If you change an
> attribute, it is changed for all. If you add attributes from any module,
> they are added for all, regardless of whether or not they have already
> imported it. No different from changing a list or dict with multiple names.
>
> The math module is read only. User python-coded modules are read-write.
>
> --
> Terry Jan Reedy

OK thanks I'll go over what I have done as I must have done something
stupid as I thought I tested it earlier and couldn't get it to work
like you suggested. Broadly this is how I am using it (incase it is
obvious, although perhaps it just works and I need to recheck). Apart
from that thanks for the modules idea it is very simple and seems to
be working nicely in principle.

from other_model import OtherSubModel
class Model:

def __init__(self):

# included other external modules (i.e. in other files)
om = OtherSubModel()

def run(self):
# call other submodel and pass params
om.run(params)

and the other file would look something like this

class OtherSubModel:
def __init__(self):
#some stuff

def run(params):

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-13 Thread Martin De Kauwe
On Feb 14, 12:02 pm, Terry Reedy  wrote:
> On 2/13/2011 6:16 PM, Martin De Kauwe wrote:
>
>
>
>
>
> > I think I got it, did you mean something like this?
>
> > class Constants:
>
> >      radius_of_earth = 6.37122E+6
> >      days_as_yrs = 1.0 / 365.25
> >      m2_as_ha = 1E-4  # metres squared as hectares
> >      g_as_tonnes = 1E-6  # grammes as tonnes
> >      kg_as_tonnes = 1E-3  # kg as tonnes
> >      kg_as_g = 1E+3
>
> >      def __init__(self):
>
> >          self.radius_of_earth = self.__class__.radius_of_earth
> >          self.days_as_yrs = self.__class__.days_as_yrs
> >          self.m2_as_ha = self.__class__.m2_as_ha
> >          self.g_as_tonnes = self.__class__.g_as_tonnes
> >          self.kg_as_tonnes = self.__class__.kg_as_tonnes
> >          self.kg_as_g = self.__class__.kg_as_g
>
> > usage something like
>
> >>> >from constants import Constants
> >>>> Constants.kg_as_g
> >>>> 1000.0
>
> No, simpler. No class statememt necessarily needed.
>
> module constants
> 
>
> radius_of_earth = 6.37122E+6
> days_as_yrs = 1.0 / 365.25
> m2_as_ha = 1E-4  # metres squared as hectares
> g_as_tonnes = 1E-6  # grammes as tonnes
> kg_as_tonnes = 1E-3  # kg as tonnes
> kg_as_g = 1E+3
> # could also have code to load stuff from other files
>
> usage
> =
>  >>> import constants
>  >>> constants.kg_as_g
> 1000.0
>
> Real example
>  >>> import math
>  >>> math.pi, math.e
> (3.141592653589793, 2.718281828459045)
>
> --
> Terry Jan Reedy

Cool! Thanks this seems straight forward, however if I do it this way
then once I change it (i.e. after reading user param file) I can't
pass the changed version to another module can i? Or am I being
stupid? Unless I do it my way I think

import cons
>>>print cons.kg_as_g
>>>1000.0
>>>cons.kg_as_g = 23.4
>>>print cons.kg_as_g

is fine.

But if I call import cons in another module it would be back to 1000.
right? Whereas if I do it as a class I can pass the adjusted instance?
E.g.

from constants import Constants
c = Constants()

I can then change "c" and pass this around?

Apologies if I have got that totally wrong!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-13 Thread Martin De Kauwe
On Feb 14, 10:16 am, Martin De Kauwe  wrote:
> On Feb 13, 6:35 pm, Terry Reedy  wrote:
>
>
>
>
>
> > On 2/12/2011 9:20 PM, Martin De Kauwe wrote:
>
> > > On Feb 13, 5:12 am, Terry Reedy  wrote:
> > >> On 2/12/2011 1:24 AM, Martin De Kauwe wrote:
>
> > >>> The point of this posting was just to ask those that know, whether it
> > >>> was a bad idea to use the class object in the way I had or was that
> > >>> OK? And if I should have just used a dictionary, why?
>
> > >> Did you miss my suggestion to use a module rather than a class?
> > >> Modules do not have some of the disadvantages of classes listed by J. 
> > >> Nagle.
>
> > >> --
> > >> Terry Jan Reedy
>
> > > Hi, sorry I did. I just re-read it, could you provide an example?
>
> > I am not sure what you are asking. tkinter.contants is one example.
>
> > > Sorry i am having a major mind blank this morning (I think this is
> > > obvious!?). And it would meet all of the criteria outlined by John
> > > Nagle?
>
> > A module will work fine if but only if you commit yourself to having all
> > keys be legal Python identifiers. But that is usually not a problem with
> > configuration values.
>
> > --
> > Terry Jan Reedy
>
> I think I got it, did you mean something like this?
>
> class Constants:
>
>     radius_of_earth = 6.37122E+6
>     days_as_yrs = 1.0 / 365.25
>     m2_as_ha = 1E-4  # metres squared as hectares
>     g_as_tonnes = 1E-6  # grammes as tonnes
>     kg_as_tonnes = 1E-3  # kg as tonnes
>     kg_as_g = 1E+3
>
>     def __init__(self):
>
>         self.radius_of_earth = self.__class__.radius_of_earth
>         self.days_as_yrs = self.__class__.days_as_yrs
>         self.m2_as_ha = self.__class__.m2_as_ha
>         self.g_as_tonnes = self.__class__.g_as_tonnes
>         self.kg_as_tonnes = self.__class__.kg_as_tonnes
>         self.kg_as_g = self.__class__.kg_as_g
>
> usage something like
>
> >>>from constants import Constants
> >>>Constants.kg_as_g
> >>>1000.0
>
> Something similar for the params?
>
> thanks

And then I guess define some default parameters and then use configobj
to parse a .INI file. Then adjust those parameters change in the .INI
file?

e.g.

from parameters import DefaultParameters as params
# d -> comes from .INI file
p = params()
for key, value in d.iteritems():
# change anything read from file
p.__dict__[key] = value

?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-13 Thread Martin De Kauwe
On Feb 13, 6:35 pm, Terry Reedy  wrote:
> On 2/12/2011 9:20 PM, Martin De Kauwe wrote:
>
> > On Feb 13, 5:12 am, Terry Reedy  wrote:
> >> On 2/12/2011 1:24 AM, Martin De Kauwe wrote:
>
> >>> The point of this posting was just to ask those that know, whether it
> >>> was a bad idea to use the class object in the way I had or was that
> >>> OK? And if I should have just used a dictionary, why?
>
> >> Did you miss my suggestion to use a module rather than a class?
> >> Modules do not have some of the disadvantages of classes listed by J. 
> >> Nagle.
>
> >> --
> >> Terry Jan Reedy
>
> > Hi, sorry I did. I just re-read it, could you provide an example?
>
> I am not sure what you are asking. tkinter.contants is one example.
>
> > Sorry i am having a major mind blank this morning (I think this is
> > obvious!?). And it would meet all of the criteria outlined by John
> > Nagle?
>
> A module will work fine if but only if you commit yourself to having all
> keys be legal Python identifiers. But that is usually not a problem with
> configuration values.
>
> --
> Terry Jan Reedy

I think I got it, did you mean something like this?


class Constants:

radius_of_earth = 6.37122E+6
days_as_yrs = 1.0 / 365.25
m2_as_ha = 1E-4  # metres squared as hectares
g_as_tonnes = 1E-6  # grammes as tonnes
kg_as_tonnes = 1E-3  # kg as tonnes
kg_as_g = 1E+3

def __init__(self):

self.radius_of_earth = self.__class__.radius_of_earth
self.days_as_yrs = self.__class__.days_as_yrs
self.m2_as_ha = self.__class__.m2_as_ha
self.g_as_tonnes = self.__class__.g_as_tonnes
self.kg_as_tonnes = self.__class__.kg_as_tonnes
self.kg_as_g = self.__class__.kg_as_g


usage something like

>>>from constants import Constants
>>>Constants.kg_as_g
>>>1000.0

Something similar for the params?

thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-12 Thread Martin De Kauwe
On Feb 13, 5:12 am, Terry Reedy  wrote:
> On 2/12/2011 1:24 AM, Martin De Kauwe wrote:
>
> > The point of this posting was just to ask those that know, whether it
> > was a bad idea to use the class object in the way I had or was that
> > OK? And if I should have just used a dictionary, why?
>
> Did you miss my suggestion to use a module rather than a class?
> Modules do not have some of the disadvantages of classes listed by J. Nagle.
>
> --
> Terry Jan Reedy

Hi, sorry I did. I just re-read it, could you provide an example?
Sorry i am having a major mind blank this morning (I think this is
obvious!?). And it would meet all of the criteria outlined by John
Nagle?
-- 
http://mail.python.org/mailman/listinfo/python-list


Unpacking multiple dictionaries in a function?

2011-02-12 Thread Martin De Kauwe
Hi,

Is there a better way to unpack more than one dictionary in a function
than...

def unpack_dicts(f):
def wrapper(*old_dicts):
dict={}
for d in old_dicts:
dict.update(d)
return f(**dict)
return wrapper

@unpack_dicts
def some_func(a=None, b=None, c=None):
print a, b, c

d1 = {'a': 20.0, 'b': '-1'}
d2 = {'c': 33.0}

some_func(d1, d2)

thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-12 Thread Martin De Kauwe
On Feb 12, 8:06 pm, Martin De Kauwe  wrote:
> On Feb 12, 7:21 pm, Andrea Crotti  wrote:
>
> > Il giorno 12/feb/2011, alle ore 00.45, Martin De Kauwe ha scritto:
>
> > > Hi,
>
> > > yes I read a .INI file using ConfigParser, just similar sections (in
> > > my opinion) to make one object which i can then pass to different
> > > classes. E.G.
>
> > Ok then I suggest configobj, less buggy and much more powerful than 
> > ConfigParser:http://www.voidspace.org.uk/python/configobj.html
>
> > (and included from python 2.7).
> > In this way you can also simply just carry around that dictionary, and it 
> > will be correctly
> > typed if you validate the input.
>
> That is interesting however I am using python 2.6 so I guess I shall
> have to stick as I am for the moment. I think the way I used it above
> was quite straight forward? It seemed OK? What are the issues?

Ignore that I have just tested it (it works in 2.6), much better!
Thanks!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-12 Thread Martin De Kauwe
On Feb 12, 7:21 pm, Andrea Crotti  wrote:
> Il giorno 12/feb/2011, alle ore 00.45, Martin De Kauwe ha scritto:
>
> > Hi,
>
> > yes I read a .INI file using ConfigParser, just similar sections (in
> > my opinion) to make one object which i can then pass to different
> > classes. E.G.
>
> Ok then I suggest configobj, less buggy and much more powerful than 
> ConfigParser:http://www.voidspace.org.uk/python/configobj.html
>
> (and included from python 2.7).
> In this way you can also simply just carry around that dictionary, and it 
> will be correctly
> typed if you validate the input.

That is interesting however I am using python 2.6 so I guess I shall
have to stick as I am for the moment. I think the way I used it above
was quite straight forward? It seemed OK? What are the issues?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-12 Thread Martin De Kauwe
On Feb 12, 7:22 pm, John Nagle  wrote:
> On 2/11/2011 6:56 AM, Martin De Kauwe wrote:
>
> > Hi,
>
> > I have a series of parameter values which i need to pass throughout my
> > code (>100), in C I would use a structure for example. However in
> > python it is not clear to me if it would be better to use a dictionary
> > or build a class object? Personally I think accessing the values is
> > neater (visually) with an object rather than a dictionary, e.g.
>
> > x = params['price_of_cats'] * params['price_of_elephants']
>
> > vs.
>
> > x = params.price_of_cats * params.price_of_elephants
>
>     Don't use a class as a dictionary.  It causes various forms
> of grief.  A dictionary will accept any string as a key, and
> has no reserved values.   That's not true of class attributes.
> There are already many names in a class's namespace, including
> any functions of the class.  Attribute syntax is restricted -
> there are some characters you can't use.  Unicode attributes
> don't work right prior to Python 3.  If the names are coming
> in from outside the program, there's a potential security
> hole if someone can inject names beginning with "__" and
> mess with the internal data structures of the class.
> And, of course, you can't use a name that's a reserved
> word in Python.
>
>     (This last is forever causing grief in programs that
> parse HTML and try to use Python class attributes to
> represent HTML attributes.  "class" is a common HTML
> attribute but a reserved word in Python.  So such parsers
> have to have a special case for reserved words.  Ugly.)
>
>     In Javascript, classes are simply dictionaries, but Python
> is not Javascript.  If you want a dictionary, use a "dict".
>
>                                 John Nagle

OK this was what I was after, thanks. I shall rewrite as dictionaries
then given what you have said.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-11 Thread Martin De Kauwe
Steven,

You make some good points and I don't disagree with you. The code is
just a transfer from some older c++ code which had all sorts of
windows menus etc, which isn't required and would make it impossible
to run (again and again). It is not an especially complicated model,
and no doubt there are bugs (though has been used widely for 20
years).

Regarding customisation, in this case it is not a big deal. The way
the code was (arguably poorly) written everything was declared
globally. So in fact by using the .INI file to hold all of them I am
probably saving myself a lot of time and I get away from globals. Most
of those parameters are things I won't change, so I take your point
but I see no issue in having the option frankly (and it is done so
there is no effort now!).

Ordinarily I might do what you said and having method/func calls with
all the params like that, but I felt it was making the code look very
untidy (i.e >5 vars with each call and needing to return > 5 vars). I
then decided it would be better to pass a (single) dictionary or class
object (not sure of correct word here) and I think by referencing it,
e.g. params.light_ext_coeff it is as clear as passing all the values
individually (i.e. you can track where the values came from). I am
happy with this at any rate. Moreover it is what I am used to doing in
C, where I would pass a single structure containing the necessary
values. I also think this is quite standard? I am sure to interface
with scientific libraries (such as GNU GSL) you pass structures in
this way, I was just adopting this method.

The point of this posting was just to ask those that know, whether it
was a bad idea to use the class object in the way I had or was that
OK? And if I should have just used a dictionary, why?

Thanks.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-11 Thread Martin De Kauwe
On Feb 12, 11:13 am, Steven D'Aprano  wrote:
> On Fri, 11 Feb 2011 10:15:27 -0800, Dan Stromberg wrote:
> > I'd use a class rather than a dictionary - because with a class, pylint
> > (and perhaps PyChecker and pyflakes?) should be able to detect typos
> > upfront.  
>
> *Some* typos. Certainly not all.
>
> The more complex the code -- and with 100 or so parameters, this sounds
> pretty damn complex -- there is a non-negligible risk of mistakenly using
> the wrong name. Unless pylint now has a "do what I mean, not what I say"
> mode, it can't save you from typos like this:
>
> params.employerID = 23
> params.employeeID = 42
> # much later
> if params.employeeID == 23:
>     # Oops, I meant employ*er*ID
>     ...
>
> > With a dictionary, typos remain runtime timebombs.
>
> Are your unit tests broken? You should fix that and not rely on just
> pylint to detect bugs. Unit tests will help protect you against many more
> bugs than just typos.
>
> Besides, and I quote...
>
> "I find it amusing when novice programmers believe their main job is
> preventing programs from crashing. ... More experienced programmers
> realize that correct code is great, code that crashes could use
> improvement, but incorrect code that doesn't crash is a horrible
> nightmare." -- Chris Smith
>
> --
> Steven

The 100+ parameters just means "everything" can be adjusted outside of
the code, invariable most of it isn't. I am setting this up with some
bayesian calibration in mind. A lot of those parameters will be
switches as well. I think perhaps I will write a defaults module and
just read a .INI file with the values the user wants changing.
Originally I was just going to read everything in, but maybe this is
better from a usage point of view.

As I said I am happy to consider a dictionary, although some of the
calculations are quite complex and I *think* it is easier to read this
way, rather than with a dictionary. That is purely an opinion, I don't
have a computer science background and so I am asking really, is what
I am doing very bad, and if so why? What do other people do? I have
tried to search a lot on this subject but I find the example not very
specific, (or I am reading the wrong places).

thanks.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-11 Thread Martin De Kauwe
Sorry I should have added a little more example to help with clarity?
So after reading the .INI file I then initialise the objects I
described e.g.

 def initialise_simulation(self):
"""Set the initial conditions.

using values from the .ini value set the C and N pools
and other misc states.

"""
for attr, value in self.initial_state.__dict__.iteritems():
#print attr, value
setattr(self.pools, attr, value)

# maybe need M2_AS_HA here?
self.pools.lai = self.params.sla * self.params.cfrac_dry_mass
self.fluxes.nuptake = 0.0
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-11 Thread Martin De Kauwe
Hi,

yes I read a .INI file using ConfigParser, just similar sections (in
my opinion) to make one object which i can then pass to different
classes. E.G.

class Dict2Obj:
""" Turn a dictionary into an object.

The only purpose of this is that I think it is neater to reference
values
x.soiltemp rather than x['soiltemp'] ;P
"""
def __init__(self, dict):

for k, v in dict.items():
setattr(self, k, v)


class Parser(object):
""" Load up all the initialisation data.

Return the met data as an array, perhaps we should really also
return a
header as well? Return the ini file as various objects.

"""
def __init__(self, ini_fname, met_fname):

self.ini_fname = ini_fname
self.met_fname = met_fname

def load_files(self):

# read the driving data in, this may get more complicated?
met_data = met_data = np.loadtxt(self.met_fname, comments='#')

# read the configuration file into a different dicts, break up
# into model_params, control and state
config = ConfigParser.RawConfigParser()
config.read(self.ini_fname)

# params
model_params = {}
sections =
['water','nitra','soilp','carba','litter','envir','prodn']
for section in sections:
for option in config.options(section):
model_params[option] = config.getfloat(section,
option)

# turn dict into an object
model_params = Dict2Obj(model_params)

# control
control = {}
for option in config.options('control'):
control[option] = config.getint('control', option)

# turn dict into an object
control = Dict2Obj(control)

initial_state = {}
sections = ['cinit','ninit','vinit']
for section in sections:
for option in config.options(section):
initial_state[option] = config.getfloat(section,
option)

# turn dict into objects
initial_state = Dict2Obj(initial_state)

return (initial_state, control, model_params,
met_data)

So I would then pass these objects through my code, for example a
given class would just expect to inherit params perhaps.

class calc_weight(object):

def __init__(self, params):
self.params = params

There are also "states" that evolve through the code which various
methods of a given class change. For example lets call it elephants
weight. So the model will do various things to predict changes in our
elephants weight, so there will be a class to calculate his/her food
intake etc. Using this example the other thing I wanted to do was pass
"states" around like self.elephant_weight. However it occurred to me
if I just used self.elephant_weight for example it would make it
harder to call individual components seperately, so better to stick to
the same method and using an object i reasoned. Hence I started with
my emptydict thing, I hope that helps make it clearer?

class EmptyObject:
pass


# generate some objects to store pools and fluxes...
self.pools = EmptyObject()

# add stuff to it
self.pools.elephant_weight = 12.0

thanks for all of the suggestions it is very insightful
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Class or Dictionary?

2011-02-11 Thread Martin De Kauwe
On Feb 12, 2:40 am, Andrea Crotti  wrote:
> On Feb 11, 3:56 pm, Martin De Kauwe  wrote:
>
> > Hi,
>
> > I have a series of parameter values which i need to pass throughout my
> > code (>100), in C I would use a structure for example. However in
> > python it is not clear to me if it would be better to use a dictionary
> > or build a class object? Personally I think accessing the values is
> > neater (visually) with an object rather than a dictionary, e.g.
>
> > x = params['price_of_cats'] * params['price_of_elephants']
>
> > vs.
>
> > x = params.price_of_cats * params.price_of_elephants
>
> Visually neater is not really a good parameter to judge.

Yes and No. Makes the code easier to read and less picky to type out,
in my opinion! But regarding the "no" that is why I posted the
question

> And in a class you can also overload __getattr__ to get the same
> syntax as a dictionary.
> But (>100) parameters seems really a lot, are you sure you can't split
> in many classes instead?

i have a number some are smaller, for example switch/control flags.
But the rest can be quite large. I can split them but I don't see the
advantage particularly. Currently by using them (e.g.
params.rate_of_decomp) it clearly distinguishes in the code this was a
model parameter read in from a file. I could create more categories
but it just means more names to remember and change in the code.

>
> Classes have also the advantage that you can do many things behind the
> scenes, while with plain dictionaries you can't do much...
> Where do you take those parameters from and what are they used for?

I have a big model which calculates a process, but this is made up of
a number of smaller models. I have coded each of these are separate
classes which expect to receive an object e.g. params, or two objects
e.g. params and switches.

thanks
-- 
http://mail.python.org/mailman/listinfo/python-list


Class or Dictionary?

2011-02-11 Thread Martin De Kauwe
Hi,

I have a series of parameter values which i need to pass throughout my
code (>100), in C I would use a structure for example. However in
python it is not clear to me if it would be better to use a dictionary
or build a class object? Personally I think accessing the values is
neater (visually) with an object rather than a dictionary, e.g.

x = params['price_of_cats'] * params['price_of_elephants']

vs.

x = params.price_of_cats * params.price_of_elephants

So currently I am building a series of class objects to hold different
parameters and then passing these through my code, e.g.

class EmptyObject:
pass

self.animal_prices = EmptyObject()
self.price_of_cats = 12 or reading a file and populating the object


I would be keen to hear any reasons why this is a bad approach (if it
is, I haven't managed to work this out)? Or perhaps there is a better
one?

thanks

Martin
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: code structure advise for a model

2011-02-05 Thread Martin De Kauwe
On Feb 4, 8:41 pm, Marco Nawijn  wrote:
> On Feb 4, 3:43 am, Martin De Kauwe  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


code structure advise for a model

2011-02-03 Thread 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).
-- 
http://mail.python.org/mailman/listinfo/python-list