Re: [Tutor] Ask a class for it's methods

2008-12-15 Thread Kent Johnson
On Fri, Dec 12, 2008 at 6:06 PM, Shrutarshi Basu
technorapt...@gmail.com wrote:
 Is there a way to ask an object for a list of it's
 methods (with argument requirements if possible)?

Take a look at the inspect module. If it does not directly give you
what you need, look at the source - it looks at function attributes
that you can directly access.

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Ask a class for it's methods

2008-12-13 Thread Lie Ryan
On Sat, 13 Dec 2008 02:59:34 +0100, Andreas Kostyrka wrote:

 On Fri, Dec 12, 2008 at 06:06:35PM -0500, Shrutarshi Basu wrote:
 I have a list containing strings like :
 
 func1[]
 func2[1,2]
 func3[blah]
 
 I want to turn them into method calls (with numeric or string
 arguments) on a supplied object. I'm trying to figure out the best way
 to do this. Since these lists could be very big, and the methods could
 be rather complex (mainly graphics manipulation) I would like to start
 by getting a list of the object's methods and make sure that all the
 strings are valid. Is there a way to ask an object for a list of it's
 methods (with argument requirements if possible)?

 Well, there are ways, but they are not reliable by design. Objects can
 return dynamically methods.
 
 So use something like this:
 
 if callable(getattr(obj, func1)):
 # func1 exists.
 
 Guess nowaday with Python3 released, you should not use callable, but
 instead test on __call__
 
 if hasattr(getattr(obj, func1), __call__):

or the more pythonic version would just call func() and catch exception 
if it's not callable:

try:
func1()
except TypeError:
print func1 is not callable

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Ask a class for it's methods

2008-12-13 Thread Andreas Kostyrka
On Sat, Dec 13, 2008 at 08:03:10AM +, Lie Ryan wrote:
 On Sat, 13 Dec 2008 02:59:34 +0100, Andreas Kostyrka wrote:
 
  On Fri, Dec 12, 2008 at 06:06:35PM -0500, Shrutarshi Basu wrote:
  I have a list containing strings like :
  
  func1[]
  func2[1,2]
  func3[blah]
  
  I want to turn them into method calls (with numeric or string
  arguments) on a supplied object. I'm trying to figure out the best way
  to do this. Since these lists could be very big, and the methods could
  be rather complex (mainly graphics manipulation) I would like to start
  by getting a list of the object's methods and make sure that all the
  strings are valid. Is there a way to ask an object for a list of it's
  methods (with argument requirements if possible)?
 
  Well, there are ways, but they are not reliable by design. Objects can
  return dynamically methods.
  
  So use something like this:
  
  if callable(getattr(obj, func1)):
  # func1 exists.
  
  Guess nowaday with Python3 released, you should not use callable, but
  instead test on __call__
  
  if hasattr(getattr(obj, func1), __call__):
 
 or the more pythonic version would just call func() and catch exception 
 if it's not callable:
 
 try:
 func1()
 except TypeError:
 print func1 is not callable

But it happens to be wrong :)

Consider:

def func1():
raise TypeError(except does not care where in the callstack the exception 
happens!!!)

Common sources, IMHE, for TypeErrors include int(not_a_string_or_number), which 
raises a TypeError.

Andreas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Ask a class for it's methods

2008-12-13 Thread Lie Ryan
On Sat, 13 Dec 2008 10:19:34 +0100, Andreas Kostyrka wrote:

 On Sat, Dec 13, 2008 at 08:03:10AM +, Lie Ryan wrote:
 On Sat, 13 Dec 2008 02:59:34 +0100, Andreas Kostyrka wrote:
 
  On Fri, Dec 12, 2008 at 06:06:35PM -0500, Shrutarshi Basu wrote:
  I have a list containing strings like :
  
  func1[]
  func2[1,2]
  func3[blah]
  
  I want to turn them into method calls (with numeric or string
  arguments) on a supplied object. I'm trying to figure out the best
  way to do this. Since these lists could be very big, and the methods
  could be rather complex (mainly graphics manipulation) I would like
  to start by getting a list of the object's methods and make sure
  that all the strings are valid. Is there a way to ask an object for
  a list of it's methods (with argument requirements if possible)?
 
  Well, there are ways, but they are not reliable by design. Objects
  can return dynamically methods.
  
  So use something like this:
  
  if callable(getattr(obj, func1)):
  # func1 exists.
  
  Guess nowaday with Python3 released, you should not use callable, but
  instead test on __call__
  
  if hasattr(getattr(obj, func1), __call__):
 
 or the more pythonic version would just call func() and catch exception
 if it's not callable:
 
 try:
 func1()
 except TypeError:
 print func1 is not callable
 
 But it happens to be wrong :)
 
 Consider:
 
 def func1():
 raise TypeError(except does not care where in the callstack the
 exception happens!!!)
 
 Common sources, IMHE, for TypeErrors include
 int(not_a_string_or_number), which raises a TypeError.

Then it's the lower function's fault (i.e. bug) for not handling that 
exception, and if you really need it, it's trivial to check the kind of 
TypeError reraising the exception if it's not what we expect.

