Parse Python code downloaded from Daniweb code blocks

Namibnat 1 Tallied Votes 720 Views Share

Note two things: First off, I don't guarantee that it works well. Writing to files, if done wrong can always break things. Be careful. I mostly participate in this forum (the Python part) for fun, and my code is written like that.
Second: this script is about copying things. I mean it to be of use where you can work with code you find to help or be helped. Copying stuff always means copyright issues can come into play.

Okay...I am sure that there must be something like this around already, but I couldn't find it and it was quicker (and kind of fun) to write than to read/search endless sticky notes/code snippets and forum-clever-stuff.

Just in case someone else is lazy like me, I thought I would stick this little script up here.

I would have thought that something like this would have been on a page like, but I didn't find it. If it is there, I am sorry, I'm on holiday :)

The problem:
If you are trying to help someone, or getting help from someone and you copy the code posted in the thread and then paste it in your text editor, it puts the line numbers on one line and the code unchanged, with the right indentation on the next line.

The solution:
A really simple script that opens your file, reads it, deletes each line with the line numbers and finally writes over your original file.

How to use it. If you are new to Python and the whole thing doesn't make sense to you right away, just copy a file to a location you want. Then in the test = ClPyScps("/file/path", "") replace "file/path" with the, um, file path, and the with the file name, including the .py ending. You can, of course make this chopping automatic, but I was to lazy to figure that out when I wrote it. If you don't want to have the typical unix lines at the top, you can even get rid of the filepath and just give it all together (just be sure to change the __init__ method accordingly.)

If you want to watch the action in your command line - i.e. see what is being printed, add another parameter to the class initialization like this test = ClPyScps("/path/to/test", "", 1) That last little 1 will make it print what it is doing.

Then run if from a linux terminal:

vernon@mybox:~/Python/trying_stuff$ python

It can be improved by being able to pass the file path/file name with the command line. I'll do that later. It can be used with other code, not just Python, but needs to be tweeked accordingly.

Once again, if I am re-inventing the wheel...well...sorry. I just put it up here because I wrote it, found it useful, and thought someone else might also find it useful.

#! /usr/bin/python


class ClPyScps:
    """Just a little script for cleaning code from Daniweb Python Forum"""
    def __init__(self, filepath, filename, pathtopython="/usr/bin/python", verbose=0):
        self.fl = filepath + "/" + filename
        self.cntr = 0
        self.clnList = []
        self.strlines = "#! %s\n\n# %s" % (pathtopython, filename)
        self.vrb = verbose
    def cleanIt(self):
        openfl = open(self.fl, 'r')
        chopfl = openfl.readlines()

        for line in chopfl:
            if self.cntr == 0:
                self.cntr = 1
                self.cntr = 0
        openwfl = open(self.fl, 'w')
        for lins in self.clnList:
            if self.vfb: print "This line written --> %s" % lins
        print "Done"

if __name__ == "__main__":
    test = ClPyScps("/path/to/test", "")
Namibnat 10 Junior Poster in Training

Just one thing, it is not intuitive about the spacing - important for Python. You still need to sort that out manually. I am not really sure why, and I will try to fix that later.

Gribouillis 1,391 Programming Explorer Team Colleague

A simpler solution is to hit the button Toggle Plain Text before copying the code.

Namibnat 10 Junior Poster in Training


Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.