On Monday, November 14, 2016 at 1:29:11 PM UTC-5, Brendan Barnwell wrote: > > Of course, but that's equally true of the "List" class that provides the > request.args functionality. That's just Python code. I could write it > myself. But web2py provides it for convenience. I'm just saying it would > be nice to have similar convenience for request.vars. >
Got it. I was just reacting to your "What can I do..." question, which made it seem as if you were at a loss for alternatives. > You're right that dict.get handles the default value, but the > functionality available for args includes substantially more than that. > It's only about 20 lines of code, but it does at least three different > things: > > 1. use the default value if provided > 2. convert the value using the "cast" parameter if provided > 3. redirect to an error page if given in the "otherwise" parameter > > Plain dicts don't do all those things, nor do Storage objects. > There's also a fourth feature of the __call__ method of the List class (of which request.args is an instance), which is that an out of bound index returns None rather than generating an error. Now, let's consider the Storage class (and request.vars) with regard to these four features: - Like the out-of-bound index behavior of the List class, if you attempt to access a non-existent key/attribute, the Storage class returns None rather than generating an error. - As already noted, with request.vars, you can specify a default via the inherited .get method. - The "otherwise" parameter would be less relevant for request.vars. Unlike request.vars, request.args captures part of the URL path, so it makes sense to validate that the entire expected path exists and possibly redirect otherwise (not saying this would never be the case with request.vars, just likely more common when dealing with the URL path). That leaves the "cast" functionality, and I'm not sure it would be worth implementing a special __call__ method just for casting. For now, what I did is just write a BetterStorage class that inherits from > Storage and includes the __call__ method from List (with a couple > modifications at the beginning because it's delegating to dict keys rather > than list indices). Would there be interest in making this change to > web2py? > I don't have a strong opinion but would lean towards no, as I think it is best to avoid adding code to the framework, unless we expect this to be a very common use case. But others may have a different opinion, so feel free to bring it up on the developers list. Anyway, how are you using BetterStorage? Your initial post referred specifically to request.vars -- are you converting request.vars to BetterStorage? In that case, it might be simpler to write a function rather than a whole new class just for this purpose. Anthony -- 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.