I want to use/implement global variables in Rails. I tried the following:
In config/environments/production.rb:
$n_exams_question = 20
In config/environments/development.rb:
$n_exams_question = 3
I want to use different values in production and development. My localserver is not detecting the global variables. Where is the best place to implement it?
Rails does it that way:
https://guides.rubyonrails.org/configuring.html#custom-configuration
In config/environments/production.rb:
config.myGlobalVariable = 'blabla1'
In config/environments/development.rb:
config.myGlobalVariable = 'blabla2'
then use this Anywhere:
Rails.configuration.myGlobalVariable
I don't know if that answers your question or if it's a good practice, but I would have used the environment variables.
To do this, I use the gem Figaro
Example :
In your application.yml file
development:
N_EXAMS_QUESTION: "15" # ENV['N_EXAMS_QUESTION']
production:
N_EXAMS_QUESTION: "20" # ENV['N_EXAMS_QUESTION']
You can call this value with ENV['N_EXAMS_QUESTION']
Related
I'm receiving data from a server containing HTML with variables included.
exemple of data:
content = '<div> I can be displayed here {{variable1}} or here {{variable2}} or any wherelse </div>'
I want to replace {{variable1}} and {{variable2}} with dynamic components in angular upon received from server.
these variables dont have fixed place nor fixed number.
I created a stackblitz where i can find these variables, create the dynamic component but i can't place them in the right place.
https://stackblitz.com/edit/angular-ivy-vcxutx
I think you wanna evaluate a string that came from your backend as an angular expression - which is not a good solution, but in a real world there a bunch of complicated problems - I'm not your jude )
So you have to use $interpolate service here: https://docs.angularjs.org/api/ng/service/$interpolate
I'm looking for a way to make environment variables in Postman, that contain other variables. For example: {Server}=localhost;{Port}=9200;{ServerUrl}={Server}:{Port}.
Like in Make...
This way it doesn't seem to work with Postman.
EDIT:
My attempt:
You could do it but I wouldn't recommend it, just seem like you're missing the benefit creating a set of variables and then changing the values of these by selecting a different environment file.
Add this string {{ElasticsearchProtocol}}://{{ElasticsearchServer}}:{{ElasticsearchPort}} as the ElasiticsearchUrl variable, on the environment file.
Or you could add this to the Pre-Request Script:
let ElasticsearchProtocol = pm.environment.get('ElasticsearchProtocol')
let ElasticsearchServer = pm.environment.get('ElasticsearchServer')
let ElasticsearchPort = pm.environment.get('ElasticsearchPort')
pm.environment.set("ElasticsearchUrl", `${ElasticsearchProtocol}://${ElasticsearchServer}:${ElasticsearchPort}`)
I want to create my own variable and want to set my default value which should work like a built in variable in Informatica. I want to use created variable in my all workflow.
Is it possible any way ?
Thanks
You can use the same parameter file across all of your mappings (there is syntax to separate out bits which are for specific sections and those which are universal by way of the scope) see following link https://network.informatica.com/thread/27560
I am working on a django app that needs a directory to download and store files.
I want to keep my app reusable so I do not want to hard code the path of this directory.
So I want to make this path a setting/a global variable that can be set up.
Where could I put this setting/global variable?
Is this kind of approach good ?
http://blog.muhuk.com/2010/01/26/developing-reusable-django-apps-app-settings.html
Thanks for your advice!
I use the following methodology:
# some file in your app:
from django.conf import settings
MY_APP_SETTING = getattr(settings, 'MY_APP_SETTING', 'some default value')
This effectively allows end-users to customized the setting in their own settings.py, but still ensures that there's always some default value set. You can now use MY_APP_SETTING at will in the rest of your code.
UPDATE
The link in your question was taking too long to load, so I just went ahead and answered. As it turns out, the method I suggested is the same as what it suggests, so yes, I'd consider that approach good ;).
How can I create an application scope variable which is loaded when the django app starts, be in memory and accessible by all.
Basically I want to reuse the variable through out the application without reloading it.
Thanks
You could add it to your settings.py file. Or, add it to the __init__.py file inside the app directory.
Are you referring to something like an environment variable? You could load it via init...
__init__.py
import os
os.environ['APP_VAR_WHATEVER'] = 'hello world!'
Python has three levels of namespace — local (specific to the current function or class method), global (specific to the current module), and built-in. That is, Python does not really have project-wide global variables.
If it's a read-only variable you want, you could use settings.py to define the value and import settings from all other modules that want access to the variable.
If it for both reading and writing, I would likely use the database backend I'm already using with Django, instead of a python variable.
If you could provide a more detailed description of what you're trying to achieve, perhaps we could come up with a better suited solution.