I'd like to implement an object that represents a circular counter, i.e.
an integer that returns to zero when it goes over it's maxVal.
This counter has a particular behavior in comparison: if I compare two of
them an they differ less than half of maxVal I want that, for example,
0 maxVal gives true.
This is because my different counters grew together and they never
differ a lot between them and so a value of 0 compared with an other of
maxVal means that the previous one just made its increment before the
other one.
The python problem that I give you it's about style.
I'd like to write in my code something that looks almost like using an
integer object.
I mean, I'd like to write:
cnt = CircularConter(maxVal=100, initialVal=10)
cnt += 100 # cnt value is 9
print cnt # prints 9
100 cnt # is false
cnt = 100 # cnt new value is 100 [NOT rebind cnt with 100]
Until now I used this class:
class CircularConter:
def __init__(self, maxVal, initialVal=0):
self.maxVal = maxVal
self.val = None
self.set( initialVal )
def __add__(self, increment):
self.set( self.val + increment )
return self
def __cmp__(self, operand):
return cmp(self.maxVal/2, abs(operand - self.val)) * cmp(self.val,
operand)
def __repr__(self):
return str(self.val)
def __str__(self):
return str(self.val)
def set(self, val):
if val self.maxVal: self.val = val-self.maxVal-1
else: self.val = val
.... and so my real writing was:
cnt = CircularConter(maxVal=100, initialVal=10)
cnt += 100
print cnt
100 cnt # is false
cnt.set(100)
The fact is that I don't like to write cnt.set(100) or
cnt = CircularConter(100, 100) instead of cnt = 100.
So I thought that property or descriptors could be useful.
I was even glad to write:
cnt = CircularConterWithProperty(maxVal=100, initialVal=10)
cnt.val += 100
print cnt.val
100 cnt.val # is false
cnt.val = 100
just to give uniformity to counter accessing syntax.
But I wasn't able to implement nothing working with my __cmp__ method.
I'll post one of mine NOT WORKING implementation.
class pro(object):
def __init__(self, maxVal, val):
self._maxVal = maxVal
self._val = val
def getval(self):
return self._val
def setval(self, val):
if val self._maxVal: self._val = val-self._maxVal-1
else: self._val = val
val = property(getval, setval)
class CircularConterWithProperty(pro):
def __init__(self, maxVal, val=0):
super(CircularConterWithProperty, self).__init__( maxVal, val)
def __cmp__(self, operand):
return cmp(self.maxVal/2, abs(operand - self.val)) * cmp(self.val,
operand)
__ I know why this doesn't work. __
__ What I don't know __ is if there is a way to write a class that allows
my desire of uniform syntax or if IT IS JUST A NON SENSE.
I'll thank in advance for any answer.
Saluti a tutti
Licia 2 2316
On Sun, 08 Oct 2006 12:25:10 +0200, IloChab wrote:
I'd like to implement an object that represents a circular counter, i.e.
an integer that returns to zero when it goes over it's maxVal.
[snip]
The python problem that I give you it's about style.
I'd like to write in my code something that looks almost like using an
integer object.
I mean, I'd like to write:
cnt = CircularConter(maxVal=100, initialVal=10)
cnt += 100 # cnt value is 9
print cnt # prints 9
100 cnt # is false
All this is perfectly sensible.
cnt = 100 # cnt new value is 100 [NOT rebind cnt with 100]
This is not possible. Names like cnt are just labels, they don't have
behaviour. Objects have behaviour; but objects don't and can't know what
name or names they are assigned to.
[snip]
The fact is that I don't like to write cnt.set(100) or
cnt = CircularConter(100, 100) instead of cnt = 100.
How do you expect the Python compiler to know you want a CircularConter
instance if you just write 100?
(By the way, in English, the correct spelling is counter, not conter.)
So I thought that property or descriptors could be useful.
I was even glad to write:
cnt = CircularConterWithProperty(maxVal=100, initialVal=10)
cnt.val += 100
print cnt.val
100 cnt.val # is false
cnt.val = 100
just to give uniformity to counter accessing syntax.
But I wasn't able to implement nothing working with my __cmp__ method.
__cmp__ is for CoMParisons, not binding names to objects.
--
Steven.
IloChab wrote:
I'd like to implement an object that represents a circular counter, i.e.
an integer that returns to zero when it goes over it's maxVal.
This counter has a particular behavior in comparison: if I compare two of
them an they differ less than half of maxVal I want that, for example,
0 maxVal gives true.
This is because my different counters grew together and they never
differ a lot between them and so a value of 0 compared with an other of
maxVal means that the previous one just made its increment before the
other one.
The python problem that I give you it's about style.
I'd like to write in my code something that looks almost like using an
integer object.
I mean, I'd like to write:
cnt = CircularConter(maxVal=100, initialVal=10)
cnt += 100 # cnt value is 9
print cnt # prints 9
100 cnt # is false
cnt = 100 # cnt new value is 100 [NOT rebind cnt with 100]
[...]
... and so my real writing was:
cnt = CircularConter(maxVal=100, initialVal=10)
cnt += 100
print cnt
100 cnt # is false
cnt.set(100)
The fact is that I don't like to write cnt.set(100) or
cnt = CircularConter(100, 100) instead of cnt = 100.
So I thought that property or descriptors could be useful.
I was even glad to write:
cnt = CircularConterWithProperty(maxVal=100, initialVal=10)
cnt.val += 100
print cnt.val
100 cnt.val # is false
cnt.val = 100
just to give uniformity to counter accessing syntax.
But I wasn't able to implement nothing working with my __cmp__ method.
[...]
>
__ What I don't know __ is if there is a way to write a class that allows
my desire of uniform syntax or if IT IS JUST A NON SENSE.
I'll thank in advance for any answer.
Saluti a tutti
Licia
As Steven said, it's not possible to do what you want. I don't think
there's any way around either
cnt.val = 100
or
cnt.setval(100)
Here's an iterator version of your Counter class:
class Counter(object):
def __init__(self, maxval, initval=0):
self.maxval = maxval
self.val = initval
def __iter__(self):
return self
def next(self):
ret = self.val
self.__add__(1)
return ret
def __add__(self, increment):
self.val = (self.val + increment) % self.maxval
return self
def __sub__(self, decrement):
self.val = (self.val - decrement) % self.maxval
return self
def __cmp__(self, operand):
return cmp(self.maxval/2, abs(operand - self.val)) *
cmp(self.val,operand)
def __repr__(self):
return str(self.val)
def __str__(self):
return str(self.val)
cnt = Counter(10)
print cnt
cnt += 23
print cnt, cnt 5
cnt.val = 7
print
print cnt, cnt 5
cnt -= 65
print
print cnt, cnt 5
print
print zip(cnt, ['a', 'b', 'c', 'd'])
-----------------------
Gerard This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Thomas Heller |
last post by:
I'm trying to implement __iter__ on an abstract base class while I don't
know whether subclasses support that or not.
Hope that makes sense, if not, this code should be clearer:
class Base:
def...
|
by: Paul |
last post by:
OK...I must be missing something...can someone tell me what I'm not doing properly...
First, create an assembly with a single interface in VB.NET as follow
Public Interface IDo
Function...
|
by: Vera |
last post by:
I have two assemblies that each consist of several classes.
Each object instantiated from those classes can have one or more child- and/or parentobjects that are also instantiated from those...
|
by: william |
last post by:
Hi,
I'm playing windows service using vb.net. I added a performance counter to my windows service, but I couldn't set MachineName property of the performance counter to blank. After I set it to...
|
by: Stephen Robertson |
last post by:
We are currently in a dead end with a circular reference issue using
vb.net, and are hoping someone might help us resolve it.
Idea...
We have frmmain calling frmperson (dim f as new frmperson)...
|
by: avsrk |
last post by:
Hi Folks
I want to create a circular queue for high speed data acquistion on
QNX with GNU C++ .
Does any one know of efficient ways either Using STL or in any other
way .
I am thing of ...
|
by: Michal Kwiatkowski |
last post by:
Hi,
Code below shows that property() works only if you use it within a class.
------------------------------------------------
class A(object):
pass
a = A()
a.y = 7
|
by: randy1200 |
last post by:
In SQL Reporting Services, there's a circular green progress bar. Looks just
like the regular progress bar in the toolbar, except that it's a circle. It's
perfect for when a Win Form client needs...
|
by: 7stud |
last post by:
Hi,
Can someone show me how to manually implement staticmethod()? Here is
my latest attempt:
----------------
def smethod(func):
def newFunc():
pass
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |