Source Code for Me (s-c.me)

Allows you to paste souce code to blogs! Adapted for Twitter! Here is Search Form in case you missed your code.
Tags: Python,remove_spaces_from_end,remove_fake_tabs,remove_tabs_from_end,replace_tabs_with_spaces,replace_spaces_with_tabs,pps, Created At: 10/24/2016 8:22:06 PMViews:

HTML view:
Copy Source | Copy HTML
#!/usr/bin/env python
 
import re
import sys
 
def remove_spaces_from_end(string):
    ret = re.sub(" *$", "", string)
    return ret
 
def remove_fake_tabs(string, tabWidth):
    ret = re.sub("<-->", "\t", string)
    return ret
 
def remove_tabs_from_end(string):
    ret = re.sub("\t*$", "", string)
    return ret
 
def replace_tabs_with_spaces(string, tabWidth):
    ret = re.sub("\t", " "*tabWidth, string)
    return ret
def replace_spaces_with_tabs(string, tabWidth):
    ret = re.sub(" "*tabWidth, "\t", string)
    return ret
 
def pps():
    tabWidth = 4
    replace="no"
    to_tabs="no"
    sys.argv[:1] = []
    #print (sys.argv)
 
    if "-replace" in sys.argv:
        replace="yes"
        sys.argv.remove("-replace")
    if "-totabs" in sys.argv:
        to_tabs="yes"
        sys.argv.remove("-totabs")
    for srcfile in sys.argv:
        print(srcfile)
        src = open(srcfile, "r")
        buffer = ""
        for line in src:
            buffer += line
            buffer = remove_spaces_from_end(buffer)
            if to_tabs=="yes":
                buffer = replace_spaces_with_tabs(buffer, tabWidth)
            else:
                buffer = replace_tabs_with_spaces(buffer, tabWidth)
            buffer = remove_fake_tabs(buffer, tabWidth)
            buffer = remove_tabs_from_end(buffer)
        src.close()
        if replace == "no":
            print (buffer)
        else:
            dst = open(srcfile, "w")
            dst.write(buffer)
            dst.close()
 
if __name__ == "__main__":
    pps()
 

Based on Manoli.Net's CodeFormatter. Made by Topbot (c) 2008-2017