cannot use class with formstyle=callback (w/ possible solution)

29 views
Skip to first unread message

André Kablu

unread,
Jul 28, 2014, 11:40:28 PM7/28/14
to web2py-d...@googlegroups.com
I was creating a new formstyle, and I wanted to pass more details:

SQLFORM(db.table, formstyle=style(placeholder=True, errors='down'))


The idea was to create a class that receives both parameters then the __call__ method receives form and field parameters from sqlhtml createform.

I use a similar idea on the REQUIRES and REPRESENT callbacks.

Well, there is a line code there for backward compatibility:

args, varargs, keywords, defaults = inspect.getargspec(formstyle)

This creates an error b/c the inspect cannot capture __call__ from class.

The possible solution (worked for me) is:

            def get_callable_argspec(fn):
                if inspect.isfunction(fn) or inspect.ismethod(fn):
                    inspectable = fn
                elif inspect.isclass(fn):
                    inspectable = fn.__init__
                elif hasattr(fn, '__call__'):
                    inspectable = fn.__call__
                else:
                    inspectable = fn
                return inspect.getargspec(inspectable)

            args, varargs, keywords, defaults = get_callable_argspec(formstyle)

I think this is the best idea, if you guys have any commend or better idea please share.

Thanks!

Anthony

unread,
Jul 29, 2014, 12:12:21 AM7/29/14
to web2py-d...@googlegroups.com
+1

Leonel Câmara

unread,
Jul 29, 2014, 10:18:42 AM7/29/14
to web2py-d...@googlegroups.com
This would work but I have to say using a class for this feels very Javaish.

Python has closures. If you wanted to have arguments for your callback you could have easily put it inside another function with your arguments.

def make_formstyle(placeholder=True, errors='down'):
    
    def callback(form, fields):
        if placeholder:
            #do some stuff
      ....

    return callback

And then you would do

SQLFORM(db.table, formstyle=make_formstyle(placeholder=True, errors='down'))


That said, formstyle should definitely accept any callable but I hate all this inspection which is very unpythonic, maybe a better way would be to try to find a smarter way to do the backwards compatibility stuff. Instead of using inspect maybe we could just use a try and except. If it failed to call formstyle using the new 2 arguments way, then use the 4 arguments oldstyle. 

Massimo DiPierro

unread,
Jul 29, 2014, 10:56:54 AM7/29/14
to web2py-d...@googlegroups.com
I agree. I am reverting the previous patch.

--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-develop...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages