Saturday, October 24, 2009

Re: Advice on checking optionally installed applications/features

Thank you!

I made the changes an believe I understand the reasoning for all of
your advice. I now have the following in my_stteing.py:

from django.conf import settings

def using_reversion():
try:
import reversion
return True
except ImportError:
return False
except:
pass

def using_cache():
if len(getattr(settings,'CACHE_BACKEND',0)) > 0:
return True
else:
return False

This really helped clean up my code!

Thank Again,
Mark
On Oct 23, 9:43 pm, Ethan Jucovy <ethan.juc...@gmail.com> wrote:
> 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+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate