[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

What do Python IDEs use for the member drop-down?

tbrianedgar

2/8/2008 6:29:00 AM

I realize in the new style, getattr and setattr are supposed to
reference something in a base class, but here is what I'm trying to
do:

class tryit:
def __init__(self, a, b):
self.__dict__["a"] = a
self.__dict__["b"] = b
def __dir__(self):
return [ "geta", "getb" ]
def __getattr__(self, attr):
if attr == "geta":
return self.__dict__["a"]
elif attr == "getb":
return self.__dict__["b"]
else:
raise AttributeError

x = tryit(1, 2)

So, I'm OK with the fact that x.a, x.b, x.geta, and x.getb all work (I
don't care too much about hiding a and b). What I would like is for
the Python IDE drop-down to include geta and getb as choices, which I
thought would be accomplished by overloading __dir__. Am I just using
the wrong IDE (tried PythonWin from ActivePython so far)? Do I need
to overload something else?

Whenever people ask me for help, I usually want to know "so what are
you trying to do?" Here goes. I'm trying to create a base C struct
class where the creator of the derived class can provide a simple "C
struct" definition, and the necessary information will be loaded into
class instances. Something like this totaly contrived example:

class file_header(c_struct):
definition_text = """
typedef struct file_header // little endian align 32
error_if_pad
{
uint32 LengthInBytes;
uint8 Revision;
uint8 Reserved;
uint16 Encoding;
} file_header; """
def __init__(self, BinaryData = ""):
c_struct.__init__(self, BinaryData)

Header = file_header( File.read(sizeof(file_header)) )
if Header.LengthInBytes > 9: # The IDE gives me my members as
choices!!
HappyWithLength()
else:
Header.LengthInBytes = 9 # Again, nice drop-down from IDE
Header.Encoding = NewEncoding( File )
Header.GUITreeControlEdit()
OutFile.write( Header.GetBinaryData() )

Thanks for any suggestions!

-Brian
1 Answer

Larry Bates

2/8/2008 3:19:00 PM

0

tbrianedgar@yahoo.com wrote:
> I realize in the new style, getattr and setattr are supposed to
> reference something in a base class, but here is what I'm trying to
> do:
>
> class tryit:
> def __init__(self, a, b):
> self.__dict__["a"] = a
> self.__dict__["b"] = b
> def __dir__(self):
> return [ "geta", "getb" ]
> def __getattr__(self, attr):
> if attr == "geta":
> return self.__dict__["a"]
> elif attr == "getb":
> return self.__dict__["b"]
> else:
> raise AttributeError
>
> x = tryit(1, 2)
>
> So, I'm OK with the fact that x.a, x.b, x.geta, and x.getb all work (I
> don't care too much about hiding a and b). What I would like is for
> the Python IDE drop-down to include geta and getb as choices, which I
> thought would be accomplished by overloading __dir__. Am I just using
> the wrong IDE (tried PythonWin from ActivePython so far)? Do I need
> to overload something else?
>
> Whenever people ask me for help, I usually want to know "so what are
> you trying to do?" Here goes. I'm trying to create a base C struct
> class where the creator of the derived class can provide a simple "C
> struct" definition, and the necessary information will be loaded into
> class instances. Something like this totaly contrived example:
>
> class file_header(c_struct):
> definition_text = """
> typedef struct file_header // little endian align 32
> error_if_pad
> {
> uint32 LengthInBytes;
> uint8 Revision;
> uint8 Reserved;
> uint16 Encoding;
> } file_header; """
> def __init__(self, BinaryData = ""):
> c_struct.__init__(self, BinaryData)
>
> Header = file_header( File.read(sizeof(file_header)) )
> if Header.LengthInBytes > 9: # The IDE gives me my members as
> choices!!
> HappyWithLength()
> else:
> Header.LengthInBytes = 9 # Again, nice drop-down from IDE
> Header.Encoding = NewEncoding( File )
> Header.GUITreeControlEdit()
> OutFile.write( Header.GetBinaryData() )
>
> Thanks for any suggestions!
>
> -Brian

> class tryit:
> def __init__(self, a, b):
> self.__dict__["a"] = a
> self.__dict__["b"] = b
> def __dir__(self):
> return [ "geta", "getb" ]
> def __getattr__(self, attr):
> if attr == "geta":
> return self.__dict__["a"]
> elif attr == "getb":
> return self.__dict__["b"]
> else:
> raise AttributeError

I "think" I understand what you want. Try this:

class tryit(object):
def __init__(self, a, b):
self.__a = a
self.__b = b

def __ga(self):
return self.__a

def __gb(selt):
return self.__b

geta=property(__ga, 'ga property')
getb=property(__gb, 'gb property')

-Larry