Python Manual Benutzerhandbuch

Seite von 124
Program Setup
22
To lookup the value of an environment variable the function "
lookupEnviron()
" is used. If a new
environment variable needs to be set, or an existing value changed, the function "
mergeEnvi-
ron()
" is used. Any changes to the environment variables will be visible immediately, but there is no
way to get a list of all environment variables which are set. When a lookup is made but no such envi-
ronment variable exists, the value
None
is returned.
netsvc.mergeEnviron("PWD",os.getcwd())
print netsvc.lookupEnviron("PWD")
In addition to these functions, the function "
expandEnviron()
" is provided. This function accepts
a string and replaces any reference to an environment variable specified using Bourne shell syntax,
with that environment variables actual value. The intent in providing this function is that it can be used
in conjunction with the configuration database, allowing configuration items to refer to environment
variables.
application.log-files : ${HOME}/logs
Note that the expansion isn’t automatic when a lookup is made against the configuration database. The
application code will have to explicitly expand the value obtained form the configuration database.
value = netsvc.lookupConfig("application.log-files")
directory = netsvc.expandEnviron(value)
Unique Identifiers
In many applications, it is often useful to be able to create abstract identifiers to uniquely identify ob-
jects or resources. These might be used to identify user sessions in a web based application, specific
requests in a distributed messaging system, or even the particular service agent which a request in a
distributed messaging system is targeted at.
Such identifiers may only need to be unique within the context of the lifetime of the application, or
possibly may need to be globally unique. In the case of the latter, to be rigourous this would normally
require an external database to be maintained which tracks what identifiers have been used. In most
cases however, it is not necessary to go to that extent and a simplistic means can be used to generate a
psuedo unique identifier which is sufficient.
To generate such identifiers the function "
uniqueId()
" is provided. The function can provide iden-
tifiers in either a short or long format. In the short format, the identifier contains components which
identify the host on which the process is running, the process id and an incremental counter. In the long
format, time values are also included which tie the identifier to an instant in time.
id1 = netsvc.uniqueId(netsvc.ID_SHORT_FORMAT)
id2 = netsvc.uniqueId(netsvc.ID_LONG_FORMAT)
If you wish to incorporate your own prefix into the identifier, an optional second argument can be sup-
plied to the "
uniqueId()
" function.