Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!newsfeed0.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'parameters': 0.04; 'static': 0.04; 'classes,': 0.05; 'interpreter': 0.05; 'root': 0.05; 'subject:Python': 0.06; 'class,': 0.07; 'duplicate': 0.07; 'intermediate': 0.07; 'paths': 0.07; 'problem:': 0.07; 'processing.': 0.07; 'variables': 0.07; 'methods,': 0.09; 'params': 0.09; 'statements': 0.09; 'terms,': 0.09; 'underscore': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'mostly': 0.14; '(include': 0.16; '(just': 0.16; '@classmethod': 0.16; 'advice,': 0.16; 'bugs.': 0.16; 'code?': 0.16; 'declared': 0.16; 'file).': 0.16; 'parameters,': 0.16; 'prefixed': 0.16; 'scope.': 0.16; 'simplest': 0.16; 'subject:class': 0.16; 'subject:parameters': 0.16; 'sys.path': 0.16; "\xc2\xa0i'm": 0.16; 'exception': 0.16; 'prevent': 0.16; 'language': 0.16; 'wrote:': 0.18; 'module': 0.19; 'passing': 0.19; 'small,': 0.19; 'seems': 0.21; 'import': 0.22; 'issue.': 0.22; 'cc:addr:python.org': 0.22; 'creating': 0.23; '(such': 0.24; 'instance,': 0.24; "shouldn't": 0.24; 'visible': 0.24; 'mon,': 0.24; 'cc:2**0': 0.24; 'source': 0.25; 'possibly': 0.26; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'function': 0.29; 'rest': 0.29; 'external': 0.29; 'thus': 0.29; 'skip:@ 10': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; '(which': 0.31; 'code': 0.31; 'bunch': 0.31; 'calculated': 0.31; 'follows': 0.31; 'object.': 0.31; 'overhead': 0.31; 'parameters.': 0.31; 'question:': 0.31; 'file': 0.32; 'class': 0.32; 'this.': 0.32; 'run': 0.32; 'url:python': 0.33; 'actual': 0.34; 'received:74.125.82': 0.34; 'could': 0.34; 'problem': 0.35; 'classes': 0.35; 'except': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; '14,': 0.36; 'accessible': 0.36; 'combination': 0.36; 'functions.': 0.36; 'instances': 0.36; 'module.': 0.36; 'url:listinfo': 0.36; 'doing': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'example,': 0.37; 'minimum': 0.38; 'thank': 0.38; 'files': 0.38; 'pm,': 0.38; 'track': 0.38; 'rather': 0.38; 'that,': 0.38; 'skip:& 20': 0.39; 'received:74.125': 0.39; 'subject:" ': 0.39; 'skip:p 20': 0.39; 'called': 0.40; 'url:mail': 0.40; 'ensure': 0.60; 'read': 0.60; 'easy': 0.60; 'most': 0.60; 'hope': 0.61; 'entire': 0.61; 'simply': 0.61; 'making': 0.63; 'skip:n 10': 0.64; '8bit%:10': 0.64; 'needing': 0.65; 'within': 0.65; 'bottom': 0.67; 'jul': 0.74; 'catherine': 0.84; 'complexity': 0.84; 'pardon': 0.84; 'treating': 0.84; 'directly.': 0.95 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc:content-type; bh=UKCmT2iE+ug13x5fYBPiO7LxIxF+dJgfWEgLpVZCszA=; b=TEuSDh+t1A3fhnZfy5j56kA4DeEqomumUG8fEoJFLvLemBJfXG0XmBBT+ofCQ8HJY/ LHRbqHH97h/UZONtQt6i+J83LOi0g9/B+V148ebxjk5BgSQ7Lt8CQIz6fIqu2yZ3w3YF Uej0ePxYNFbh7PZkbo7RwnfBNtWupHxepmCn0kjU4NXrLzTBA2ahC+y3KHKta07/ZsIM NBjgC9WKL9iMt8DaTEMbY9ys5f6/RvLT+zQ1PGvr5Geea1oI763KzJFCfax8pqQ2ZggD DVJsfm/pEQNeRB3FuEV6J5kZI09eDYw+0dA5QKyHPsVsGZW8c+gCtpGXEs7dqCOesolK 0Frg== X-Gm-Message-State: ALoCoQmUpzTKU6PjKvjZnasyQtL7yVuSpCm3d33UJp7EOn15Qos9chVcqow+8UskjJPvnkt3aeQB X-Received: by 10.180.105.68 with SMTP id gk4mr1136915wib.24.1405380107373; Mon, 14 Jul 2014 16:21:47 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <53C4589A.9040109@jpl.nasa.gov> References: <53C4589A.9040109@jpl.nasa.gov> From: Chris Kaynor Date: Mon, 14 Jul 2014 16:21:27 -0700 Subject: Re: initializing "parameters" class in Python only once? To: Catherine M Moroney Content-Type: multipart/alternative; boundary=f46d04426a601332ab04fe2f8e97 Cc: "python-list@python.org" X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 200 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1405380114 news.xs4all.nl 2864 [2001:888:2000:d::a6]:56883 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:74444 --f46d04426a601332ab04fe2f8e97 Content-Type: text/plain; charset=UTF-8 On Mon, Jul 14, 2014 at 3:24 PM, Catherine M Moroney < Catherine.M.Moroney@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 > --f46d04426a601332ab04fe2f8e97 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
On M= on, Jul 14, 2014 at 3:24 PM, Catherine M Moroney <Catherine= .M.Moroney@jpl.nasa.gov> wrote:
Hello,

