Re: Storing value with limits in object

2008-06-26 Thread Josip
Thanks alot. I'm going to use this with few modifications to tailor it to my needs. Thumbs up! > #!/usr/bin/env python > > ## VERSION 2 > ## > ## changelog: > ## - Uses inheritance from _Limited > ## - Added _LimitedLong and llong > ## - limit choose between int, long, and float > > class _Limite

Re: Storing value with limits in object

2008-06-23 Thread Lie
#!/usr/bin/env python ## VERSION 2 ## ## changelog: ## - Uses inheritance from _Limited ## - Added _LimitedLong and llong ## - limit choose between int, long, and float class _Limited(object): def setlimits(self, lim): ''' Set the limits and if value is not within limit, r

Re: Storing value with limits in object

2008-06-23 Thread Lie
On Jun 23, 1:24 am, "Josip" <[EMAIL PROTECTED]> wrote: > > Why not make it a function? > > > function assignLimited(value, vmin, vmax): > >     value = max(vmin, value) > >     value = min(vmax, value) > >     return value > > > a = assignLimited(7, 0, 10) > > > Seems like it solves your problem re

Re: Storing value with limits in object

2008-06-23 Thread Lie
On Jun 23, 1:24 am, "Josip" <[EMAIL PROTECTED]> wrote: > > Why not make it a function? > > > function assignLimited(value, vmin, vmax): > >     value = max(vmin, value) > >     value = min(vmax, value) > >     return value > > > a = assignLimited(7, 0, 10) > > > Seems like it solves your problem re

Re: Storing value with limits in object

2008-06-22 Thread Josip
> I bet you didn't even try this, unless your definition of "works" > includes a "RuntimeError: maximum recursion depth exceeded". Here's a > a working version: Actually, the version I'm using is somewhat bigger. I removed docstrings and recklessly stripped away some methods to make it shorter con

Re: Storing value with limits in object

2008-06-22 Thread Josip
> Why not make it a function? > > function assignLimited(value, vmin, vmax): > value = max(vmin, value) > value = min(vmax, value) > return value > > > a = assignLimited(7, 0, 10) > > > Seems like it solves your problem relatively cleanly. > Note: I also removed min/max variables becaus

Re: Storing value with limits in object

2008-06-22 Thread Josip
> Why not make it a function? > > function assignLimited(value, vmin, vmax): > value = max(vmin, value) > value = min(vmax, value) > return value > > > a = assignLimited(7, 0, 10) > > > Seems like it solves your problem relatively cleanly. > Note: I also removed min/max variables becaus

Re: Storing value with limits in object

2008-06-22 Thread Larry Bates
Josip wrote: I'm trying to limit a value stored by object (either int or float): class Limited(object): def __init__(self, value, min, max): self.min, self.max = min, max self.n = value def set_n(self,value): if value < self.min: # boundary check self.

Re: Storing value with limits in object

2008-06-22 Thread George Sakkis
On Jun 22, 5:44 am, "Josip" <[EMAIL PROTECTED]> wrote: > I'm trying to limit a value stored by object (either int or float): > > class Limited(object): > def __init__(self, value, min, max): > self.min, self.max = min, max > self.n = value > def set_n(self,value): >

Re: Storing value with limits in object

2008-06-22 Thread Josip
> In theory you could hack Python's internal locals or globals > dictionary so that it did something unusual while looking up your > object. But in practice this doesn't work, because the returned > objects (when you call globals() or locals()) attributes are readonly. > Probably because those inte

Re: Storing value with limits in object

2008-06-22 Thread David
On Sun, Jun 22, 2008 at 12:24 PM, Josip <[EMAIL PROTECTED]> wrote: >> Not with normal vars, because = is a rebinding operator in Python, >> rather than assignment. >> >> You can do (close to) the above with object properties. >> >> David. > > Yes, but it's done with built-in types like int and floa

Re: Storing value with limits in object

2008-06-22 Thread Josip
> Not with normal vars, because = is a rebinding operator in Python, > rather than assignment. > > You can do (close to) the above with object properties. > > David. Yes, but it's done with built-in types like int and float. I suspect I could subclass from them and implement limits, but I would h

Re: Storing value with limits in object

2008-06-22 Thread David
On Sun, Jun 22, 2008 at 11:44 AM, Josip <[EMAIL PROTECTED]> wrote: > I'm trying to limit a value stored by object (either int or float): > > class Limited(object): >def __init__(self, value, min, max): >self.min, self.max = min, max >self.n = value >def set_n(self,value): >