[Pyparsing] Grouping when using asList()
Brought to you by:
ptmcg
From: Joshua J. K. <jk...@sk...> - 2008-03-05 21:30:34
|
Warning: long First: thank for the great package! It greatly simplifies my life. :) I'm working on parsing some SQL create statements from MS SQL (sigh, I know, but it's what we're stuck with at the moment). Here is the create: CREATE TABLE [dbo].[auth_login] ( [login_id] [int] IDENTITY (1, 1) NOT NULL , [login] [varchar] (255) NULL , [password_hash] [varchar] (255) NULL , [login_name] [varchar] (255) NULL , PRIMARY KEY CLUSTERED ( [login_id] ) ON [PRIMARY] , CONSTRAINT [IX_auth_login] UNIQUE NONCLUSTERED ( [login] ) ON [PRIMARY] ) ON [PRIMARY] You'll notice that the column definitions as well as the primary key and constraints are all within one enclosing pair of parentheses. My code is as such: create_table = (p.CaselessKeyword('create').suppress() + p.CaselessKeyword('table').suppress() + bracket_quoted.setResultsName('schema') + "." + bracket_quoted.setResultsName('table_name') + p.nestedExpr(content=p.delimitedList(p.Or( [p.Group(column_def.setResultsName('columns')), p.Group(primary_key), p.Group(constraint)]))) + p.CaselessKeyword('on').suppress() + bracket_quoted.suppress() ) p denotes the pyparsing module (import pyparsing as p) bracket_quoted is QuotedString using '[' and ']' column_def, primary_key, and constraint are all pyparsing expressions. So, at any rate, running that code and outputting via asList() gives me: ['dbo', '.', 'auth_login', [['login_id', 'int', 'IDENTITY', '1, 1', 'not', 'null'], ['login', 'varchar', '255', 'null'], ['password_hash', 'varchar', '255', 'null'], ['login_name', 'varchar', '255', 'null'], ['paid_us_good', 'money', 'null'], ['asdfsdafsadf', 'nchar', '10', 'null'], ['primary', 'key', 'clustered', ['login_id']], ['constraint', 'IX_auth_login', 'unique', 'nonclustered', ['login']]]] The column defs are in the same list element as the primary key and constraint. Apparently I'm not understanding the Group class. What can I do to put each of those three things (column defs, primary key defs, and constraint defs) in their own list elements? I want to know that the elements of l[3] are columns, l[4] are primary keys, or similar. Thanks! j -- Joshua Kugler VOC/SigNet Provider (aka Web App Programmer) S&K Aerospace Alaska |