Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #74441 > unrolled thread
| Started by | Catherine M Moroney <Catherine.M.Moroney@jpl.nasa.gov> |
|---|---|
| First post | 2014-07-14 15:32 -0700 |
| Last post | 2014-07-14 22:55 -0500 |
| Articles | 4 — 4 participants |
Back to article view | Back to comp.lang.python
initializing "parameters" class in Python only once? Catherine M Moroney <Catherine.M.Moroney@jpl.nasa.gov> - 2014-07-14 15:32 -0700
Re: initializing "parameters" class in Python only once? Chris Angelico <rosuav@gmail.com> - 2014-07-15 09:41 +1000
Re: initializing "parameters" class in Python only once? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-07-15 01:05 +0100
Re:initializing "parameters" class in Python only once? Dave Angel <davea@davea.name> - 2014-07-14 22:55 -0500
| From | Catherine M Moroney <Catherine.M.Moroney@jpl.nasa.gov> |
|---|---|
| Date | 2014-07-14 15:32 -0700 |
| Subject | initializing "parameters" class in Python only once? |
| Message-ID | <53C45A6D.9040401@jpl.nasa.gov> |
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
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-07-15 09:41 +1000 |
| Message-ID | <mailman.11813.1405381311.18130.python-list@python.org> |
| In reply to | #74441 |
On Tue, Jul 15, 2014 at 8:32 AM, Catherine M Moroney
<Catherine.M.Moroney@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
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-07-15 01:05 +0100 |
| Message-ID | <mailman.11816.1405382722.18130.python-list@python.org> |
| In reply to | #74441 |
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
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2014-07-14 22:55 -0500 |
| Message-ID | <mailman.11823.1405396545.18130.python-list@python.org> |
| In reply to | #74441 |
Catherine M Moroney <Catherine.M.Moroney@jpl.nasa.gov> Wrote in
message:
> 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.
No, you don't want to create separate instances of the Params
class. You want to create exactly one object, and have each
source file get their attributes from the same object.
That object could be the same module object as already mentioned.
Or it could be an object created in the module exactly once. I
like the latter method and I'll explain why after showing
how.
In parametersetup.py, you do something like:
class Foo: # assuming python 3.x
def __init__(self ):
self.parm1 = 42
self.otherparm =3
parms = Foo () # this is the sole instance and its only created here.
del Foo # in case you want to make sure.
In each source file you have something like:
from parametersetup import parms
and you refer to parms.param1
Now, why the extra fuss? In case you later want a different set of
parms for each thread, or for certain modules, or for debugging.
You're halfway there.
--
DaveA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web