Pardon me for not using the proper Python language terms, but I hope that p= eople can still understand the question:

The problem: =C2=A0I'm writing a large Python program and I have a bunc= h 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 =3D 1, b =3D 2, etc.
in there (no classes, no methods, just a bunch of declarations). =C2=A0But,= some of these static parameters have to be calculated rather than simply h= ard-coded.

Within a module (such as &qu= ot;Params.py"), you can put any statements or code you want, without n= eeding classes or functions. Depending on the complexity of the calculation= s, you may want to put some of the functionality into functions, which coul= d then be run on import (just put the function call at the bottom of the mo= dule file).

The interpreter will take care to only import one copy,= with the exception of if the Params.py file is accessible from multiple pa= ths in sys.path (generally not an issue).

For exam= ple, the following is a valid module (untested):
# Params.py
a =3D 1
b =3D 5
c =3D a * b = + 42
d =3D c - b
# End Params.Py

You could also go farther and read external configuration files at the r= oot scope. The main thing to remember is that all intermediate variables wi= ll be visible outside, and thus should be prefixed with an underscore (Pyth= on'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. =C2=A0Easy 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 dec= lared 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():
=C2=A0 =C2=A0 myVar = =3D 0
=C2=A0 =C2=A0 @classmethod
=C2=A0 =C2=A0 def myMe= thod(cls):
=C2=A0 =C2=A0 =C2=A0 =C2=A0 cls.myVar +=3D 1
=C2=A0 =C2=A0 =C2=A0 =C2=A0 return cls.myVar

= you can access them without an instance, by using Params.myVar or Params.my= Method().
=C2=A0
The actual scope of the problem is very small, so memory/cpu time is not an issue. =C2=A0I'm just looking for the most pythonic/elegant way of d= oing this.

For most configuration, I wo= uld recommend using a combination of the above. Create the module and place= some configuration variables inside the module directly. Use classes (whic= h 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 pa= ckage (folder/directory) with multiple modules and possibly classes, howeve= r 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 threa= d-safety and prevent difficult to track bugs.
=C2=A0

What is the recommended way of passing a bunch of static (hard-coded and ca= lculated) parameters to various parts of the code?

Thank you for any advice,

Catherine
--
https://mail.python.org/mailman/listinfo/python-list

--f46d04426a601332ab04fe2f8e97--