Seite 1 von 1

whitespace2tab

Verfasst: Dienstag 5. September 2006, 18:30
von name
Ein Script um fuer die indention verwendete whitespaces in tabs umzuwandeln

Code: Alles auswählen

#! /usr/bin/env python
# Licensed under the CreativeCommons Attribution-NonCommercial-ShareAlike (http://creativecommons.org/licenses/by-nc-sa/2.5/) by Florian Mayer(aka name)
# THIS COMES WITH ABSOLUTELY NO WARRANTY
import sys
def Replace(file,tabsize):
	source = open ( file )
	source = source.readlines()
	i = 0
	while(i < len(source)):
		tabs = getamount(source[i]) / tabsize
		whitespaces = getamount(source[i])
		if(source[i][0:tabs*tabsize] == " "*tabs*tabsize and not tabs == 0 and not whitespaces ==0 ):
			source[i] = "\t"*tabs + source[i][tabs*tabsize:]
		i+=1
	towrite = "".join(source)
	sourcefile = open ( file, 'w' ) 
	sourcefile.write(towrite)
	sourcefile.close()
	return True
def Parsefile(file):
	source = open ( file )
	source = source.readlines()
	i = 0
	while(i < len(source)):
		if("if" in source[i] or "while" in source[i] or "else" in source[i] or "class" in source[i] or "def" in source[i]):
			tabsize=getamount(source[i+1])-getamount(source[i])
			return tabsize
		i+=1
def getamount(string):
	i = 0
	count = 0
	while(i < len(string)):
		if(string[i] == " "):
			count+=1
		else:
			return count
		i+=1
Replace(sys.argv[1],Parsefile(sys.argv[1]))
Was sagt ihr dazu?
Verbesserungsvorschlaege? Kritik?
Geht aber nur bei "class" "if" "else" "while" oder "def"s im code, sollten aber in jedem sein :)

Re: whitespace2tab

Verfasst: Dienstag 5. September 2006, 19:41
von Leonidas
name hat geschrieben:Was sagt ihr dazu?
Verbesserungsvorschlaege? Kritik?
Ohne mir das Skript genauer angeschaut zu haben: du hast ein Programm geschrieben, das Programme so umschreibt, dass sie nicht mehr dem Styleguide entsprechen?
Python Styleguide hat geschrieben:For new projects, spaces-only are strongly recommended over tabs.
Ansonsten.. interessante Idee für ein Anfängerprojekt. Hast du Lust im Wiki auf [wiki]Projektideen[/wiki] etwas dazu zu schreiben?

Edit: P.S.: Hallo name, willkommen im Forum!

Verfasst: Dienstag 5. September 2006, 20:06
von name
Naja mir sind Tabs angenehmer, deswegen hab ich das geschrieben :)
Edit: P.S.: Hallo name, willkommen im Forum!
Danke :)

Re: whitespace2tab

Verfasst: Mittwoch 6. September 2006, 14:36
von birkenfeld

Code: Alles auswählen

def Parsefile(file):
	source = open ( file )
	source = source.readlines()
	i = 0
	while(i < len(source)):
		if("if" in source[i] or "while" in source[i] or "else" in source[i] or "class" in source[i] or "def" in source[i]):
...
Was sagt ihr dazu?
Das geht ganz schön in die Hose. "if" in source trifft z.B. auch auf "ifile = file(...)" zu.

Leider ist die Aufgabe nicht ganz einfach, und selbst Tim Peters hat 293 Zeilen gebraucht, um ein entsprechendes Skript zu schreiben (das allerdings in die andere, richtige Richtung konvertiert).

Verfasst: Donnerstag 7. September 2006, 09:16
von mitsuhiko
Kommen eigentlich Kommentare im AST vor? Wenn ja könnte man ast dazu verwenden mal ein neues reindent skript zu schreiben :)

Verfasst: Freitag 8. September 2006, 19:47
von name
Da sollte das doch schon besser gehen, oder?

Code: Alles auswählen

if("if" in source[i] or "while" in source[i] or "else" in source[i] or "class" in source[i] or "def" in source[i] and  not getamount(source[i]) == getamount(source[i+1])):