473,662 Members | 2,413 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Another software design question: program-level globals

OK, here's another software design question, one that's been bugging me
for a while now. What do y'all think is the best way to handle
program-level globals, such as configuration option -- especially in
multi-module programs?

Here's one approach:

--- main.py ---
import my_globals as g
import somefuncs
import morefuncs
from optparse import OptionParser

def main():
parser = OptionParser()
parser.add_opti on( ... )
g.opts, g.args = parser.parse_ar gs()

somefuncs.do_se tup()
morefuncs.do_so me_more_setup()

somefuncs.do_so me_work()

if __name__ == '__main__':
main()
--- somefuncs.py ---
import my_globals as g

def do_setup():
if g.opts.some_opt ion:
do_it_one_way()
else:
do_it_a_differe nt_way()
--- morefuncs.py ---
import my_globals as g

def do_some_more_se tup():
for arg in g.args:
do_something_wi th(arg)
--- my_globals.py ---
(empty file)
This approach relies on the fact that modules only get imported once.
Thus when each module imports my_globals, it gets the *same* object
(which is then given the name g to make it easier to write later). So
when the attributes of g are modified in the main() function, they can
then be read by do_setup() and do_some_more_se tup(), because the object
that somefuncs.py and morefuncs.py call "g" is the same module object
that main.py also calls "g".

This is the best approach I've found so far, but I would welcome
comments and suggestions for improvements.

--
Robin Munn
rm***@pobox.com
Jul 18 '05 #1
2 1540
On Fri, 13 Feb 2004 18:43:54 GMT, Robin Munn wrote:
OK, here's another software design question, one that's been bugging
me for a while now. What do y'all think is the best way to handle
program-level globals, such as configuration option -- especially in
multi-module programs?


The first thing to do is: don't use globals. The interface between
disparate parts of your code (functions, classes, modules, packages)
should be as explicit and discoverable as possible.

Often the simplest first step in eliminating globals is to wrap them up
in a class (such as ConfigOptions) as class attributes. Other, more
elegant solutions may require refactoring the code.

--
\ "I like to go to art museums and name the untitled paintings. |
`\ 'Boy With Pail'. 'Kitten On Fire'." -- Steven Wright |
_o__) |
Ben Finney <http://bignose.squidly .org/>
Jul 18 '05 #2
"Robin Munn" <rm***@pobox.co m> wrote in message
news:KV******** ********@newssv r26.news.prodig y.com...
OK, here's another software design question, one that's been bugging me
for a while now. What do y'all think is the best way to handle
program-level globals, such as configuration option -- especially in
multi-module programs?
You've basically got it: modules are inherently singletons.
I would, of course, dress up your solution by putting the
configuration reader and so forth into a class, and possibly
putting that behind some kind of proxy so I could easily
slide in mock objects for testing.

John Roth
--
Robin Munn
rm***@pobox.com

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

24
1928
by: Uwe Mayer | last post by:
Hi, I have the following inter-class relationships: __main__: (in file LMCMain.py) imports module FileIO defines class LMCMain instanciats main = LMCMain(...) FileIO.py:
0
2493
by: Dana Morris | last post by:
Call for Participation OMG's First Annual Software-Based Communications (SBC) Workshop: From Mobile to Agile Communications http://www.omg.org/news/meetings/SBC2004/call.htm September 13-16, 2004 Washington, DC USA Introduction
6
2940
by: Gary James | last post by:
This may not be a direct C# question, but since I'll be using using C# for development, I thought I'd pose the question here. I'll soon be involved in the design of a new software product that will employ a software "Plug-In" architecture. Taking the plug-in route will give us a design that can adapt to, as yet, undefined future requirements (within the scope of the plug-in interface spec of course). In the past I've done this with...
8
1089
by: David | last post by:
and now there's Visual C# 2005 Express Edition http://lab.msdn.microsoft.com/express/vcsharp/default.aspx sigh I find it harder to choose my environment then picking up on C# !!!
11
2353
by: John Fly | last post by:
I'm working on a large project(from scratch). The program is essentially a data file processor, the overall view is this: A data file is read in, validated and stored in a memory structure similar to a database or XML representation. Rules to modify the stored data will be executed, then the data will be transformed into an output format. Think something similar to FormatA -> XML -> Manipulate XML -> FormatB
6
2133
by: JoeC | last post by:
I have a question about designing objects and programming. What is the best way to design objects? Create objects debug them and later if you need some new features just use inhereitance. Often times when I program, I will create objects for a specific purpose for a program and if I need to add to it I just add the code.
14
5878
by: shuisheng | last post by:
Dear All, I want to start a c++ software project. It is used to simulate the propagation of sound waves in complex media. It may contain 4 parts: GUI, preprocession, simulation and postprocession. Users can use GUI to model their geometry, set some parameters and show the results. The system is kind of large. I have no expierence in large codes. But I am very familiar with the physics part and what functionaloty the system should...
62
3125
by: Ajinkya | last post by:
As "spawnl" executes an exe through C code., similarly how can we compile a C program through another C program (on Windows platform) ?
3
3124
MrMancunian
by: MrMancunian | last post by:
Ok, I'm stuck on a design problem and I need some feedback how to go around it. CASE: Pathologists can request stains on certain tissues. It's possible to request more than one stain a time. The program where they make these requests (which is third-party software) creates an XML-file for one stain. So, if two stains are requested on one piece of tissue, it will create two XML-files. The program I'm writing needs to parse those XML-files,...
0
8432
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8343
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8545
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.