Hi there
I’ve been testing and "kind of " using GRASS-Python scripts for nearly 2 weeks and it’s being great. So far no major issues but now I need to do something:
- From a text file (ASCII) I need to read a few variables and make them global bariables I mean, be available and defined for a huge set of Scripts in order to avoid to define those variables in each Script. And, in case I change them, I only have to change in that ASCII File. My questions are:
1- How can I do GRASS to read an external File in order to define GLOBAL VARIABLES?
2- How can I call them from a GRASS_SCRIPT?
Thanks
Best regards,
Kim
Hi Kim,
I'm no expert, but I'd suggest not reading them into GRASS as global
variables but defining them as global variables within the script in Python.
You could then have Python read the variables from a file and set them
within the environment of the script, like with a config file. Here's some
interesting stuff about reading from files in Python:
http://www.java2s.com/Code/Python/File/File-Read.htm
http://docs.python.org/tutorial/inputoutput.html
Hope that helps! Sorry that I couldn't find a better tutorial, but there's
tons of stuff out there and the stuff I just posted is probably really basic
for you anyway 
Best,
Daniel
--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Define-Global-Variables-and-use-them-in-a-Python-script-tp5256628p5256687.html
Sent from the Grass - Users mailing list archive at Nabble.com.
Kim Besson wrote:
I've been testing and "kind of " using GRASS-Python scripts for nearly 2
weeks and it's being great. So far no major issues but now I need to do
something:
- From a text file (ASCII) I need to read a few variables and make them
global bariables I mean, be available and defined for a huge set of Scripts
in order to avoid to define those variables in each Script. And, in case I
change them, I only have to change in that ASCII File. My questions are:
1- How can I do GRASS to read an external File in order to define GLOBAL
VARIABLES?
2- How can I call them from a GRASS_SCRIPT?
A "global" variable in Python is local to the module in which it's
defined. So you you have a module named config, to access any global
variables in that module from other modules you would need either
"import config" (in which case, variables will be accessible as
config.whatever) or "from config import *" (but as the program grows,
the chances of name collisions increase).
For a set of related variables, e.g. configuration parameters, it's
usually better to make them all members of a specific object, e.g.:
class Params(object):
def __init__(self, filename):
# initialise members from file
# read the config file
params = Params("config.cfg")
Then you only need to import a single name:
from config import params
--
Glynn Clements <glynn@gclements.plus.com>
Hello Glynn
As soon as I arrive to the office I will give it a try. Thought that I could have global variables such as LC_LANGUAGES and other GRASS_GLOBAL VARIABLES. Is it possible to define them in Init.sh (or .bat) and be used in Python Scripts?
Best regards,
Kim
2010/7/5 Glynn Clements <glynn@gclements.plus.com>
Kim Besson wrote:
I’ve been testing and "kind of " using GRASS-Python scripts for nearly 2
weeks and it’s being great. So far no major issues but now I need to do
something:
- From a text file (ASCII) I need to read a few variables and make them
global bariables I mean, be available and defined for a huge set of Scripts
in order to avoid to define those variables in each Script. And, in case I
change them, I only have to change in that ASCII File. My questions are:
1- How can I do GRASS to read an external File in order to define GLOBAL
VARIABLES?
2- How can I call them from a GRASS_SCRIPT?
A “global” variable in Python is local to the module in which it’s
defined. So you you have a module named config, to access any global
variables in that module from other modules you would need either
“import config” (in which case, variables will be accessible as
config.whatever) or “from config import *” (but as the program grows,
the chances of name collisions increase).
For a set of related variables, e.g. configuration parameters, it’s
usually better to make them all members of a specific object, e.g.:
class Params(object):
def init(self, filename):
initialise members from file
read the config file
params = Params(“config.cfg”)
Then you only need to import a single name:
from config import params
–
Glynn Clements <glynn@gclements.plus.com>
Kim Besson wrote:
As soon as I arrive to the office I will give it a try. Thought that I could
have global variables such as LC_LANGUAGES and other GRASS_GLOBAL VARIABLES.
Is it possible to define them in Init.sh (or .bat) and be used in Python
Scripts?
I think you're referring to environment variables. Those can be
accessed from within Python using os.getenv() or os.environ.
However, the OS may impose limitations on the total space used by
environment variables, so you shouldn't use too many of these.
Also, environment variables are per-process, so you can't write a
script or program which modifies them for the session as a whole (this
is why GRASS has $GISRC).
--
Glynn Clements <glynn@gclements.plus.com>
Hi Glynn and rest of the list
I have tested this method by creating a config.py and a demo-example of config and I’m not being able to use this solution. Maybe I’m not fully understanding it (never used classes in Python):
1- I create a config1.py file in a working Python directory
2- I added this to config.py
class Params(object):
def init(self, filename):
params = Params(“config.cfg”)
3- My config.cfg has the following:
Ah= ‘1’
Bu= ‘2’
4- Just for testing, in a Python Shell I do:
from config1 import Params
(WORK)
Then:
print Params
I get: <class ‘config1.Params’>
So I cannot access variables inside config.cfg
What am I doing wrong?
Kim
2010/7/5 Glynn Clements <glynn@gclements.plus.com>
Kim Besson wrote:
I’ve been testing and "kind of " using GRASS-Python scripts for nearly 2
weeks and it’s being great. So far no major issues but now I need to do
something:
- From a text file (ASCII) I need to read a few variables and make them
global bariables I mean, be available and defined for a huge set of Scripts
in order to avoid to define those variables in each Script. And, in case I
change them, I only have to change in that ASCII File. My questions are:
1- How can I do GRASS to read an external File in order to define GLOBAL
VARIABLES?
2- How can I call them from a GRASS_SCRIPT?
A “global” variable in Python is local to the module in which it’s
defined. So you you have a module named config, to access any global
variables in that module from other modules you would need either
“import config” (in which case, variables will be accessible as
config.whatever) or “from config import *” (but as the program grows,
the chances of name collisions increase).
For a set of related variables, e.g. configuration parameters, it’s
usually better to make them all members of a specific object, e.g.:
class Params(object):
def init(self, filename):
initialise members from file
read the config file
params = Params(“config.cfg”)
Then you only need to import a single name:
from config import params
–
Glynn Clements <glynn@gclements.plus.com>
Kim Besson wrote:
Hi Glynn and rest of the list
I have tested this method by creating a config.py and a demo-example of
config and I'm not being able to use this solution. Maybe I'm not fully
understanding it (never used classes in Python):
1- I create a config1.py file in a working Python directory
2- I added this to config.py
class Params(object):
def __init__(self, filename):
params = Params("config.cfg")
3- My config.cfg has the following:
Ah= '1'
Bu= '2'
4- Just for testing, in a Python Shell I do:
from config1 import Params
(WORK)
Then:
>>> print Params
I get: <class 'config1.Params'>
So I cannot access variables inside config.cfg
What am I doing wrong?
First, Params.__init__ needs to actually read the specified file.
E.g.:
(attachments)
config.py (449 Bytes)