Opened 17 years ago
Closed 15 years ago
#10098 closed Uncategorized (wontfix)
LazySettings.get function with default value
| Reported by: | fero | Owned by: | nobody |
|---|---|---|---|
| Component: | Uncategorized | Version: | |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | yes |
| Easy pickings: | yes | UI/UX: |
Description
Add a 'get' method to LazySettings class in order to easily get a settings.py variable or its default value.
settings.get(var_name, default) instead of
try:
myvar = settings.var
except AttributeError:
myvar = default_value
my current patch is:
--- django/conf/init.py 2009-01-22 18:19:38.585001432 +0100
+++ django/conf/init.py.new 2009-01-22 18:23:00.221043810 +0100
@@ -41,6 +41,13 @@
self._import_settings()
setattr(self._target, name, value)
+ def get(self, name, default=None):
+
+ try:
+ getattr(self, name)
+ except AttributeError:
+ return default
+
def _import_settings(self):
"""
Load the settings module pointed to by the environment variable. This
Change History (5)
comment:1 by , 17 years ago
comment:2 by , 17 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | new → closed |
Or just use what already works:
myvar = getattr(settings, 'var', some_default)
comment:3 by , 15 years ago
| Easy pickings: | unset |
|---|---|
| Resolution: | wontfix |
| Severity: | → Normal |
| Status: | closed → reopened |
| Type: | → Uncategorized |
I feel like we should review this again. Truly reusable apps provide settings. Settings may or may not exist. Accessing settings which may or may not exist is a very common problem.
Do we really want to promote a non-conformed API of getattr throughout everyones projects, or would we rather implement a 2-line function which can act as a reusable standard API for pulling in settings which may or may not be present.
comment:4 by , 15 years ago
| Easy pickings: | set |
|---|---|
| Patch needs improvement: | set |
| Version: | 1.0 |
comment:5 by , 15 years ago
| Resolution: | → wontfix |
|---|---|
| Status: | reopened → closed |
Please don't reopen ticket closed by a core dev. If you disagree with the decision to close the ticket, bring up the topic on the django-developers list.
sorry...I translate..
Add a 'get' method to LazySettings? class in order to easily get a settings.py variable or its default value.
instead of:
try: myvar = settings.var except AttributeError: myvar = default_valuemy patch is
--- django/conf/__init__.py 2009-01-22 18:19:38.585001432 +0100 +++ django/conf/__init__.py.new 2009-01-22 18:23:00.221043810 +0100 @@ -41,6 +41,13 @@ self._import_settings() setattr(self._target, name, value) + def get(self, name, default=None): + + try: + getattr(self, name) + except AttributeError: + return default + def _import_settings(self): """ Load the settings module pointed to by the environment variable. This