Is there a better way of doing this?

Michael slick_mick_00 at hotmail.com
Sat May 28 09:24:19 EDT 2005


Hi,
I'm fairly new at Python, and have the following code that works but isn't
very concise, is there a better way of writing it?? It seems much more
lengthy than python code i have read..... :-)
(takes a C++ block and extracts the namespaces from it)



def ExtractNamespaces(data):
 print("Extracting Namespaces")
 p = re.compile( 'namespace (?P<name>[\w]*)[\n\t ]*{')

 subNamespaces = []

 newNS = p.search(data)
 while( newNS ):
  print "\t" + newNS.group("name")

  OPCount = 1
  Offset = newNS.end()
  while(OPCount > 0):
   if( data[Offset] == "}" ):
    OPCount = OPCount -1;
   elif( data[Offset] == "{" ):
    OPCount = OPCount + 1;
   Offset = Offset+1;

  #Extract Data:
  newNSData = data[newNS.end():Offset-1]
  data = data[0:newNS.start()] + data[Offset:]
  newNamespace = [newNS.group("name"), newNSData];
  subNamespaces.append(newNamespace)

  #Perform NewSearch
  newNS = p.search(data)
 return [subNamespaces,data]









More information about the Python-list mailing list