Comments (8)
Interesting, that's not a bad idea.
But this would only work for string based settings as accessing os.environ
in Python doesn't really allow you to get any other type. Any idea how we could solve this?
from django-configurations.
Well, what I was trying was a bit of coercion. The below snippet could be more comprehensive to attempt to guess at the type.
def env_var( value ):
if str(os.environ[value]).lower() in ("true",):
return True
if str(os.environ[value]).lower() in ("false",):
return False
try:
return int(os.environ[value])
except Exception:
pass
try:
return float(os.environ[value])
except Exception:
pass
return os.environ[value]
In the case of dicts, you could delimit the object hierarchy.
A_SETTING = {
'attr1': 5,
'attr2': 10,
}
# in os.environ search for
A_SETTING--attr1
A_SETTING--attr2
Note sure when it comes to lists/tuples. You would have to have markup in them or peer env vars that denote types of other existing vars wrt Python types.
I actually thought I could have this working in a 'nice' way by implementing new or init on a BaseSettings class that everything builds from (aka configurations.Settings), however it doesn't appear that the settings classes are ever instantiated. And I can't modify static class attributes.
So here I am. Thought you might have some ideas to extend django-configurations to support such a notion natively in it's construction of the final settings object.
from django-configurations.
Ooops. :)
from django-configurations.
IMHO if you're doing anything that involved, it's probably a better idea to delimit your settings in the settings module and then opt-in using a (plain old string) flag in os.environ. This could be accomplished by using a factory function for DJANGO_CONFIGURATION
and, within it, mixing in the appropriate classes based on os.environ values. Coming up with a way of serializing non-string types just seems like a mess.
from django-configurations.
Yeah, type guessing is a bad idea in that case, agreed. @cguethle can you elaborate what your use case is?
from django-configurations.
Okay, closing the ticket for now as I haven't gotten feedback from @cguethle.
from django-configurations.
That's fine. While a generic solution would be nice, I acknowledge the coercion problems. I have setup an alternative at this point.
Thanks!
Chris
iPhone Approved
On Sep 21, 2012, at 9:36 AM, Jannis Leidel [email protected] wrote:
Okay, closing the ticket for now as I haven't gotten feedback from @cguethle.
—
Reply to this email directly or view it on GitHub.
from django-configurations.
Ah, gotcha, please do tell if it can be generalized.
from django-configurations.
Related Issues (20)
- Improve Dotenv usage to avoid basic issues HOT 1
- Use the 'furo' theme for Sphinx documentation
- Improve code blocks in documentation
- Release minor version 2.4.1 HOT 3
- 2.4.1: pytest is failing HOT 2
- Replace deprecated imp module with importlib HOT 4
- How can this be used with `django.setup()` HOT 2
- Make magical importing optional
- <frozen importlib._bootstrap>:1049: ImportWarning: ConfigurationImporter.find_spec() not found; falling back to find_module() HOT 4
- "STATICFILES_STORAGE/STORAGES are mutually exclusive" on Django>=4.2
- WSGI application loaded twice unexpectedly
- Release 2.4.2 is not on PyPI HOT 9
- Update Python and Django supported versions
- Add support for Python 3.12 HOT 1
- DeprecationWarning for USE_DEPRECATED_PYTZ, CSRF_COOKIE_MASKED, USE_L10N
- Broken link in `Configuration.load_dotenv` docstring HOT 1
- Add Django 5.0 classifier
- Publish version 2.5.1 on PyPi HOT 2
- Values not working in DATABASES dictionary and values.Value returning Value instance type instead of a str HOT 1
- DOTENV - Using environ_name strange behevior HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-configurations.