[Pyparsing] Naming elements based on their predecessor
Brought to you by:
ptmcg
From: Joshua J. K. <jk...@sk...> - 2008-03-07 00:17:32
|
Now that I have your attention via that confusing subject line, here is what I'm trying to do. I read through the manual again...if what I'm describing is in there, it didn't jump out at me. In the MS SQL table dumps I'm parsing, there are lines such as these: exec sp_addextendedproperty N'MS_Description', N'The user and password table', N'user', N'dbo', N'table', N'auth_login' GO exec sp_addextendedproperty N'MS_Description', N'User name', N'user', N'dbo', N'table', N'auth_login', N'column', N'login' GO I guess you could say they are kind of a really messed up implementation of Python's keyword arguments. After the procedure call, you have lines of the format: N'Key' N'Value'... So, I have a Pyparsing expression that parses these nicely: ext_prop = (p.Combine(p.Keyword('exec') + p.Keyword('sp_addextendedproperty'), joinString=" ", adjacent=False) + p.delimitedList(p.Keyword('N').suppress() + p.QuotedString(quoteChar="'")) ) Combine that with: table_dump = (create_table.setResultsName('table_def') + p.Keyword('GO').suppress() + p.ZeroOrMore(ext_prop.setResultsName('ext_prop', listAllMatches=True) + p.Keyword('GO').suppress()) ) and create_data = table_dump.parseString(sql) for ep in create_data.ext_prop: print ep Gives me: ['exec sp_addextendedproperty', 'MS_Description', 'The user and password table', 'user', 'dbo', 'table', 'auth_login'] ['exec sp_addextendedproperty', 'MS_Description', 'User name', 'user', 'dbo', 'table', 'auth_login', 'column', 'login'] Nice. Now, I'd like to extend that in this way: Starting with the first "key" (in this case MS_Description) I'd like the follow value to have the name of that key, as if I had called setResultsName for it. Said another (hopefully clearer) way: When I do that above "for ep..." loop, I'd like to be able to: print ep.MS_Description #and get 'User name' print ep.table #and get auth_login print ep.column #and get login I'd check for ep.column first via hasattr, since it might not be there if it's parsing a call to set exteneded properties for a table. Is this at all doable without some major pyparsing hackery? Granted, I can just access by position in list, but I like the attribute accessors, as they look cleaner and are somewhat self documenting. Of course, I can always spell out the full procedure call, and manually name each value, but the less (and simpler) the code the better. j -- Joshua Kugler VOC/SigNet Provider (aka Web App Programmer) S&K Aerospace Alaska |