import re column_attributes = ( 'DEFAULT|' 'IDENTITY\s*?(\(.*?\))?|' 'ENCODE|' 'DISTKEY|' 'SORTKEY' ) column_constraints = ( 'NOT\s+NULL|NULL|' 'UNIQUE|PRIMARY\s+KEY|' 'REFERENCES' ) column_encodings = ( 'BYTEDICT|DELTA|DELTA32K|LZO|MOSTLY8|MOSTLY16|MOSTLY32|RAW|RUNLENGTH|' 'TEXT255|TEXT32K' ) replace_identity = re.compile( '(IDENTITY|IDENTITY\s*\(.*?\))\s*(?=(,|\)|(({}|{})+' '(?=(\s|,|\))+))))'.format( column_attributes, column_constraints), flags=re.I) test_cases = [{ 'actual': ( 'CREATE TABLE foo(' ' col1 int,' ' col2 int IDENTITY(0,1)' ') '), 'expected': ('CREATE TABLE foo(' ' col1 int,' ' col2 int SERIAL ' ') ') },{ 'actual': ( 'CREATE TABLE foo(' ' col1 int,' ' col2 int\n IDENTITY(0,1) PRIMARY KEY' ') '), 'expected': ('CREATE TABLE foo(' ' col1 int,' ' col2 int\n SERIAL PRIMARY KEY' ') ') }, { 'actual': ( 'CREATE TABLE staging.lnrt_student_parameters (\n' 'user_id CHAR(36),\n' 'unit_id CHAR(36),\n' 'subgraph_id CHAR(36),\n' 'log_time TIMESTAMP,\n' 'speed_r FLOAT8,\n' 'speed_q FLOAT8,\n' 'quit_duration FLOAT8,\n' 'tag_rq_speed VARCHAR(8000),\n' 'id INT IDENTITY(1,1)\n' ')\n' 'DISTKEY(unit_id)\n' 'SORTKEY(log_time);' ), 'expected': ( 'CREATE TABLE staging.lnrt_student_parameters (\n' 'user_id CHAR(36),\n' 'unit_id CHAR(36),\n' 'subgraph_id CHAR(36),\n' 'log_time TIMESTAMP,\n' 'speed_r FLOAT8,\n' 'speed_q FLOAT8,\n' 'quit_duration FLOAT8,\n' 'tag_rq_speed VARCHAR(8000),\n' 'id INT SERIAL ' ')\n' 'DISTKEY(unit_id)\n' 'SORTKEY(log_time);' ) } ] for test_case in test_cases: print "*Substituted:\n", replace_identity.sub('SERIAL ', test_case['actual']) print "**Expected:\n", test_case['expected'] assert replace_identity.sub('SERIAL ', test_case['actual']) == test_case['expected'] print "\n"
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question