Re: [GRASS-user] Define Global Variables and use them in a Python script
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: > 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.: import grass.script as grass class Params(object): def __init__(self, filename): f = open(filename, 'r') s = f.read() f.close() self.params = grass.parse_key_val(s) def __getattr__(self, name): return self.params[name] def __getitem__(self, name): return self.params[name] def __repr__(self): return repr(self.params) # read the config file params = Params("config.cfg") This assumes that the config.cfg file uses variable=value syntax. The resulting config.params object allows the parameters to be accessed as object attributes (e.g.: config.params.something) or dictionary entries (e.g.: config.params['something']). The Python standard library has the ConfigParser module if you want more complex functionality: http://docs.python.org/library/configparser.html -- Glynn Clements ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Define Global Variables and use them in a Python script
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: So I cannot access variables inside config.cfg What am I doing wrong? Kim 2010/7/5 Glynn Clements > > 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 > ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Define Global Variables and use them in a Python script
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 ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Define Global Variables and use them in a Python script
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 > > 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 > ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Define Global Variables and use them in a Python script
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 ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
[GRASS-user] Define Global Variables and use them in a Python script
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 ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user