In analysing a very big application (pysol) made of almost
100 sources, I had the need to remove comments.
Removing the comments which take all the line is straightforward ...
Instead for the embedded comments I used the tokenize module.
To my surprise the analysed output is different from the input
(the last tuple element should exactly replicate the input line)
The error comes out in correspondance of a triple string.
I don't know if this has already been corrected (I use Python 2.3)
or perhaps is a mistake on my part...
Next you find the script I use to replicate the strange behaviour:
import tokenize
Input = "pippo1"
Output = "pippo2"
f = open(Input)
fOut=open(Outpu t,"w")
nLastLine=0
for i in tokenize.genera te_tokens(f.rea dline):
.. if nLastLine != (i[2])[0]: # the 3rd element of the tuple is
.. . nLastLine = (i[2])[0] # (startingRow, startingCol)
.. . fOut.write(i[4])
f.close()
fOut.close()
The file to be used (pippo1) contains an extract:
class SelectDialogTre eData:
.. img = None
.. def __init__(self):
.. . self.tree_xview = (0.0, 1.0)
.. . self.tree_yview = (0.0, 1.0)
.. . if self.img is None:
.. . . SelectDialogTre eData.img = (makeImage(dith er=0, data="""
R0lGODlhEAAOAPI FAAAAAICAgMDAwP//AP///4AAAAAAAAAAACH5 BAEAAAUALAAAAAA QAA4AAAOL
WLrcGxA6FoYYYoR ZwhCDMAhDFCkBoa 6sGgBFQAzCIAzCI AzCEACFAEEwEAwE A8FAMBAEAIUAYSA Y
CAaCgWAgGAQAhQB BMBAMBAPBQDAQBA CFAGEgGAgGgoFgI BgEAAUBBAIDAgMC AwIDAgMCAQAFAQQ D
AgMCAwIDAgMCAwE ABSaiogAKAKeoqa kFCQA7"""), makeImage(dithe r=0, data="""
R0lGODlhEAAOAPI FAAAAAICAgMDAwP//AP///4AAAAAAAAAAACH5 BAEAAAUALAAAAAA QAA4AAAN3
WLrcHBA6Foi1YZZ AxBCDQESREhCDMA iDcFkBUASEMAiDM AiDMAgBAGlIGgQA gZeSEAAIAoAAQTA Q
DAQDwUAwAEAAhQB BMBAMBAPBQBAABA CFAGEgGAgGgoFgI AAEAAoBBAMCAwID AgMCAwEAAApERI4 L
jpWWlgkAOw==""" ), makeImage(dithe r=0, data="""
R0lGODdhEAAOAPI AAAAAAAAAgICAgM DAwP///wAAAAAAAAAAACwA AAAAEAAOAAADTii 63DowyiiA
GCHrnQUQAxcQAAE QgAAIg+MCwkDMdD 0LgDDUQG8LAMGg1 gPYBADBgFbs1QQA wYDWBNQEAMHABrA R
BADBwOsVAFzoqlq dAAA7"""), makeImage(dithe r=0, data="""
R0lGODdhEAAOAPI AAAAAAAAAgICAgM DAwP8AAP///wAAAAAAACwAAAAA EAAOAAADVCi63Do wyiiA
GCHrnQUQAxcUQAE UgAAIg+MCwlDMdD 0LgDDQBE3UAoBgU CMUCDYBQDCwEWwF AUAwqBEKBJsAIBj Q
CDRCTQAQDKBQAcD FBrjf8Lg7AQA7"" "))
The output of tokenize (pippo2) gives instead:
class SelectDialogTre eData:
.. img = None
.. def __init__(self):
.. . self.tree_xview = (0.0, 1.0)
.. . self.tree_yview = (0.0, 1.0)
.. . if self.img is None:
.. . . SelectDialogTre eData.img = (makeImage(dith er=0, data="""
AgMCAwIDAgMCAwE ABSaiogAKAKeoqa kFCQA7"""), makeImage(dithe r=0, data="""
jpWWlgkAOw==""" ), makeImage(dithe r=0, data="""
BADBwOsVAFzoqlq dAAA7"""), makeImage(dithe r=0, data="""
CDRCTQAQDKBQAcD FBrjf8Lg7AQA7"" "))
.... with a big difference! Why?
100 sources, I had the need to remove comments.
Removing the comments which take all the line is straightforward ...
Instead for the embedded comments I used the tokenize module.
To my surprise the analysed output is different from the input
(the last tuple element should exactly replicate the input line)
The error comes out in correspondance of a triple string.
I don't know if this has already been corrected (I use Python 2.3)
or perhaps is a mistake on my part...
Next you find the script I use to replicate the strange behaviour:
import tokenize
Input = "pippo1"
Output = "pippo2"
f = open(Input)
fOut=open(Outpu t,"w")
nLastLine=0
for i in tokenize.genera te_tokens(f.rea dline):
.. if nLastLine != (i[2])[0]: # the 3rd element of the tuple is
.. . nLastLine = (i[2])[0] # (startingRow, startingCol)
.. . fOut.write(i[4])
f.close()
fOut.close()
The file to be used (pippo1) contains an extract:
class SelectDialogTre eData:
.. img = None
.. def __init__(self):
.. . self.tree_xview = (0.0, 1.0)
.. . self.tree_yview = (0.0, 1.0)
.. . if self.img is None:
.. . . SelectDialogTre eData.img = (makeImage(dith er=0, data="""
R0lGODlhEAAOAPI FAAAAAICAgMDAwP//AP///4AAAAAAAAAAACH5 BAEAAAUALAAAAAA QAA4AAAOL
WLrcGxA6FoYYYoR ZwhCDMAhDFCkBoa 6sGgBFQAzCIAzCI AzCEACFAEEwEAwE A8FAMBAEAIUAYSA Y
CAaCgWAgGAQAhQB BMBAMBAPBQDAQBA CFAGEgGAgGgoFgI BgEAAUBBAIDAgMC AwIDAgMCAQAFAQQ D
AgMCAwIDAgMCAwE ABSaiogAKAKeoqa kFCQA7"""), makeImage(dithe r=0, data="""
R0lGODlhEAAOAPI FAAAAAICAgMDAwP//AP///4AAAAAAAAAAACH5 BAEAAAUALAAAAAA QAA4AAAN3
WLrcHBA6Foi1YZZ AxBCDQESREhCDMA iDcFkBUASEMAiDM AiDMAgBAGlIGgQA gZeSEAAIAoAAQTA Q
DAQDwUAwAEAAhQB BMBAMBAPBQBAABA CFAGEgGAgGgoFgI AAEAAoBBAMCAwID AgMCAwEAAApERI4 L
jpWWlgkAOw==""" ), makeImage(dithe r=0, data="""
R0lGODdhEAAOAPI AAAAAAAAAgICAgM DAwP///wAAAAAAAAAAACwA AAAAEAAOAAADTii 63DowyiiA
GCHrnQUQAxcQAAE QgAAIg+MCwkDMdD 0LgDDUQG8LAMGg1 gPYBADBgFbs1QQA wYDWBNQEAMHABrA R
BADBwOsVAFzoqlq dAAA7"""), makeImage(dithe r=0, data="""
R0lGODdhEAAOAPI AAAAAAAAAgICAgM DAwP8AAP///wAAAAAAACwAAAAA EAAOAAADVCi63Do wyiiA
GCHrnQUQAxcUQAE UgAAIg+MCwlDMdD 0LgDDQBE3UAoBgU CMUCDYBQDCwEWwF AUAwqBEKBJsAIBj Q
CDRCTQAQDKBQAcD FBrjf8Lg7AQA7"" "))
The output of tokenize (pippo2) gives instead:
class SelectDialogTre eData:
.. img = None
.. def __init__(self):
.. . self.tree_xview = (0.0, 1.0)
.. . self.tree_yview = (0.0, 1.0)
.. . if self.img is None:
.. . . SelectDialogTre eData.img = (makeImage(dith er=0, data="""
AgMCAwIDAgMCAwE ABSaiogAKAKeoqa kFCQA7"""), makeImage(dithe r=0, data="""
jpWWlgkAOw==""" ), makeImage(dithe r=0, data="""
BADBwOsVAFzoqlq dAAA7"""), makeImage(dithe r=0, data="""
CDRCTQAQDKBQAcD FBrjf8Lg7AQA7"" "))
.... with a big difference! Why?
Comment