Python Manual Benutzerhandbuch

Seite von 124
Naming Hierarchies
21
file = "database.cfg"
errors = netsvc.loadConfig(file,None)
if errors:
errors = "Error reading %s\n%s" % (‘file‘,errors)
netsvc.Logger().notify(netsvc.LOG_DEBUG,errors)
Naming Hierarchies
If a naming hierarchy is required, the components of the hierarchy within the name should be separated
by using a period.
compiler.preprocessor.debug-level : 0
compiler.parser.debug-level : 1
compiler.code-generator.debug-level : 0
compiler.assembler.debug-level : 0
In general, the purpose of using a naming hierarchy is to associate properties with the same name with
different parts of an application, or with different instances of some object. To cater for default values,
rather than enumerating all possible objects, a wildcard can be specified in place of a single component
in a naming hierarchy. This says to match any component name in this position. Only those items
which need to be different then need to be explicitly specified.
compiler.*.debug-level : 0
compiler.parser.debug-level : 1
When a lookup is made against the database, a check is first made for any entry which matches exactly
the name of interest. If this name is not present, a search is then made of the entries containing a wild-
card. If a match is found, the value associated with the wildcard entry will be returned. If there are
multiple wildcard entries which match a lookup against the configuration database, that which has the
longest leading exact match will be used.
Environment Variables
In addition to the configuration database, an interface is also provided to the standard operating system
environment variables. Python does already provide an interface for this, however the Python interface
does have a few quirks which can sometimes make it less than useful.
One problem with the standard Python interface is that when "
os.putenv()
" is used to set an envi-
ronment variable, that variable is not then visible using "
os.getenv()
". This is because
"
os.getenv()
" uses "
os.environ
", which is a copy of the environment which is populated at
startup and any changes to environment variables are not reflected in that copy.
As such, although changes to the environment will be seen by subprocesses, they will not be visible in
the same process. This means that an environment variable can’t at the same time be used to transfer
information to a different part of the application.