Re: dynamically modify help text

2010-06-30 Thread Aahz
In article mailman.2338.1277812368.32709.python-l...@python.org,
Thomas Jollans  tho...@jollans.com wrote:

% python2.6
Python 2.6.5+ (release26-maint, Jun 28 2010, 19:46:36)
[GCC 4.4.4] on linux2
Type help, copyright, credits or license for more information.
 class OLD: pass
...
 class NEW(object): pass
...
 OLD.__doc__ = foo
 NEW.__doc__ = bar
Traceback (most recent call last):
  File stdin, line 1, in module
AttributeError: attribute '__doc__' of 'type' objects is not writable


I'd argue that's a bug -- feel free to file one.  I think this might
even be fixable in 2.7.1.
-- 
Aahz (a...@pythoncraft.com)   * http://www.pythoncraft.com/

If you don't know what your program is supposed to do, you'd better not
start writing it.  --Dijkstra
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dynamically modify help text

2010-06-29 Thread Thomas Jollans
On 06/29/2010 02:37 AM, Ben Finney wrote:
 Brian Blais bbl...@bryant.edu writes:
 
 On Jun 28, 2010, at 14:25 , Chris Rebert wrote:
 __doc__ is normally defined on classes, e.g. `A`, not instances,
 e.g. `a`. help() looks for __doc__ accordingly.

 so that gets back to my original question: can I change this text at
 runtime.  Doesn't look like I can, because it is defined for classes
 rather than instances.  Am I thinking about this correctly?
 
 Classes are objects. You can change the ‘__doc__’ attribute of a class
 object the same as you'd change it for any other object::
 
 A.__doc__ = new docstring
 

No, you can't. Well, yeah, you can. But you can't. But you can. Ahrgh

I want Python 2.x to go away. It's so inconsistent and silly.

% python2.6
Python 2.6.5+ (release26-maint, Jun 28 2010, 19:46:36)
[GCC 4.4.4] on linux2
Type help, copyright, credits or license for more information.
 class OLD: pass
...
 class NEW(object): pass
...
 OLD.__doc__ = foo
 NEW.__doc__ = bar
Traceback (most recent call last):
  File stdin, line 1, in module
AttributeError: attribute '__doc__' of 'type' objects is not writable


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


Re: dynamically modify help text

2010-06-28 Thread Brian Blais


On Jun 27, 2010, at 22:37 , Red Forks wrote:

Read you doc file and set the __doc__ attr of the object you want  
to change.


On Monday, June 28, 2010, Brian Blais bbl...@bryant.edu wrote:
I know that the help text for an object will give a description of  
every method based on the doc string.  Is there a way to add  
something to this text, specific to an object, but generated at  
run-time?  I have an object that reads a file, and I would like  
part of that file to be shown if one does:  help(myobject)





python file:

#=
class A(object):
pass

help_text=this is some text

a=A()
a.__doc__=help_text
#=






in ipython:

shows up here:

In [5]:a?
Type:   A
Base Class: class '__main__.A'
String Form:__main__.A object at 0x13270f0
Namespace:  Interactive
File:   /Library/Frameworks/Python.framework/Versions/5.0.0/ 
lib/python2.5/site-packages/IPython/FakeModule.py

Docstring:
this is some text


but not with the help command:

In [6]:help(a)
Help on A in module __main__ object:

class A(__builtin__.object)
 |  Data descriptors defined here:
 |
 |  __dict__
 |  dictionary for instance variables (if defined)
 |
 |  __weakref__
 |  list of weak references to the object (if defined)


also does the same thing with the regular python prompt.

is there a reason for this?


thanks,

Brian Blais

--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais
http://bblais.blogspot.com/



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


Re: dynamically modify help text

2010-06-28 Thread Chris Rebert
On Mon, Jun 28, 2010 at 11:13 AM, Brian Blais bbl...@bryant.edu wrote:
 On Jun 27, 2010, at 22:37 , Red Forks wrote:
 Read you doc file and set the __doc__ attr of the object you want to
 change.

 On Monday, June 28, 2010, Brian Blais bbl...@bryant.edu wrote:
 I know that the help text for an object will give a description of every
 method based on the doc string.  Is there a way to add something to this
 text, specific to an object, but generated at run-time?  I have an object
 that reads a file, and I would like part of that file to be shown if one
 does:  help(myobject)

 python file:

 #=
 class A(object):
    pass

 help_text=this is some text

 a=A()
 a.__doc__=help_text
 #=

 in ipython:

 shows up here:

 In [5]:a?
 Type:           A
 Base Class:     class '__main__.A'
 String Form:    __main__.A object at 0x13270f0
 Namespace:      Interactive
 File:
 /Library/Frameworks/Python.framework/Versions/5.0.0/lib/python2.5/site-packages/IPython/FakeModule.py
 Docstring:
    this is some text


 but not with the help command:

 In [6]:help(a)
 Help on A in module __main__ object:

 class A(__builtin__.object)
  |  Data descriptors defined here:
  |
  |  __dict__
  |      dictionary for instance variables (if defined)
  |
  |  __weakref__
  |      list of weak references to the object (if defined)


 also does the same thing with the regular python prompt.

 is there a reason for this?

__doc__ is normally defined on classes, e.g. `A`, not instances, e.g.
`a`. help() looks for __doc__ accordingly.

Cheers,
Chris
--
http://blog.rebertia.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dynamically modify help text

2010-06-28 Thread Benjamin Kaplan
On Mon, Jun 28, 2010 at 11:25 AM, Chris Rebert c...@rebertia.com wrote:
 On Mon, Jun 28, 2010 at 11:13 AM, Brian Blais bbl...@bryant.edu wrote:
 On Jun 27, 2010, at 22:37 , Red Forks wrote:
 Read you doc file and set the __doc__ attr of the object you want to
 change.

 On Monday, June 28, 2010, Brian Blais bbl...@bryant.edu wrote:
 I know that the help text for an object will give a description of every
 method based on the doc string.  Is there a way to add something to this
 text, specific to an object, but generated at run-time?  I have an object
 that reads a file, and I would like part of that file to be shown if one
 does:  help(myobject)

 python file:

 #=
 class A(object):
    pass

 help_text=this is some text

 a=A()
 a.__doc__=help_text
 #=

 in ipython:

 shows up here:

 In [5]:a?
 Type:           A
 Base Class:     class '__main__.A'
 String Form:    __main__.A object at 0x13270f0
 Namespace:      Interactive
 File:
 /Library/Frameworks/Python.framework/Versions/5.0.0/lib/python2.5/site-packages/IPython/FakeModule.py
 Docstring:
    this is some text


 but not with the help command:

 In [6]:help(a)
 Help on A in module __main__ object:

 class A(__builtin__.object)
  |  Data descriptors defined here:
  |
  |  __dict__
  |      dictionary for instance variables (if defined)
  |
  |  __weakref__
  |      list of weak references to the object (if defined)


 also does the same thing with the regular python prompt.

 is there a reason for this?

 __doc__ is normally defined on classes, e.g. `A`, not instances, e.g.
 `a`. help() looks for __doc__ accordingly.

 Cheers,
 Chris
 --


Just to save the OP some trouble later on: this optimization is done
for most of the __*__ methods. Overriding __add__ on an instance won't
change the behavior of a + b.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dynamically modify help text

2010-06-28 Thread Emile van Sebille

On 6/28/2010 12:02 PM Benjamin Kaplan said...

Just to save the OP some trouble later on: this optimization is done
for most of the __*__ methods. Overriding __add__ on an instance won't
change the behavior of a + b.


ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)] on 
win32

Type help, copyright, credits or license for more information.
 class Test: pass
...
 i = Test()
 i+1
Traceback (most recent call last):
  File stdin, line 1, in ?
TypeError: unsupported operand type(s) for +: 'instance' and 'int'
 def __add__(self): return 6
...
 i.__add__ = __add__
 i+1
6


Was this in reference to a specific python version?

Emile

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


Re: dynamically modify help text

2010-06-28 Thread Chris Kaynor
On Mon, Jun 28, 2010 at 1:13 PM, Emile van Sebille em...@fenx.com wrote:

 On 6/28/2010 12:02 PM Benjamin Kaplan said...

  Just to save the OP some trouble later on: this optimization is done
 for most of the __*__ methods. Overriding __add__ on an instance won't
 change the behavior of a + b.


 ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
 Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)] on
 win32
 Type help, copyright, credits or license for more information.
  class Test: pass
 ...
  i = Test()
  i+1
 Traceback (most recent call last):
  File stdin, line 1, in ?
 TypeError: unsupported operand type(s) for +: 'instance' and 'int'
  def __add__(self): return 6
 ...
  i.__add__ = __add__
  i+1
 6
 

 Was this in reference to a specific python version?


That limitation only applies to new-style classes. (The stuff below is from
Python 2.6x86 on Windows, but should apply to your 2.4.1 as well).

 class Test(object): pass
...
 i = Test()
 i + 1
Traceback (most recent call last):
  File stdin-inspect, line 1, in module
TypeError: unsupported operand type(s) for +: 'Test' and 'int'
 def __add__(self): return 6
...
 i.__add__ = __add__
 i + 1
Traceback (most recent call last):
  File stdin-inspect, line 1, in module
TypeError: unsupported operand type(s) for +: 'Test' and 'int'


Chris


 Emile


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

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


Re: dynamically modify help text

2010-06-28 Thread Thomas Jollans
On 06/28/2010 10:13 PM, Emile van Sebille wrote:
 On 6/28/2010 12:02 PM Benjamin Kaplan said...
 Just to save the OP some trouble later on: this optimization is done
 for most of the __*__ methods. Overriding __add__ on an instance won't
 change the behavior of a + b.
 
 ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
 Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)] on
 win32
 Type help, copyright, credits or license for more information.
 class Test: pass
 ...
 i = Test()
 i+1
 Traceback (most recent call last):
   File stdin, line 1, in ?
 TypeError: unsupported operand type(s) for +: 'instance' and 'int'
 def __add__(self): return 6
 ...
 i.__add__ = __add__
 i+1
 6

 
 Was this in reference to a specific python version?

ahem


Python 2.5.5 (r255:77872, Apr 21 2010, 08:40:04)
[GCC 4.4.3] on linux2
Type help, copyright, credits or license for more information.
 class A(object):
... def __add__(self, other):
... return indeed
...
 a = A()
 a + 1
'indeed'
 def new__add__(other):
... return not at all
...
 a.__add__ = new__add__
 a.__add__(1)
'not at all'
 a+1
'indeed'


 class B:
... def __add__(self, other):
... return 'now this is old-style'
...
 b = B()
 b+1
'now this is old-style'
 b.__add__ = new__add__
 b+1
'not at all'


I hate the fact that Python 2.x has two types of classes. Good riddance.

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


Re: dynamically modify help text

2010-06-28 Thread Stephen Hansen

On 6/28/10 1:13 PM, Emile van Sebille wrote:

On 6/28/2010 12:02 PM Benjamin Kaplan said...

Just to save the OP some trouble later on: this optimization is done
for most of the __*__ methods. Overriding __add__ on an instance won't
change the behavior of a + b.


ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on
Python 2.4.1 (#65, Jun 20 2005, 17:01:55) [MSC v.1310 32 bit (Intel)] on
win32
Type help, copyright, credits or license for more information.
  class Test: pass
...
  i = Test()
  i+1
Traceback (most recent call last):
File stdin, line 1, in ?
TypeError: unsupported operand type(s) for +: 'instance' and 'int'
  def __add__(self): return 6
...
  i.__add__ = __add__
  i+1
6
 

Was this in reference to a specific python version?


No, its a reference to new-style vs old-style classes. If Test inherits 
from object, it won't work. Its one of several subtle behaviorial 
differences between old/new classes.


--

   ... Stephen Hansen
   ... Also: Ixokai
   ... Mail: me+list/python (AT) ixokai (DOT) io
   ... Blog: http://meh.ixokai.io/

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


Re: dynamically modify help text

2010-06-28 Thread Christian Heimes
   i.__add__ = __add__
   i+1
 6
  
 
 Was this in reference to a specific python version?

This doesn't work with new style classes and thus not in Python 3.x.
Subclass from object and you'll see the difference.

Christian

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


Re: dynamically modify help text

2010-06-28 Thread Brian Blais

On Jun 28, 2010, at 14:25 , Chris Rebert wrote:


On Jun 27, 2010, at 22:37 , Red Forks wrote:

Read you doc file and set the __doc__ attr of the object you want to
change.



__doc__ is normally defined on classes, e.g. `A`, not instances, e.g.
`a`. help() looks for __doc__ accordingly.

Cheers,
Chris


so that gets back to my original question: can I change this text at  
runtime.  Doesn't look like I can, because it is defined for classes  
rather than instances.  Am I thinking about this correctly?



bb


--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais
http://bblais.blogspot.com/



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


Re: dynamically modify help text

2010-06-28 Thread Ben Finney
Brian Blais bbl...@bryant.edu writes:

 On Jun 28, 2010, at 14:25 , Chris Rebert wrote:
  __doc__ is normally defined on classes, e.g. `A`, not instances,
  e.g. `a`. help() looks for __doc__ accordingly.

 so that gets back to my original question: can I change this text at
 runtime.  Doesn't look like I can, because it is defined for classes
 rather than instances.  Am I thinking about this correctly?

Classes are objects. You can change the ‘__doc__’ attribute of a class
object the same as you'd change it for any other object::

A.__doc__ = new docstring

-- 
 \ “All television is educational television. The question is: |
  `\   what is it teaching?” —Nicholas Johnson |
_o__)  |
Ben Finney
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: dynamically modify help text

2010-06-28 Thread Steven D'Aprano
On Tue, 29 Jun 2010 10:37:44 +1000, Ben Finney wrote:

 Brian Blais bbl...@bryant.edu writes:
 
 On Jun 28, 2010, at 14:25 , Chris Rebert wrote:
  __doc__ is normally defined on classes, e.g. `A`, not instances, e.g.
  `a`. help() looks for __doc__ accordingly.

 so that gets back to my original question: can I change this text at
 runtime.  Doesn't look like I can, because it is defined for classes
 rather than instances.  Am I thinking about this correctly?
 
 Classes are objects. You can change the ‘__doc__’ attribute of a class
 object the same as you'd change it for any other object::
 
 A.__doc__ = new docstring


True, but what you can't do is:



a = A()
a.__doc__ = new docstring



unless you jump through hoops with __getattribute__ or descriptors.



-- 
Steven



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


dynamically modify help text

2010-06-27 Thread Brian Blais

Hello,

I know that the help text for an object will give a description of  
every method based on the doc string.  Is there a way to add  
something to this text, specific to an object, but generated at run- 
time?  I have an object that reads a file, and I would like part of  
that file to be shown if one does:  help(myobject)



thanks,

Brian Blais

--
Brian Blais
bbl...@bryant.edu
http://web.bryant.edu/~bblais
http://bblais.blogspot.com/



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


Re: dynamically modify help text

2010-06-27 Thread Red Forks
Read you doc file and set the __doc__ attr of the object you want to change.

On Monday, June 28, 2010, Brian Blais bbl...@bryant.edu wrote:
 Hello,

 I know that the help text for an object will give a description of every 
 method based on the doc string.  Is there a way to add something to this 
 text, specific to an object, but generated at run-time?  I have an object 
 that reads a file, and I would like part of that file to be shown if one 
 does:  help(myobject)


                         thanks,

                                 Brian Blais

 --
 Brian Blais
 bbl...@bryant.edu
 http://web.bryant.edu/~bblais
 http://bblais.blogspot.com/



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

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