try:
func1()
except TypeError, e:
if not e.message.endswith('object is not callable'): 
print 'There is something deeply wrong in your func1'
raise
print func1 is not callable

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Ask a class for it's methods

2008-12-12 Thread Shrutarshi Basu
I have a list containing strings like :

func1[]
func2[1,2]
func3[blah]

I want to turn them into method calls (with numeric or string
arguments) on a supplied object. I'm trying to figure out the best way
to do this. Since these lists could be very big, and the methods could
be rather complex (mainly graphics manipulation) I would like to start
by getting a list of the object's methods and make sure that all the
strings are valid. Is there a way to ask an object for a list of it's
methods (with argument requirements if possible)?
The next question is once I've validated the list, what's the easiest
way to turn the list element into a method call? I'll be parsing the
string to separate out the method name and arguments. If I store the
method name (say func1)  in a variable, say var, could I do
object.var() and have if call the func1 method in object?
Thanks for your help and I'll surely have more questions as I get along,
Thanks
Basu

-- 
The ByteBaker :
http://www.bytebaker.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Ask a class for it's methods

2008-12-12 Thread Alan Gauld


Shrutarshi Basu technorapt...@gmail.com wrote


I have a list containing strings like :

func1[]
func2[1,2]
func3[blah]

I want to turn them into method calls (with numeric or string
arguments) on a supplied object.


The easiest way is to call getattr() which will return a reference
to the method if it exists.

be rather complex (mainly graphics manipulation) I would like to 
start

by getting a list of the object's methods and make sure that all the
strings are valid.


Why not just handle it using try/except?
Thats exactly what exceptions were designed for.


Is there a way to ask an object for a list of it's
methods (with argument requirements if possible)?


You can try dir() but that won't give you the parameters.
But again try/except can catch an invalid call and you can detect
whether the number of parameters is wrong in the except clause.


method name (say func1)  in a variable, say var, could I do
object.var() and have if call the func1 method in object?


no, but you can do this:


class C:

... def f(self, x): print x
...

c = C()
dir(c)

['__doc__', '__module__', 'f']

m = getattr(c,'f')
m(42)

42

try: m(43,'foo')

... except TypeError:
... # parse the error message here
... print 'Method of', c, 'takes ??? arguments'
...
Method of __main__.C instance at 0x01BF8350 takes ??? arguments




HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Ask a class for it's methods

2008-12-12 Thread Shrutarshi Basu
I normally would use exceptions, because I think exceptions are a
great idea. But since the functions may be time-consuming graphics
functions and the lists could easily be hundreds of such calls, I
don't want the user to sit around for something that might fail. Of
course, I'm just starting so my assumptions about time might not turn
out to be valid, so I could just use exceptions in the end. This is an
option I'm exploring.

Getattr and dir seem to be the way to go for now, so I'll be trying to
apply them over the weekend and see how it turns out. Any more ideas
welcome.
Thanks,
Basu

-- 
The ByteBaker :
http://www.bytebaker.com
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Ask a class for it's methods

2008-12-12 Thread Lie Ryan
On Fri, 12 Dec 2008 20:05:23 -0500, Shrutarshi Basu wrote:

 I normally would use exceptions, because I think exceptions are a great
 idea. But since the functions may be time-consuming graphics functions
 and the lists could easily be hundreds of such calls, I don't want the
 user to sit around for something that might fail. Of course, I'm just
 starting so my assumptions about time might not turn out to be valid, so
 I could just use exceptions in the end. This is an option I'm exploring.

The general rule of thumb is usually that exception is slow on the 
exceptional cases arise, while being faster if the try block doesn't 
fail. On the other hand, using if-block is usually slow all over, but not 
as slow as exception's exceptional cases.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Ask a class for it's methods

2008-12-12 Thread Andreas Kostyrka
On Fri, Dec 12, 2008 at 06:06:35PM -0500, Shrutarshi Basu wrote:
 I have a list containing strings like :
 
 func1[]
 func2[1,2]
 func3[blah]
 
 I want to turn them into method calls (with numeric or string
 arguments) on a supplied object. I'm trying to figure out the best way
 to do this. Since these lists could be very big, and the methods could
 be rather complex (mainly graphics manipulation) I would like to start
 by getting a list of the object's methods and make sure that all the
 strings are valid. Is there a way to ask an object for a list of it's
 methods (with argument requirements if possible)?

Well, there are ways, but they are not reliable by design. Objects can return 
dynamically
methods.

So use something like this:

if callable(getattr(obj, func1)):
# func1 exists.

Guess nowaday with Python3 released, you should not use callable, but instead
test on __call__

if hasattr(getattr(obj, func1), __call__):



 The next question is once I've validated the list, what's the easiest
 way to turn the list element into a method call? I'll be parsing the
 string to separate out the method name and arguments. If I store the
 method name (say func1)  in a variable, say var, could I do
 object.var() and have if call the func1 method in object?
getattr(object, var)() # calls the method named in var:

class A:
def x(self, a, b):
return a + b

instance = A()

name = x

args_positional = (1, 2)

print getattr(instance, name)(*args_positional) # prints 3

args_by_name = dict(a=1, b=2)

print getattr(instance, name)(**args_by_name) # prints 3 too.

Andreas
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor