Re: initializing parameters class in Python only once?
On 15/07/2014 3:28 PM, Steven D'Aprano wrote: # === module params.py === class Params(object): a = 1 b = 2 @property def c(self): return self.a**2 + self.b**2 - self.a + 1 params = Params() del Params # hide the class Then callers just say: from params import params print params.c I'd replace the instantiation deletion of the class in params.py with: import sys sys.modules[__name__] = Params() ..and replace the module itself with the parameter object. I'd also add: __file__ = __file__ ...to the class definition to help with debugging. But this is really just bikeshedding. It's a shame the property decorator doesn't work at the module level, though. -- https://mail.python.org/mailman/listinfo/python-list
Re: initializing parameters class in Python only once?
On Thu, 17 Jul 2014 13:35:24 +1000, alex23 wrote: It's a shame the property decorator doesn't work at the module level, though. Not necessarily the property decorator, but some sort of computed variable would be nice. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: initializing parameters class in Python only once?
On 07/16/2014 08:35 PM, alex23 wrote: On 15/07/2014 3:28 PM, Steven D'Aprano wrote: # === module params.py === class Params(object): a = 1 b = 2 @property def c(self): return self.a**2 + self.b**2 - self.a + 1 params = Params() del Params # hide the class Then callers just say: from params import params print params.c I'd replace the instantiation deletion of the class in params.py with: import sys sys.modules[__name__] = Params() ..and replace the module itself with the parameter object. I'd also add: __file__ = __file__ ...to the class definition to help with debugging. But this is really just bikeshedding. Just make sure the 'sys.modules' assignment happens at the *end* of params.py. -- ~Ethan~ -- https://mail.python.org/mailman/listinfo/python-list
initializing parameters class in Python only once?
Hello, Pardon me for not using the proper Python language terms, but I hope that people can still understand the question: The problem: I'm writing a large Python program and I have a bunch of parameters (whose values are static) that I want to make available to the rest of the code with minimum overhead and duplicate processing. I think that the simplest way would be to create a file called Params.py and then simply have statements like a = 1, b = 2, etc. in there (no classes, no methods, just a bunch of declarations). But, some of these static parameters have to be calculated rather than simply hard-coded. I thought of creating a class called Params and having a bunch of methods (decorated with @classmethod) that set/calculate the value of all the parameters. Easy enough, but then I have to create a Params object in every source file that uses these parameters, and that seems wasteful. The actual scope of the problem is very small, so memory/cpu time is not an issue. I'm just looking for the most pythonic/elegant way of doing this. What is the recommended way of passing a bunch of static (hard-coded and calculated) parameters to various parts of the code? Thank you for any advice, Catherine -- https://mail.python.org/mailman/listinfo/python-list
initializing parameters class in Python only once?
Hello, Pardon me for not using the proper Python language terms, but I hope that people can still understand the question: The problem: I'm writing a large Python program and I have a bunch of parameters (whose values are static) that I want to make available to the rest of the code with minimum overhead and duplicate processing. I think that the simplest way would be to create a file called Params.py and then simply have statements like a = 1, b = 2, etc. in there (no classes, no methods, just a bunch of declarations). But, some of these static parameters have to be calculated rather than simply hard-coded. I thought of creating a class called Params and having a bunch of methods (decorated with @classmethod) that set/calculate the value of all the parameters. Easy enough, but then I have to create a Params object in every source file that uses these parameters, and that seems wasteful. The actual scope of the problem is very small, so memory/cpu time is not an issue. I'm just looking for the most pythonic/elegant way of doing this. What is the recommended way of passing a bunch of static (hard-coded and calculated) parameters to various parts of the code? Thank you for any advice, Catherine -- https://mail.python.org/mailman/listinfo/python-list
Re: initializing parameters class in Python only once?
On Mon, 14 Jul 2014 15:24:26 -0700 Catherine M Moroney catherine.m.moro...@jpl.nasa.gov wrote: Hello, Pardon me for not using the proper Python language terms, but I hope that people can still understand the question: The problem: I'm writing a large Python program and I have a bunch of parameters (whose values are static) that I want to make available to the rest of the code with minimum overhead and duplicate processing. I think that the simplest way would be to create a file called Params.py and then simply have statements like a = 1, b = 2, etc. in there (no classes, no methods, just a bunch of declarations). But, some of these static parameters have to be calculated rather than simply hard-coded. I thought of creating a class called Params and having a bunch of methods (decorated with @classmethod) that set/calculate the value of all the parameters. Easy enough, but then I have to create a Params object in every source file that uses these parameters, and that seems wasteful. The actual scope of the problem is very small, so memory/cpu time is not an issue. I'm just looking for the most pythonic/elegant way of doing this. What is the recommended way of passing a bunch of static (hard-coded and calculated) parameters to various parts of the code? Thank you for any advice, Catherine You're 90% of the way there. You can just create params.py, fill it with statements that are either hard-coded or calculated as necessary, and import params from all over the rest of the program. Once the interpreter has imported it for the first time, everyone else just gets a link to the same instance of the module rather than running it all over and over again. -- Rob Gaddi, Highland Technology -- www.highlandtechnology.com Email address domain is currently out of order. See above to fix. -- https://mail.python.org/mailman/listinfo/python-list
Re: initializing parameters class in Python only once?
On Mon, Jul 14, 2014 at 3:24 PM, Catherine M Moroney catherine.m.moro...@jpl.nasa.gov wrote: Hello, Pardon me for not using the proper Python language terms, but I hope that people can still understand the question: The problem: I'm writing a large Python program and I have a bunch of parameters (whose values are static) that I want to make available to the rest of the code with minimum overhead and duplicate processing. I think that the simplest way would be to create a file called Params.py and then simply have statements like a = 1, b = 2, etc. in there (no classes, no methods, just a bunch of declarations). But, some of these static parameters have to be calculated rather than simply hard-coded. Within a module (such as Params.py), you can put any statements or code you want, without needing classes or functions. Depending on the complexity of the calculations, you may want to put some of the functionality into functions, which could then be run on import (just put the function call at the bottom of the module file). The interpreter will take care to only import one copy, with the exception of if the Params.py file is accessible from multiple paths in sys.path (generally not an issue). For example, the following is a valid module (untested): # Params.py a = 1 b = 5 c = a * b + 42 d = c - b # End Params.Py You could also go farther and read external configuration files at the root scope. The main thing to remember is that all intermediate variables will be visible outside, and thus should be prefixed with an underscore (Python's standard for private). I thought of creating a class called Params and having a bunch of methods (decorated with @classmethod) that set/calculate the value of all the parameters. Easy enough, but then I have to create a Params object in every source file that uses these parameters, and that seems wasteful. If all the methods of a class as decorated with @classmethod or @staticmethod, and all variables are declared on the class, and not inside of any non-class/static methods (include __init__), you would not need to create instances of the object. For example, if you have a class like follows (untested): class Params(): myVar = 0 @classmethod def myMethod(cls): cls.myVar += 1 return cls.myVar you can access them without an instance, by using Params.myVar or Params.myMethod(). The actual scope of the problem is very small, so memory/cpu time is not an issue. I'm just looking for the most pythonic/elegant way of doing this. For most configuration, I would recommend using a combination of the above. Create the module and place some configuration variables inside the module directly. Use classes (which shouldn't be instantiated) for namespacing. If you start to get a LOT of parameters, you may also want to consider making the entire params a package (folder/directory) with multiple modules and possibly classes, however that is likely overkill. Note that, in any case, I would recommend treating the parameters as constants, meaning do not change their values, except during the initialization of the parameters module. This is mostly to ensure thread-safety and prevent difficult to track bugs. What is the recommended way of passing a bunch of static (hard-coded and calculated) parameters to various parts of the code? Thank you for any advice, Catherine -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: initializing parameters class in Python only once?
On Tue, Jul 15, 2014 at 8:32 AM, Catherine M Moroney catherine.m.moro...@jpl.nasa.gov wrote: The actual scope of the problem is very small, so memory/cpu time is not an issue. I'm just looking for the most pythonic/elegant way of doing this. Small job? Use the simplest possible technique. Just create params.py with a bunch of assignments in it: # params.py a = 1 b = 2 c = a + b # every other file import params print(c is,params.c) # if you need to change anything: params.c += 5 # everyone else will see the change, because there can be # only one instance of the module (Highlander!) Works nicely for anything even moderately complex. Also serves as a convenient way to separate configs from code; for instance, I do this any time I need to have a program with database passwords, or per-installation setup, or stuff like that. Two examples: https://github.com/Rosuav/Yosemite/blob/master/config.py https://github.com/Rosuav/Flask1/blob/master/1.py In the latter case, config.py doesn't even exist in the repository, as its main purpose is to store the database connection string - both private (don't want that published on Github) and per-installation (my dev and production systems use different connection strings). The Yosemite config is actually a bit legacy now; I used to have two distinctly different instances of it, one running on Windows and the other on Linux, but now I have a large number of identical instances (all on Linux and all referencing the same disk server - which, incidentally, is the one that I've weaponized with Alice, Elsa, Anya, a Vorpal blade, and a Portal turret). Either way, though, config.py consists generally of simple assignments (and comments), but it's most welcome to use all the power of Python to calculate values. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: initializing parameters class in Python only once?
Catherine M Moroney catherine.m.moro...@jpl.nasa.gov writes: The problem: I'm writing a large Python program and I have a bunch of parameters (whose values are static) that I want to make available to the rest of the code with minimum overhead and duplicate processing. Better than putting these in executable code, then, is to put them in a configuration file read as *data*, not code, when your program starts. Look at the ‘configparser’ module from the Python standard library URL:https://docs.python.org/3/library/configparser.html for a robust way to read run-time configuration parameters from a non-executable file. The result of reading the config file (or set of them, if your use case is complex enough) is an object containing the parameters, which can be interrogated as normal by getting its attributes. See the documentation for examples. -- \“[T]he great menace to progress is not ignorance but the | `\ illusion of knowledge.” —Daniel J. Boorstin, historian, | _o__)1914–2004 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: initializing parameters class in Python only once?
On 14/07/2014 23:32, Catherine M Moroney wrote: Hello, Pardon me for not using the proper Python language terms, but I hope that people can still understand the question: The problem: I'm writing a large Python program and I have a bunch of parameters (whose values are static) that I want to make available to the rest of the code with minimum overhead and duplicate processing. I think that the simplest way would be to create a file called Params.py and then simply have statements like a = 1, b = 2, etc. in there (no classes, no methods, just a bunch of declarations). But, some of these static parameters have to be calculated rather than simply hard-coded. I thought of creating a class called Params and having a bunch of methods (decorated with @classmethod) that set/calculate the value of all the parameters. Easy enough, but then I have to create a Params object in every source file that uses these parameters, and that seems wasteful. The actual scope of the problem is very small, so memory/cpu time is not an issue. I'm just looking for the most pythonic/elegant way of doing this. What is the recommended way of passing a bunch of static (hard-coded and calculated) parameters to various parts of the code? Thank you for any advice, Catherine Besides the answers you've already had, you might like to consider using the enum module for some of your parameters. IIRC it's only available in 3.4 but there's what I understand to be pretty much the same thing on pypi. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence --- This email is free from viruses and malware because avast! Antivirus protection is active. http://www.avast.com -- https://mail.python.org/mailman/listinfo/python-list
Re: initializing parameters class in Python only once?
On Mon, 14 Jul 2014 15:24:26 -0700, Catherine M Moroney wrote: The problem: I'm writing a large Python program and I have a bunch of parameters (whose values are static) that I want to make available to the rest of the code with minimum overhead and duplicate processing. I think that the simplest way would be to create a file called Params.py and then simply have statements like a = 1, b = 2, etc. in there (no classes, no methods, just a bunch of declarations). That sounds exactly right to me. But, some of these static parameters have to be calculated rather than simply hard-coded. I don't think that should be a problem. You can include calculations: a = 1 b = 2 c = a**2 + b**2 - a + 1 The only difficulty is if you want those values to be calculated on demand, rather than at program startup. If that's the case, I think the best solution is to turn them into functions: def c(): return a**2 + b**2 - a + 1 This, sadly, requires you to tell the difference between params that should be constants and those which should be function calls. If that is a serious problem, you should consider a singleton class with properties, but if the simpler solution works for you, stick to the simpler solution! I thought of creating a class called Params and having a bunch of methods (decorated with @classmethod) that set/calculate the value of all the parameters. Easy enough, but then I have to create a Params object in every source file that uses these parameters, and that seems wasteful. I don't think that Params with class methods is the best solution. I would do something like this: # === module params.py === class Params(object): a = 1 b = 2 @property def c(self): return self.a**2 + self.b**2 - self.a + 1 params = Params() del Params # hide the class Then callers just say: from params import params print params.c All modules now see the same params instance, so it is (almost) a singleton. -- Steven -- https://mail.python.org/mailman/listinfo/python-list