Hi all, I want to modify the method that set use for see if there is
already an object inside its obj-list. Something like this:
class foo: pass
bar1 = foo()
bar1.attr = 1
bar2 = foo()
bar2.attr = 1
set( (bar1, bar2), key=lambda o: o.attr)
and, of course, set has only one value.
It's possible?
Thanks,
Michele 8 1132
Ciao, Michele:
On Thu, Sep 4, 2008 at 11:48 AM, Michele Petrazzo
<mi**************@togliunipex.itwrote:
Hi all, I want to modify the method that set use for see if there is
already an object inside its obj-list. Something like this:
class foo: pass
bar1 = foo()
bar1.attr = 1
bar2 = foo()
bar2.attr = 1
set( (bar1, bar2), key=lambda o: o.attr)
and, of course, set has only one value.
It's possible?
Thanks,
Michele
-- http://mail.python.org/mailman/listinfo/python-list
looking at the source, maybe you could create a subclass of Set
redefining the __contains__ method?
Regards
Marco
--
Marco Bizzarri http://notenotturne.blogspot.com/ http://iliveinpisa.blogspot.com/
On Thu, Sep 4, 2008 at 11:58 AM, Wojtek Walczak <gm*****@bzt.bztwrote:
On Thu, 04 Sep 2008 11:48:18 +0200, Michele Petrazzo wrote:
>Hi all, I want to modify the method that set use for see if there is already an object inside its obj-list. Something like this:
...
>It's possible?
As far as I understand you, you need descriptors: http://users.rcn.com/python/download/Descriptor.htm
I know descriptors a litte, Wojtek, but didn't use them often; can you
elaborate a little more on your idea?
--
Regards,
Wojtek Walczak, http://tosh.pl/gminick/
-- http://mail.python.org/mailman/listinfo/python-list
Saluti
Marco
--
Marco Bizzarri http://notenotturne.blogspot.com/ http://iliveinpisa.blogspot.com/
Michele Petrazzo wrote:
Hi all, I want to modify the method that set use for see if there is
already an object inside its obj-list. Something like this:
class foo: pass
bar1 = foo()
bar1.attr = 1
bar2 = foo()
bar2.attr = 1
set( (bar1, bar2), key=lambda o: o.attr)
and, of course, set has only one value.
It's possible?
Using a decorator/delegate-pattern, yes:
class Foo(object):
def __init__(self, delegate):
self.delegate = delegate
def __hash__(self):
return hash(self.delegate.attr)
def __cmp__(self, other):
return cmp(self.delegate.attr, other.delegate.attr)
set(Foo(a) for a in bar1, bar2)
Diez
Marco Bizzarri wrote:
looking at the source, maybe you could create a subclass of Set
redefining the __contains__ method?
Made some tries, but __contains__ are never called
>>class foo(set):
.... def __contains__(self, value):
.... print value
....
>>a = foo((1,2))
Thanks,
Michele
Le Thursday 04 September 2008 14:31:23 Michele Petrazzo, vous avez écrit*:
Marco Bizzarri wrote:
looking at the source, maybe you could create a subclass of Set
redefining the __contains__ method?
Made some tries, but __contains__ are never called
No, __contains__ is only called with "in" operator, not for internal hashing.
Anyway this solution is bad, you'll need to compare the new element with all
the set contain, which would result in a O(n) algorithm for adding elements
to the set in place of the O(1) it use.
The right way to go is one like Diez show you in a previous post.
*>>class foo(set):
... *def __contains__(self, value):
... * print value
...
*>>a = foo((1,2))
*>>>
--
_____________
Maric Michaud
On Thu, Sep 4, 2008 at 3:07 PM, Maric Michaud <ma***@aristote.infowrote:
Le Thursday 04 September 2008 14:31:23 Michele Petrazzo, vous avez écrit :
>Marco Bizzarri wrote:
looking at the source, maybe you could create a subclass of Set
redefining the __contains__ method?
Made some tries, but __contains__ are never called
No, __contains__ is only called with "in" operator, not for internal hashing.
Anyway this solution is bad, you'll need to compare the new element with all
the set contain, which would result in a O(n) algorithm for adding elements
to the set in place of the O(1) it use.
Thanks for the clarification, Maric; I take notices to watch source
more closely next time (( hopefully, before writing a wrong answer )).
Regards
Marco
_____________
Maric Michaud
-- http://mail.python.org/mailman/listinfo/python-list
--
Marco Bizzarri http://notenotturne.blogspot.com/ http://iliveinpisa.blogspot.com/
On Thu, 4 Sep 2008 12:06:14 +0200, Marco Bizzarri wrote:
>As far as I understand you, you need descriptors: http://users.rcn.com/python/download/Descriptor.htm
I know descriptors a litte, Wojtek, but didn't use them often; can you
elaborate a little more on your idea?
Marco, I think that I misunderstood the OP, but I was thinking
about immutable (or set-once) attributes. Something like this:
---
class SetOnce(object):
def __init__(self):
self._attr1 = ""
self._attr1flag = False
def setatt(self, x):
if self._attr1flag:
raise ValueError("attribute already set")
self._attr1flag = True
self._attr1 = x
def getatt(self):
return self._attr1
attr1 = property(getatt, setatt)
a = SetOnce()
a.attr1 = 1
print a.attr1
try:
a.attr1 = 2
except ValueError:
print a.attr1
---
$ python immutattr.py
1
1
$
--
Regards,
Wojtek Walczak, http://tosh.pl/gminick/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Gord |
last post by:
Hello,
If you set the flag for an overwrite prompt using the 'Save' common dialog,
how do you read the response when the user clicks the Yes or No in the
'overwrite' message box?
Everything...
|
by: Vishal Grover |
last post by:
Hello Everyone,
I am seeing a certain behaviour which I find strange, and am curious to get an
explanation to it. I have the following program.
#include <iostream>
#include <cstdlib>
using...
|
by: niko |
last post by:
Hi
I would like to overwrite, deploy our ADE file on every client pc.
Unfortunately sometime the application is still open at the client and
the copy prossess fails due to a lock.
Do you know a...
|
by: Ramaraj M Bijur |
last post by:
Hi All,
Could anyone help me to resolve following problem in C the IDE used is
Microsoft VC++,
Please let me know the which option in VC++ will do the needful
The problem statement:...
|
by: B-Dog |
last post by:
Is there a way to make vb.net to overwrite the file when moving? Here is
what I'm trying to do:
If System.IO.File.Exists(dest) Then
'handle overwrite here
If MessageBox.Show("Do you want...
|
by: Developer |
last post by:
I use the RichTextBox and sometimes put it into overwrite mode.
I'd like the cursor to show whether its in overwrite or insert mode.
I often use
Cursor.Current = Cursors.WaitCursor
and...
|
by: Björn Langhof |
last post by:
Hello.
I'd like to overwrite a native function. It's the reload()-function of
the document.location-object. My idea:
document.location.reload = function(){alert('foo')};
So if the function...
|
by: Kai Kuehne |
last post by:
Hi list!
It is possible to overwrite only one function with the property-function?
x = property(getx, setx, delx, 'doc')
I just want to overwrite setx, but when I set the others to None,
I...
|
by: martinezfive |
last post by:
Hi,
I feel no doubt some documentation contains my answer, so bare with
me. Given the following:
#inclde <stdio.h>
#include <sstream>
void f()
{
std::stringstream a("Hello World!\n");
|
by: hzgt9b |
last post by:
I know how to overwrite a function. Normally this is what I would do:
function someFunction() { /* orig definition here */ }
//later in the execution stream I would do...
someFunction = function...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: lllomh |
last post by:
How does React native implement an English player?
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |