On Fri, Oct 23, 2009 at 1:09 PM, Mark (Nosrednakram) <nosrednak...@gmail.com > wrote:
> > Hello Django Users, > > I sometimes have reversion installed and sometimes have a > CACHE_BACKEND defined and came up with the following solution for > determining if I should register a model or use cache specific > functionality. Since I'm not a Python Guru I'm asking an evaluation > of this method before I start using it in all my code. It seems sensible to me. I put a few implementation-level comments inline, but I agree that checking for imports and the existence of settings is the best way to do this. The length > check on CACHE_BACKEND is in the even someone sets it to ''. > Currently I started adding this to each file where it's relevant. > You might want to encapsulate these in their own module somewhere, so you can later change or add ways of determining the answers. From the outside it could then look like `from my_configuration import using_reversion; if using_reversion(): [etc]` > try: > import reversion > _USE_REVERSION=True > except: > _USE_REVERSION=False > Don't use a blanket `except`, just to be on the safe side. You want `except ImportError`. try: > if len(settings.CACHE_BACKEND) > 0: > _USE_CACHE=True > except: > _USE_CACHE=False > Again, avoid catching all exceptions. (If e.g. the settings module threw a RuntimeError on attribute access, you'd probably want to know about that, not disable the cache.) Instead, `except AttributeError`, or just `if len(getattr(settings, "CACHE_BACKEND", '')) > 0`. Regards, egj --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---