views:

77

answers:

1

Hi,

After just been coding for about 6-9 months. I probably changed my coding style a number of times after reading some code or read best practices. But one thing I haven't yet come a cross is a good why to populate the template_dict.

As of now I pass the template_dict across a number of methods (that changes/modifies it) and returns is. The result is that every methods takes template_dict as first argument and the returns it and this in my eyes doesn't seems to be the best solution.

An idea is to have a method that handles all the changes. But I'm curios if there's a best practice for this? Or is it "do what you feel like"-type of thing?

The 2 things I think is pretty ugly is to send as an argument and return it in all methods. And the just the var name is written xxx number of times in the code :)

..fredrik

EDIT:

To demonstrate what I mean with template_dict (I thought that was a general term, I got it from the google implementation of django's template methods).

I have an dict I pass to the template via the render.template method:

template.render(path, template_dict) #from google.appengine.ext.webapp import template

This template_dict I need to manipulate in order to send data/dicts/lists to the view (html-file). If I'm not mistaken.

So with this in mind, my code usually ends up looking some this like this:

## Main.py file to handle the request and imports classes.

from models import data
from util import foo

class MainHandler(webapp.RequestHandler):
    template_dict = { 'lang' : 'en' }
    ## reads all current keys and returns dict w/ new keys, if needed
    template_dict = data.getData(template_dict) 
    if 'unsorted_list' in template_dict:
        template_dict = util.foo(template_dict)
    ## and so on....

    path = os.path.join(os.path.dirname(__file__), 'templates', file)
    self.response.out.write(template.render(path, template_dict))

In most of my applications the many returns and sets doesn't appear in the main.py but rather in other classes and methods.

But you should do the general idea.

+1  A: 

If the functions in question are all methods of some object foo, then each of them can refer to the context they're building up (I imagine that's what you mean by "template dict"?) as self.ctx or the like (attribute name's somewhat arbitrary, the key point is that you can keep the context as an attribute of foo, typically initialized to empty in foo's __init__, and incrementally build it up via foo's methods; in the end, foo.ctx is ready for you).

This doesn't work in a more general case where the functions are all over the place rather than being methods of a single object. In that case ctx does need to be passed to each function (though the function can typically alter it in-place and doesn't need to return it).

Alex Martelli
Sorry for the first (a bit confusing) post. It's updated.
fredrik
@fredrik, sure: it's what is normally called a context, as I imagined (and that's why I used the attribute name `ctx`;-). As I already mentioned, just at the end of my answer above: just have all those functions alter the dict (AKA context) in place; you'll still pass it as an argument (the only sound practice if the functions aren't methods of a single argument), but you won't have all the `template_dict =` boilerplate assignments.
Alex Martelli
Ah took me 2-3 reads to understand what you meant. Thanks.
fredrik