question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unneeded DATA_DIR kludge

See original GitHub issue

I got my project generated, and I’m looking at the diffs between it and a Django-CMS 3.5 project I set up a year ago, and there’s this weirdness at the start of settings.py:

import os  # isort:skip
gettext = lambda s: s
DATA_DIR = os.path.dirname(os.path.dirname(__file__))
"""
Django settings for abcde project.

Generated by 'django-admin startproject' using Django 2.1.9.

For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

There’s a few problems here:

1. You can’t put code in front of the file’s docstring. Python will ignore it; it is not placed in the module’s __doc__ property:

$ ./manage.py shell
>>> import abcde.settings as tgt_settings
>>> repr(tgt_settings.__doc__)
'None'

Worse, while Python itself ignores those strings, other tools such as epydoc consider strings after variable assignments to be docstrings for those variables (e.g. for documenting module constants).

2. DATA_DIR has the same value as BASE_DIR, and BASE_DIR’s algorithm is more robust. DATA_DIR should be set equal to BASE_DIR.

3. Setting gettext to a no-op without documenting why, when we should be trying to import the proper gettext function, and only setting the no-op version if the import fails or something.

4. The os module is imported twice.

Corrected version:

"""
Django settings for abcde project.

Generated by 'django-admin startproject' using Django 2.1.9.

For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""

import os
try:
    from gettext import gettext
except ImportError:
    def gettext(s):
        return s

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DATA_DIR = os.environ.get('DATA_DIR', BASE_DIR)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
yakkycommented, Jan 2, 2020

@bluegrounds gettext can be probably removed now as it should be safe to use the “real” gettext_lazy in settings. this used to be impossibile in settings.py and it remained in the generated settings file. We are testing a more intelligent way to patch settings.py via ast module but it will take some time for it to be mergeable (a few months). In the meantime we don’t plan any cleanup on settings file

0reactions
yakkycommented, Jan 4, 2020

DATA_DIR is used to set MEDIA_ROOT, STATIC_ROOT

we will revise this upon the refactoring of the settings generation

Read more comments on GitHub >

github_iconTop Results From Across the Web

Commit - rpms/postgresql ... - Fedora Package Sources
+ Note: as of PG 8.3 this patch will be unnecessary; use configure's. 8. + --with-system-tzdata switch instead. 9. - install-time kluge seen...
Read more >
GNU make
Write text to a file. Expand a user-defined function. Return the un-expanded value of a variable. Evaluate the arguments as makefile syntax.
Read more >
mingwrt/Makefile.in at master · MaxKellermann/mingwrt · GitHub
it appears to be unused, and could be removed. objdir = . ... datadir = @datadir@. infodir = @infodir@ ... and this kludge...
Read more >
Question: Excessive disk usage in /var/urbackup - Server
Running UrBackup as the only resource-using application on a Raspberry Pi3 connected to a 4Tb external disk. Disk is partitioned as 10Gb for ......
Read more >
Server loops excessively in _checkchunk() when safemalloc is ...
I am sorry, but the the core and the datadir were not preserved. ... removing quite a bit of kludge: redefinition of my_malloc, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found