remove strings from source

Mark McEahern marklists at mceahern.com
Sat Feb 26 10:55:38 EST 2005


qwweeeit wrote:

>For a python code I am writing I need to remove all strings
>definitions from source and substitute them with a place-holder.
>
>To make clearer:
>line 45  sVar="this is the string assigned to sVar"
>must be converted in:
>line 45 sVar=s00001
>
>Such substitution is recorded in a file under:
>s0001[line 45]="this is the string assigned to sVar"
>
>For curious guys:
>I am trying to implement a cross variable reference tool and the
>variability (in lenght) of the string definitions (expecially if
>multi-line) can cause display problems.
>
>I need your help in correctly identifying the strings (also embedding
>the r'xx..' or u'yy...' as part of the string definition). The problem
>is mainly on the multi-line definitions or in cached strings
>(embedding chr() definitions or escape sequences).
>  
>
Approach this in a test-driven development way.  Create sample input and 
output files.  Write a unit test something like this (below) and run 
it.  You'll either solve the problem yourself or ask more specific 
questions.  ;-)

Cheers,

// m

#!/usr/bin/env python
import unittest

def substitute(data):
    # As a first pass, just return the data itself--obviously, this 
should fail.
    return data

class Test(unittest.TestCase):
    def test(self):
        data = open("input.txt").read()
        expected = open("expected.txt").read()
        actual = substitute(data)
        self.assertEquals(expected, actual)

if __name__ == '__main__':
    unittest.main()





More information about the Python-list mailing list