Home   Archive   Permalink



Parsing a Python line; why does this even work?

I am trying to transition to Python for the sake of our shared expertise in that language, and I am trying to centralize our ODBC database connection strings for easy maintenance. I discovered a format for coding the connection strings into a Python module so they can be parsed and used by a REBOL script. One format to rule them all, as it were. But, the question is, it works, but why? As shown in the demo below, I can divide the file into sets of
    
[
(connection-name-1) (connection-string-1)
(connection-name-2) (connection-string-2)
...
]
    
based on that "equal" sign that divides the connection name and the connection string, but why doesn't it divide on all the "equal" signs inside the connection strings.
    
I am delighted that this works as I hoped, but I would like to be able to explain why.
    
Thank you.
    
R E B O L [
     Title: "General ODBC functions"
     Purpose: {Isolate ODBC connection strings for easy maintenance.
     Store them in a format such that they can be a Python module
     and still be used by a REBOL program.}
]
    
;; -- This is a demo. These will be stored in a file.
ODBC-CONNECTIONS:
{DB1_DBCONNECT="DRIVER={SQL Server};SERVER={SERVER1};DATABASE=DB1;UID=user1;PWD=password1"
DB2_DBCONNECT="DRIVER={SQL Server};SERVER={SERVER2};DATABASE=DB2;UID=user2;PWD=password2"
DB3_DBCONNECT="DRIVER={SQL Server};SERVER={SERVER3};DATABASE=DB3;UID=user3;PWD=password3"
DB4_DBCONNECT="DRIVER={SQL Server};SERVER={SERVER4};DATABASE=DB4;UID=user4;PWD=password4"
DB5_DBCONNECT="DRIVER={SQL Server};SERVER={SERVER5};DATABASE=DB5;UID=user5;PWD=password5"}
    
ODBC-CONNECTIONLIST: parse/all ODBC-CONNECTIONS "=^/"
    
ODBC-OPEN: func [
     ODBC-CONNECTIONNAME
     /local ODBC-CONNECTSTRING
] [
     ODBC-CONNECTIONSTRING: select ODBC-CONNECTIONLIST ODBC-CONNECTIONNAME
     ODBC-CON: open [
         scheme: 'odbc
         target: ODBC-CONNECTIONSTRING
     ]
     ODBC-CMD: first ODBC-CON
]
    
ODBC-EXECUTE: func [
     ODBC-SQL
] [
     insert ODBC-CMD ODBC-SQL
     return copy ODBC-CMD
]
    
ODBC-CLOSE: does [
     close ODBC-CMD
]
    
foreach [NAME CONSTRING] ODBC-CONNECTIONLIST [
     print [mold NAME ":" mold CONSTRING]
]
halt


posted by:   Steven White       3-Jun-2019/14:22:39-7:00



It looks its the quotes:
    
     >> parse/all {test="a=1,b=2"} "=,"
     == ["test" "a=1,b=2"]
    
     >> parse/all {test="a=1,b=2",x=3,"y=4"} "=,"
     == ["test" "a=1,b=2" "x" "3" "y=4"]
    
     >> parse/all {test='a=1,b=2'} "=,"
     == ["test" "'a" "1" "b" "2'"]
    
But I don't know why it works that way.


posted by:   Endo       9-Jun-2019/13:58:12-7:00



Parse in Rebol 2 (and Rebol 3 Alpha) in 'Split' mode has one or two quirks, one of which is to skip content within quotes (if a quote character immediately follows a delimiter).
    
I suspect the reason for this is rooted in handling a certain CSV pattern more common when the interpreter was first written, but could also just be a bug.
    
Red and Ren-C both deprecated 'Split' mode in favour of a separate SPLIT function.

posted by:   Chris       13-Jun-2019/16:45:08-7:00



Name:


Message:


Type the reverse of this captcha text: "~ r o"



Home