Is there a better way of doing this?

Cyril BAZIN cyril.bazin at gmail.com
Sun May 29 08:13:47 EDT 2005


Hi,

I don't know very well what you want to do, but if you want to parse c++, 
take a look at "GCC-XML python" (http://www.gccxml.org) and the python 
binding (http://pygccxml.sourceforge.net/). These tools translate c++ code 
to XML. Then, you can parse xml with your favorite tools and find the 
namespaces for example...

Your code don't seem very "pythonic", and it seems there is a bug... For 
example what's append if a comment contains a '}' inside a namesapce? (or a 
string contains a '{' ? 

I think you should better use most appropriate tools to do this kind of 
jobs...

Cyril


On 5/28/05, Michael <slick_mick_00 at hotmail.com> wrote:
> 
> 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]
> 
> 
> 
> 
> 
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20050529/fd407a7d/attachment.html>


More information about the Python-list mailing list