Re: Bounds checking

2011-03-21 Thread Jean-Michel Pichavant
Martin De Kauwe wrote: 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 ev

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

Re: Bounds checking

2011-03-21 Thread Jean-Michel Pichavant
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 teste

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

Re: Bounds checking

2011-03-19 Thread Steven D'Aprano
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 rea

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 wo

Re: Bounds checking

2011-03-19 Thread Martin De Kauwe
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

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

Re: Bounds checking

2011-03-19 Thread Steven D'Aprano
On Fri, 18 Mar 2011 15:35:40 -0700, Martin De Kauwe wrote: >> 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. Sorry, are you trying

Re: Bounds checking

2011-03-19 Thread Steven D'Aprano
;s probably not wise. At the very least, dir() will be a fairly expensive call. 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.

Re: Bounds checking

2011-03-18 Thread Terry Reedy
On 3/18/2011 10:24 AM, Martin De Kauwe wrote: 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) dir() has to do a bit a com

Re: Bounds checking

2011-03-18 Thread Dan Stromberg
Actually, I'd probably create a class with 3 arguments - an initial value, a lower bound, and an upper bound, give it a _check method, and call _check from the various operator methods. The class would otherwise impersonate an int. In code that isn't performance-critical, it's better to check for

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.

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 a

Re: Bounds checking

2011-03-18 Thread Katie T
On Fri, Mar 18, 2011 at 3:01 PM, Jean-Michel Pichavant < jeanmic...@sequans.com> wrote: > > Don't check for bounds, fix any bug in the code that would set your values > out of bounds and use asserts while debugging. > > Otherwise if you really need dynamic checks, it will cost you cpu, for > sure.

Re: Bounds checking

2011-03-18 Thread Miki Tebeka
> 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() Not that related to the question. But it's usua

Re: Bounds checking

2011-03-18 Thread Mel
Jean-Michel Pichavant wrote: > Martin De Kauwe wrote: > Don't check for bounds, fix any bug in the code that would set your > values out of bounds and use asserts while debugging. [ ... ] > def __setattr__(self, attribute, value): >if not self.funcTable.get(attribute, lambda x: True)(v

Re: Bounds checking

2011-03-18 Thread Jean-Michel Pichavant
Martin De Kauwe wrote: 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 e

Re: Bounds checking

2011-03-18 Thread Katie T
What sort of checks are you making ? - in general greater than/less than tend to be fairly optimal, although you might be able to do a faster "is negative" test Katie On Fri, Mar 18, 2011 at 2:24 PM, Martin De Kauwe wrote: > Hi, > > if one has a set of values which should never step outside ce

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