getting values of global variables?

Gordon McMillan gmcm at hypernet.com
Thu Jul 15 11:16:57 EDT 1999


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!

> I'd like to parse over a file reading line per line.
> I want to write everything through a special filter
> which exchanges known tokens. This may be special
> dictionaries or global variables, too.
> The tokens have to be surrounded by '%'-charcter.
> 
> Getting a problem with the global varaibles. I couldn't
> get their values as string and havn't found the help
> in books yet. I think it's quite simple and I'm just a 
> "four blind mice".;-)
> 
> To check the filter I wrote:
> 
> -------------
> import string, os, re
> HEIN="Blöd"
> 
> def egalizeTokens(aString):
>  "changes known tokens in a string"
>  try:
>   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.

>  for s in splitter:
>   print s
>   if len(splitter)==1:
>    aTok = checkForToken(s)
>   else:
>    ox = string.split(s, '%')
>    if len(ox)==1:
>     aTok = ox[0]
>    else:
>     aTok = checkForToken(ox[1])
>   if aTok==None:
>    splitter[splitter.index(s)] = s
>   else:			
>    splitter[splitter.index(s)] = aTok	
>  try:
>   aTok = string.joinfields(splitter,'')
>  except:
>   aTok = splitter[0] #only one element!
>  return aTok

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)

> 
> 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.

>   return aTok
>  except:
>   aTok = os.environ.get(aString)
>   if aTok==None:			
>    aTok = directories.get(aString)
>    if aTok==None:
>     aTok = files.get(aString)
>   return aTok

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

> if __name__=='__main__':
>  print egalizeTokens("This is a test! %WINDIR% %1 %2 %HEIN%. That's
>  it") raw_input ("Press RETURN!")
> -------------------------------
> 
> Perhaps there's a much easier way to get what I want????
> (Still I am a newbie..;-)))
> 
> 
> 
> many thanx,
> Holger
> 
> -- 
> http://www.python.org/mailman/listinfo/python-list

- Gordon




More information about the Python-list mailing list