Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #74446

Re: initializing "parameters" class in Python only once?

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.albasani.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; 'subject:Python': 0.06; '(all': 0.07; 'dev': 0.07; 'linux,': 0.07; 'nicely': 0.07; 'string': 0.09; 'latter': 0.09; 'params': 0.09; 'url:github': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'windows': 0.15; 'assignments': 0.16; 'examples:': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'referencing': 0.16; 'simplest': 0.16; 'subject:class': 0.16; 'subject:parameters': 0.16; 'url:py': 0.16; 'which,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; 'small,': 0.19; 'import': 0.22; 'issue.': 0.22; 'separate': 0.22; 'cc:addr:python.org': 0.22; 'config': 0.24; 'convenient': 0.24; 'instance,': 0.24; 'setup,': 0.24; 'cc:2**0': 0.24; "i've": 0.25; '15,': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'generally': 0.29; "doesn't": 0.30; 'change,': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'that.': 0.31; '(my': 0.31; 'bunch': 0.31; 'values.': 0.31; 'file': 0.32; 'this.': 0.32; 'stuff': 0.32; 'linux': 0.33; 'everyone': 0.33; 'running': 0.33; 'actual': 0.34; 'problem': 0.35; 'connection': 0.35; 'case,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'disk': 0.36; 'instances': 0.36; 'doing': 0.36; 'possible': 0.36; 'subject:?': 0.36; 'two': 0.37; 'server': 0.38; 'anything': 0.39; 'subject:" ': 0.39; 'though,': 0.39; 'either': 0.39; 'even': 0.60; 'consists': 0.60; 'most': 0.60; 'simple': 0.61; 'different': 0.65; 'production': 0.68; 'portal': 0.68; 'published': 0.71; 'jul': 0.74; 'power': 0.76; 'catherine': 0.84; 'moderately': 0.84; 'technique.': 0.84; 'url:config': 0.84; 'url:master': 0.84; 'passwords,': 0.91; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=twO+R+am6uqZaLrS5MvMs/yI1bAQmmiLiBIp2EnVBR4=; b=KW1FtybqkksqpIW5zXdmU8gNFPjwZFuDKj62XnADJPavqmbbW9BByKBHLON1Z9AS2r cSTA9kbj49yR2Vp7lBhn0URoE/kWJocrkkm0Plq+LE5r+voQ4hBmszbzcskBgWPDeoCT 7ojC/2mcFRYdRA3nrDwCeNKD7wNcemkvCjAsuxgndbpcdeFFNPY2ZmmxkQhRacoYyFjd ynWTT0Cbn6khfXkUdyJMtRWF2bwptT+pj4Lj7/ujhzdpFL7QTtNnEuK8qrfPTxkGYoNB /pVVNIYP2i3tsajnI8lzGyCuVdsce+L3zqjYWUKzIUoYGpO+l+4mKDSLYSeCmlX/MbYR b2cA==
MIME-Version 1.0
X-Received by 10.221.64.80 with SMTP id xh16mr3051072vcb.35.1405381308499; Mon, 14 Jul 2014 16:41:48 -0700 (PDT)
In-Reply-To <53C45A6D.9040401@jpl.nasa.gov>
References <53C45A6D.9040401@jpl.nasa.gov>
Date Tue, 15 Jul 2014 09:41:48 +1000
Subject Re: initializing "parameters" class in Python only once?
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.11813.1405381311.18130.python-list@python.org> (permalink)
Lines 43
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1405381311 news.xs4all.nl 2842 [2001:888:2000:d::a6]:37906
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:74446

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web