Opened 7 years ago

Closed 4 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 Changed 7 years ago by fero

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

sorry...I translate..

Add a 'get' method to LazySettings? class in order to easily get a settings.py variable or its default value.

 myvar = settings.get(var_name, default)

instead of:

try:

    myvar = settings.var

except AttributeError:

    myvar = default_value

my 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

comment:2 Changed 7 years ago by ubernostrum

  • Resolution set to wontfix
  • Status changed from new to closed

Or just use what already works:

myvar = getattr(settings, 'var', some_default)

comment:3 Changed 4 years ago by dcramer

  • Easy pickings unset
  • Resolution wontfix deleted
  • Severity set to Normal
  • Status changed from closed to reopened
  • Type set to 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 Changed 4 years ago by dcramer

  • Easy pickings set
  • Patch needs improvement set
  • Version 1.0 deleted

comment:5 Changed 4 years ago by jezdez

  • Resolution set to wontfix
  • Status changed from reopened to 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.

Note: See TracTickets for help on using tickets.
Back to Top