-
-
Notifications
You must be signed in to change notification settings - Fork 317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lazily load default value of environment variable #185
Comments
Please describe how you suggest it will work! Allowing the value to be just a plain function? I use a pattern like this when I want to have that feature and it's not very verbose and easy to follow: env = environ.Env(
SOMETHING=(str),
)
SOMETHING = env("SOMETHING",default=None)
if not SOMETHING:
... calculate and assign value of something I think it's maybe not a good idea to allow lazy evaluation of values after |
Yeah I think being able to pass a plain function that's evaluated when |
So something like this then.. I wonder if it clashes with other features or not, I cant think of anything from the top of my head.. def create_something():
return "SOMETHING"
env = environ.Env(
SOMETHING=(str, create_something),
)
SOMETHING = env('SOMETHING')
OTHER_THING=env('OTHER_THING', default_value=create_something, cast_to=str) Note that the amount of code isn't shorter than the code I posted earlier, the only benifit I can see is that the type |
Yeah this is how I'd use it. I could submit a PR if you're okay with this.
…________________________________
De : Thomas Frössman <[email protected]>
Envoyé : dimanche 1 juillet 2018 11:02:16
À : joke2k/django-environ
Cc : konoufo; Author
Objet : Re: [joke2k/django-environ] Lazily load default value of environment variable (#185)
So something like this then..
def create_something():
return "SOMETHING"
env = environ.Env(
SOMETHING=(str, create_something),
)
SOMETHING = env('SOMETHING')
OTHER_THING=env('OTHER_THING', default_value=create_something, cast_to=str)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#185 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AJMLjLkjRj9f6f-n7pqylFxKFr7sd_mfks5uCKw4gaJpZM4UVWCm>.
|
I am not a maintainer. I just wanted to understand what you were requesting (for the benefit of everyone) , it looks clear to me now.. |
Oh okay! Sorry. Alright, waiting for the maintainers then...
…________________________________
De : Thomas Frössman <[email protected]>
Envoyé : dimanche 1 juillet 2018 17:45:57
À : joke2k/django-environ
Cc : konoufo; Author
Objet : Re: [joke2k/django-environ] Lazily load default value of environment variable (#185)
I am not a maintainer. I just wanted to understand what you were requesting, it looks clear to me now..
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#185 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AJMLjHAUoyFzbQDZ-tawBnfDcQZvTpBnks5uCQrVgaJpZM4UVWCm>.
|
I haven't tested this, but would creating your own lazy class work? def create_something():
return "SOMETHING"
class LazyString:
def __init__(self, value, fn):
self.value = value
self.fn = fn
def __str__(self):
if self.value is not None:
return self.value
return self.fn()
OTHER_THING=LazyString(env('OTHERTHING', default=None), create_something)
# someotherfile.py
from django.conf import settings
other_thing = str(settings.OTHER_THING) |
Well, I'd have to edit every code that uses `OTHER_THING` to replace `settings.OTHER_THING` with `settings.OTHER_THING.value`. I'm aware of workarounds to get the functionality I want. But for the sake of separation of concerns, I don't want to call `create_something` inside the settings file. And for consistency, I want the actual value of a setting to be always directly accessible thru `settings.FOO`. Moreover, I think to have the `default` argument to also accept a function just make sense and this pattern is ubiquitous in Python libraries.
…________________________________
De : Jafnee <[email protected]>
Envoyé : lundi 2 juillet 2018 03:29:24
À : joke2k/django-environ
Cc : konoufo; Author
Objet : Re: [joke2k/django-environ] Lazily load default value of environment variable (#185)
I haven't tested this, but would creating your own lazy class work?
def create_something():
return "SOMETHING"
class LazyString:
def __init__(self, value, fn):
self.value = value
self.fn = fn
def __str__(self):
if self.value:
return self.value
return self.fn()
OTHER_THING=LazyString(env('OTHERTHING', default=None), create_something)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#185 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/AJMLjIn_CBbk2BYfjx6illwqOByYc_1eks5uCZOUgaJpZM4UVWCm>.
|
Shouldn't need to replace everything to |
Remember that if you are using it for creating a new temporary directory and use it as a default value and don't return a static value the value will probably be different for each django process in a uwsgi server. If possible you should normally always prefer a deterministic way to create the settings even if it's lazily set. |
Hi, I think this feature could be useful. |
@Jafnee that is if you assume that the setting will be used in some way as a string or coerced to a string. |
Would you accept a pull request to enable lazy evaluation of default value ? I think there are several use cases where you want to generate the default on-demand. For example, creating a new temporary directory and use it as a default value when a env var doesn't exist in environment.
The text was updated successfully, but these errors were encountered: