How to override the doc of an object instance.

2006-06-21 Thread David Huard
Hi, 

I'm not really sure about the right terminology, but here is my question, boiled
down to this code:

class widget (object):
"""This is a widget."""
def  __init__(self):
self._x = None
def fget(self):
return self._x
def fset(self, value):
self._x = value
print self._x, 'Ok'
x = property(fget = fget, fset = fset, doc= "It prints")


print widget.x.__doc__

w = widget()
w.x = 5
print w.x.__doc__

I would like the last statement to print the doc string that I specified in
property, instead of the docstring for an int. The goal is to have ipython print
that string with the command 
w.x? 
So the user knows what this attribute does, and how he can set it. 

Is this possible ? 

Thanks, 

David Huard

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to override the doc of an object instance.

2006-06-21 Thread David Huard
On Wed, 21 Jun 2006 15:39:02 +0200, Maric Michaud wrote:

> This is w.__class__.x.__doc__.

Thanks, 

So in order to implement what I want, I should rather consider an
ipython hack to print w.__class__.x.__doc__ when it exists, instead of
w.x.__doc_ ? Does this makes sense or it will ruin the standard behaviour?

David

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to override the doc of an object instance.

2006-06-21 Thread David Huard
Paul, 

Although your solution works for the class itself, it doesn't for class
attributes, since they point to built-ins whose attributes are read-only. 

>>> w.x.__doc__ = widget.x.__doc__
AttributeError: 'int' object attribute '__doc__' is read-only

Would the solution be to build a new type and change its dict ?
 
David

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to override the doc of an object instance.

2006-06-21 Thread David Huard
On Wed, 21 Jun 2006 17:15:16 +0200, Maric Michaud wrote:
> 
> In [53]: class a(object) :
>: x=property(lambda s: 0, doc='my doc string')
>:
>:
> 
> In [54]: b=a()
> 
> In [55]: help(b)

I agree it works, but for a class with tens of attributes, this is not
very practical. 


> Also you can do something like that (put it in some startup script) :
> 
> 
> In [27]: __IPYTHON__.old_pinfo = __IPYTHON__.magic_pinfo
> 
> In [28]: def new_pinfo(obj) :
>: return __IPYTHON__.old_pinfo('modified_version_of_obj')
>:
> 
> In [29]: __IPYTHON__.magic_pinfo = new_pinfo
> 
> But you can also send a bug report to Ipython maintainer :)

I looked into the internals of IPython and I can't say I understood much... 
I think I'll follow your advice. 

Has this problem come up before ? It seems that with the new classes, this
kind of wish will generalize, or is it a bad coding habit to embed
objects inside classes ? 

Thanks
David


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to override the doc of an object instance.

2006-06-21 Thread David Huard
It works !

Wow. Thanks a lot. If you don't mind, I'll post your code to the ipython
list so it can be reused.

David



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Programmatically exit the REPL

2008-09-16 Thread David Huard

> [snip]
> 
> Does anyone know how to make raw_input think it has gotten input?
> 
> -Matt

Hi Matt, 

So you really need raw_input ? Couldn't you use a mock-up ?

sys.stdout.write('> ')
sys.stdout.flush()

And get the user input with something like: 

while self.continue:
  input = os.read(sys.stdin.fileno(), 80).strip()
  if input== '':
time.sleep(.2)

The background thread can stop the listening thread by setting 
self.continue to False. 

The subversion trunk of pymc (on google code) has something that I think 
is similar to your problem, you might want to look at it (look at 
revision 868). 

HTH,

David Huard


--
http://mail.python.org/mailman/listinfo/python-list