473,837 Members | 1,917 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Import config into global space?

I'm writing a module, and it's getting too big to cope with as it has
a lot of configuration in it - constants, dictionaries etc; it'd be
nice to externalise this and keep my module down to just classes/defs
etc.

What I'd like to do is have myprog.py import config.py and be able to
reference the configuration variables globally - i.e. I could just
reference myvar instead of config.myvar

So I'd have something like this:

config.py
=========

ID_TOOLBAR = 105

myprog.py
=========

import config
print ID_TOOLBAR

result
======
105


It doesn't seem to work though, as global is limited to modules as far
as I know.

Any ideas how to do this, it's basically like a require or #include in
Perl/C...
Jul 18 '05 #1
5 4421
> config.py
=========

ID_TOOLBAR = 105

myprog.py
=========

import config
print ID_TOOLBAR

result
======


from config import *

- Josiah
Jul 18 '05 #2
Josiah Carlson <jc******@uci.e du> wrote:

[global import from file]
from config import *


Oh man! I can't believe it was that simple and I didn't think of it!

Actually, I've gone with the config.variable syntax for now anyway, it
actually makes more sense as I don't have to name my variables like
fontsize_config to differentiate them.
Jul 18 '05 #3
simo wrote:
Josiah Carlson <jc******@uci.e du> wrote:

[global import from file]

from config import *

Oh man! I can't believe it was that simple and I didn't think of it!

Actually, I've gone with the config.variable syntax for now anyway, it
actually makes more sense as I don't have to name my variables like
fontsize_config to differentiate them.

In general, "from module import *" is generally frowned upon, if only
because it can clobber your module's namespace. There are certainly
exceptions, but namespaces are useful.

- Josiah
Jul 18 '05 #4
Josiah Carlson <jc******@uci.e du> wrote:

[snip]
from config import *
Actually, I've gone with the config.variable syntax for now anyway, it
actually makes more sense as I don't have to name my variables like
fontsize_config to differentiate them.
In general, "from module import *" is generally frowned upon, if only
because it can clobber your module's namespace. There are certainly
exceptions, but namespaces are useful.


Yes, that's what I thought, one of the reasons I'm going to go with
the config.variable syntax after all.

I'm moving to the wxWidgets 2.5 way of using the wx namespace too
(e.g. wx.ListCtrl instead of wxListCtrl).
Jul 18 '05 #5
> Yes, that's what I thought, one of the reasons I'm going to go with
the config.variable syntax after all.

I'm moving to the wxWidgets 2.5 way of using the wx namespace too
(e.g. wx.ListCtrl instead of wxListCtrl).


That's good, because wxListCtrl won't be available in 2.5, wx.ListCtrl
will be. *wink*

- Josiah
Jul 18 '05 #6

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

Similar topics

16
2766
by: Manlio Perillo | last post by:
Hi. I'm a new user of Python but I have noted a little problem. Python is a very good language but it is evolving, in particular its library is evolving. This can be a problem when, ad example, a module change its interface or its implementation in a fundamental way (an example: wxPython). This, I think, can be resolved by allowing an user to explicitly say what version of a module it wants (sush as version numbers in Linux shared...
17
1891
by: Jacek Generowicz | last post by:
I have a module whose behaviour needs to be configurable. The module needs to decide, the first time it is imported, beteween alternative interfaces it presents. Currently, I set some environment variables which select the desired behaviour, and the module inspects those variables to determine the mode in which it should set itself up. I would prefer a pure Python solution, rather than one which depends on external state. Can you...
0
1670
by: Simon John | last post by:
I have a program that consists of one main module and lots of small sub-modules. In the main module I open a text file and grep for a language setting, this language setting will then be used as the module name of a config file to import. E.g. "Language=en" means config_en.py is imported as config, "Language=fr" means config_fr.py is imported as config....
0
1010
by: John Wallace | last post by:
Hi, I have an initial build of an ASP.NET/C# application (based around a "custom control" with some supporting webforms) that contains the usual mix of: 1) ASP files 2) A single DLL (containing all the classes and code-behind I believe) 3) A supporting folder structure 4) Some data/config files used by the app.
3
2194
by: Michael Brenner | last post by:
Hi, I'm implementing a plugin-based program, structured like the example below (where m1 in the main module, loading m2 as a plugin). I wanted to use a single global variable (m1.glob in the example) to store some config data that the plugins can access. However, the output shown belown seems to imply that glob is *copied* or recreated during the import in m2. Am I missing something? I thought m1 should be in sys.modules and not be...
2
12434
by: HMS Surprise | last post by:
Greetings. What is the implication of the error message 'cannot import name .....'? It occurs when executing the line: from nBaseTest import nBaseTest The file exists and the class within it exists. Changing it to from nBaseTest import x
16
1432
by: Stef Mientki | last post by:
This might be a very weird construction, but it's the most easy way in translating another language into Python (for simulation). Although it works, I like to know if this a valid construction: I've defined a class, like this, attaching a not yet defined global to itself class T6963_device (tDevice): def __init__ (self):
5
4516
by: =?Utf-8?B?bWNxd2VydHk=?= | last post by:
Hello, I'm trying to enable an error handling package at the root of my web server by editing this file: C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\CONFIG\web.config In the httpHandlers section I've added: <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah, Version=1.0.9414.0, Culture=neutral, PublicKeyToken=42734318a98ac1df"
6
26341
by: provor | last post by:
Hello, I have the following code that I am using when a user presses a button to import an excel file into a table. The code is hard coded to point to the correct table. This works great for this one table. My problem is I have two buttons I want to use this code for for the two buttons would put the data in different tables. I have tried copying and changing a few things and nothing will work for me. The code is set up in a module and then I...
0
9843
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
10570
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10626
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
10273
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...
1
7806
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
7001
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
5670
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...
1
4474
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4041
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.