getting values of global variables?

Holger Jannsen holger at phoenix-edv.netzservice.de
Fri Jul 16 07:37:42 EDT 1999


Hugh Gordon,

remember me to spent an cup of ice, when we meet us;-))

Gordon McMillan wrote:
> 
> Holger Jannsen writes:
> >
> > ok, sounds easy, but I get a little confused...;-(
> 
> That's because what's confusing you isn't what you think is confusing
> you!

Ah, yeah!!!!

> > ...
> >   splitter=re.split('(%.*?%)',aString)
> >  except:
> >   return aString
> 
> Judging by the format of your input, a better regex would be
> '%[^ ]*?%' - which give you ' %1 %2 ' instead of a phony match.

Thanx for involving, but [sorry;-)] you've gorgotten that there could be 
spaces within that tokens, too. Check it out by setting a 
environment-variable like "set HEIN BLOED=ON". You would have missed
that token in common with execution of your expression.
That's a real problem, because there could be spaces in keys of
possible other directories, too. E.g.:
aDict={}
aDict["my house"]="very nice"
Ok, I could restrict the tokens to be without any spaces, but it's
not really the thing i like... (see below!)

> >...[better not show again;-))]
> Sorry, Holger, but that's a mess! Something like this will get you
> away from all those special cases:
> 
>  rslt = []
>  for tok in splitter:
>      if tok[0] != '%' or tok[-1]!= '%':
>          rslt.append(tok)       #not a special token
>      else:
>          test = tok[1:-1]               #strip those '%'s
>          rslt.append(checkForToken(test))
>  return string.join(rslt)

Ok, I see, but the right way is:
return string.joinfields(rslt, '')

> > def checkForToken(aString):
> >  "checks a string for known tokens"
> >  try:
> >   aTok = eval(aString)
> >   if aTok==float(aString): #don't exchange numbers!
> >    return None
> 
> Well, eval('HEIN') does indeed produce 'Blöd'. But float('HEIN')
> raises an exception, which is caught by code that assumes it's "eval"
> that failed.

Ooouch! That's one of the 'four blind mice', isn't it?

> >...
> How about (leaving out directories and files, which you haven't
> included):
> 
> def checkForToken(aString):
>  "checks a string for known tokens"
>  print 'checking', aString
>  try:
>   aTok = eval(aString)
>  except:
>   aTok = os.environ.get(aString)
>   if aTok==None:
>    aTok = aString
>  if type(aTok) != type(''):
>      aTok = str(aTok)
>  return aTok

The directories- and file-dictionaries do I get from another class of
that module. They are not important to understand the difficulties of that problem. 
Thy just act like os.environ (see below).

I think my problem is to get that values of the dict.keys with spaces 
inside or appending, so e.g. "%HEIN BLOED%" or "%HEIN %".
They are not possible as global variables inside python-code, but
even in all dictionaries e.g. like os.environ!

And there is another difficulty problem: 
I declared, that a standalone string also have to be tested, so e.g. 
egalizeTokens("WINDIR") must return "c:\WinNT". It's the same
as egalizeTokles("%WINDIR%")!
Therefore I put
	if len(splitter)==1:
		return checkForToken(aString)                
inside that function egalizeTokens.
OK. But what to do with several strings, that could be interpreted
as numbers??? So I often have to interprete version-strings like '1-2-22'.
eval('1-2-22') = -24!!!;-(
How could I forbid that, but let %HEIN%=3 an allowed token? Because of that
I'd written that code with 'if float(...)'!

Do ya' understand my problem?

Ciao,
Holger




More information about the Python-list mailing list