return locals()

129 views
Skip to first unread message

Annet

unread,
May 22, 2015, 1:11:24 PM5/22/15
to web...@googlegroups.com
I have functions that return locals(), I wonder whether it is possible to limit
the variables returned to those needed by the related view.


Kind regards,

Annet

Niphlod

unread,
May 22, 2015, 2:56:40 PM5/22/15
to web...@googlegroups.com
you're the only one knowing what the view uses. using locals() is good for development, then you'll need to return a dict holding just what's needed.... but there's no facility to do it.

Michele Comitini

unread,
May 22, 2015, 4:15:36 PM5/22/15
to web...@googlegroups.com
replace
return locals()

with
return dict(var1=var1, var2=var2...)

where var[1,2] are the variables you use in the view

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anthony

unread,
May 22, 2015, 4:59:24 PM5/22/15
to web...@googlegroups.com, anne...@googlemail.com
Can you be more clear about what you're trying to do? Why doesn't the usual returning of a dictionary work in this case?

Annet

unread,
May 24, 2015, 3:21:29 AM5/24/15
to web...@googlegroups.com, anne...@googlemail.com
Thank you all for your replies.



Can you be more clear about what you're trying to do? Why doesn't the usual returning of a dictionary work in this case?

The usual returning of a dictionary works. To limit the number of views I designed an outer_layout and inner_layout and tried
to keep the other views as generic as possible.

I moved the defintion of some variables to db.py or to the controller outside any function, to prevent myself from starting a
function with defining variables the function itself does not need, but one of the views does need. Some functions return
10 + variables, so I thought that instead of putting them in a dictionary it would be easier to do something like:

def my_function():
    ....
    return function_vars()


Kind regards,

Annet

Anthony

unread,
May 24, 2015, 1:51:32 PM5/24/15
to web...@googlegroups.com, anne...@googlemail.com
It's still not quite clear what you want to do. Variables defined in models will be available in all views, regardless of what the action returns. Variables defined in controllers outside of functions will not be available in any views (unless explicitly returned by a controller function). locals() will include any variables defined locally within a given controller function. Can you provide a more detailed example of what you are trying to achieve?

Anthony

Annet

unread,
May 25, 2015, 11:31:19 AM5/25/15
to web...@googlegroups.com, anne...@googlemail.com
Hi Anthony,


Can you provide a more detailed example of what you are trying to achieve?

I have afunction which returns the following dictionary:

return dict(stylesheet=stylesheet, navbarpad=navbarpad, logo=logo, wordmark=wordmark, brandurl=brandurl,
                dropdowns=dropdowns, plural=plural, maincomponent=maincomponent, downloadvcard=downloadvcard,
                vertex=vertex, address=address, googlemaps=googlemaps, qrcode=qrcode, socialmedia=socialmedia)


I just thought that when these variables are stored in sort of a function object I could do:

return functions_vars()


instead of returning them explicitly in a dict()


Best,

Annet

Anthony

unread,
May 25, 2015, 4:56:05 PM5/25/15
to web...@googlegroups.com, anne...@googlemail.com
Are you saying you want to do something like:

def function_vars(...):
    stylesheet
= some_stylesheet
    navbarpad
= some_navbarpad
    logo
= some_logo
   
return locals()

def some_action():
   
return function_vars()

Yes, you can do that. Have you tried it and had a problem? Note, if function_vars is defined in a controller, either make sure it takes at least one argument or name it __function_vars -- otherwise, it will be accessible via URL.

Anthony

Annet

unread,
May 26, 2015, 12:38:50 PM5/26/15
to web...@googlegroups.com, anne...@googlemail.com
Hi Anthony,

Thanks for your reply.


Are you saying you want to do something like:

Yes, that's exactly what I want, I just wasn't able to figure out
how to write the code. Your code works.


Best Annet.

 

Felix Penetrante

unread,
May 28, 2015, 1:16:53 AM5/28/15
to web...@googlegroups.com
Thanks for this post. Now I know how to secure views.
Reply all
Reply to author
Forward
0 new messages