[Python] Scriptsprachen für welche Projekte
Übersicht Andere Programmiersprachen Allgemein
UNZBetreff: [Python] Scriptsprachen für welche Projekte |
Do, Jun 27, 2013 20:47 Antworten mit Zitat |
|
---|---|---|
Hi,
ich hatte vorhin den Wunsch meine Playlisten vom Rechner iwie auf meinen mp3-Player zu bringen. Folglich habe ich mal gegoogelt: Windows Media Player hier, externes Programm zum Synchronisieren dort, bla bla. Hat mir nicht gefallen und da ich sowieso lieber mit Ordnerstrukturen arbeite, wollte ich die Dateien mit einem einfachen Programm sammeln. Die Playlist-Dateien (m3u) sind zimlich einfach aufgebaut: Code: [AUSKLAPPEN] #EXTM3U
#EXTINF:257,Fade - Black Hearts And Dollar Signs E:\Dateien\Musik\Soundtracks\Mix\Black Hearts And Dollar Signs.mp3 #EXTINF:455,Clubbed To Death E:\Dateien\Musik\Soundtracks\Mix\Clubbed To Death.mp3 usw. Habe mir also kurzerhand ein Programm geschrieben, dass das macht. Aber dafür ein ganzes compiliertes Programm finde ich recht viel und da ich mal Python lernen will (unter Anderem) habe ich das gleiche Programm nochmal in Python geschrieben. (shell oder batch wäre vermutlich schneller gewesen, wenn man weiß wie, aber das tue ich nicht und ich möchte etwas flexibleres lernen) Soviel zum drumherum jetzt zu meiner Frage Wann haltet ihr es für sinnvoll etwas in einer Scriptsprache zu machen? Ab wann wird es unübersichtlich? Die Eigenschaft, dass man bei Variablen keine Angaben über ihren Datentyp (int,float,string,liste) macht ist für kleine Sachen zwar super, aber bei größeren Projekten fehleranfälliger (hab ich gehört). Trotzdem gibt es auch große gui module wie pyQT, mit denen sowas machbar ist. Ab welcher Projekt größe würdet ihr was benutzen und warum? Für wen es hilfreich ist: Hier mein Tool: Code: [AUSKLAPPEN] SuperStrict Framework brl.standardio Import brl.filesystem Import brl.linkedlist Global inputList:TList = New TList Global prefixPosition:Int=False 'should the files be prefixed with the position in the playlist? ReadAppArgs() Collect() '----------------------------------------------------- Rem bbdoc: collect all files of the playlists End Rem Function Collect() Local playlistDir$ For Local p:TPlaylist = EachIn inputList Print "Collecting:" + p.name p.Collect() Next End Function '----------------------------------------------------- Rem bbdoc: read the playlists and additional parameters from the application arguments End Rem Function ReadAppArgs() Local p:TPlaylist For Local i:Int= 1 To AppArgs.length-1 Local s$ = AppArgs[i] Select s Case "-p" prefixPosition = True Print "position prefix active" Case "-h","\?","--h","-help" ShowHelpText() End Default p= TPlaylist.Create(s) If p Then inputList.AddLast(p) End Select Next End Function '----------------------------------------------------- Rem bbdoc: prints some help info End Rem Function ShowHelpText() Print "Program to collect all files of an m3u playlist.~n".. + "Paths should be absolute in that playlist.~n~n".. + "Parameters:~n".. + "-p = prefix files with position in playlist" End Function '=================================================================== Type TPlaylist Field path$ Field dir$ Field name$ '----------------------------------------------------- Rem bbdoc: create a playlist End Rem Function Create:TPlaylist(path$) If Not isFilePath(path) Print "warning:'"+path+"' is not a valid file path!" Return null Endif Local o:TPlaylist = New TPlaylist o.path = path o.dir = ExtractDir(path) o.name= StripAll(path) Return o End Function '----------------------------------------------------- Rem bbdoc: collect all files of this playlist End Rem Method Collect() Local outDir$ = AppDir+"/"+dir + "/" + name Local outPath$ 'insert to dir? If isDirPath(outDir) Local answer$ Repeat answer = Input("Directory:'"+outDir+"' already exists. Insert there? (y/n)~n") If answer = "n" Then Return If answer <> "y" Then Print("Invalid answer.") Until answer = "y" Else CreateDir(outDir) EndIf 'open dir Local dir:Int = ReadDir(outDir) If Not dir Print "can't open directory!" Return Endif 'copy files to dir Local stream:TStream = ReadFile(path) Local cnt:Int= 1 Repeat Local line$ = ReadLine(stream) If line 'ignore comments If Not line.Replace(" ", "").Replace("~t", "").StartsWith("#") If isFilePath(line) outPath = outDir + "/" If prefixPosition outPath = outPath + PreZero(cnt, 2) + " - " + StripDir(line ) Else outPath = outPath + StripDir(line) EndIf Print "~tCreate File:" + outPath CopyFile(line, outPath) cnt :+ 1 EndIf Endif Endif Until stream.eof() End Method End Type '=================================================================== 'util '----------------------------------------------------- Rem bbdoc: checks if a path is a file End Rem Function isFilePath:Int(path$) Return FileType(path) = FILETYPE_FILE End Function '----------------------------------------------------- Rem bbdoc: checks if a path is a dir End Rem Function isDirPath:Int(path$) Return FileType(path) = FILETYPE_DIR End Function '----------------------------------------------------- Rem bbdoc: add zeroes to in front of an integer value End Rem Function PreZero$(value:Int, targetZeroes:Int) Local s$= String(value) For Local i:Int = s.length To targetZeroes-1 s= "0"+s Next Return s End Function Code: [AUSKLAPPEN] import sys import os from genericpath import isfile, isdir, exists from fileinput import filename from shutil import copyfile from os import makedirs from test.test_import import remove_files global input_list input_list= [] global prefixPosition prefixPosition= False #--------------------------------- def filename(path,strip_extension= False): dir,filename= os.path.split(path) if strip_extension: filename= filename.rsplit( ".", 1 )[0] return filename #--------------------------------- def add_playlist(path): global input_list if isfile(path): print("collecting "+filename(path,True)) input_list.append(path) else: print("invalid path:"+path) #---------------------------------- def read_args(): global input_list,prefixPosition cnt= 0 for s in sys.argv: cnt += 1 if cnt > 1: if s == "-p": print("position prefix active") prefixPosition= True elif s == "-h" or s == "--h" or s == "-help" or s == "\?" or s == "/?": show_help() sys.exit() else: add_playlist(s) def pre_zero(value,targetZeroes): s= str(value) for i in range(len(s),targetZeroes): s= "0"+s return s #---------------------------------- def collect(): global input_list,prefixPosition for s in input_list: f = open(s) if f: #construct out dir outdir = os.path.dirname(s)+"/"+filename(s,True) if isdir(outdir): while True: answer= input("Directory '"+outdir+"' already exists. Insert there? (y/n)\n") if answer == "n": return elif answer == "y": break; print("Invalid answer!") cnt= 0 for line in f: line = line.rstrip().lstrip() #no linefeed and carriage return #ignore comments if not line.replace(" ","").replace("\t","").startswith("#"): cnt += 1 #construct out path outpath = outdir+"/" if prefixPosition: outpath = outpath + pre_zero(cnt, 2) +" - " + filename(line) else: outpath = outpath + filename(line) print("\tCreate file:"+outpath) #create out dir if not exists(outdir): makedirs(outdir) #remove files if exist if isfile(outpath): remove_files(outpath) #copy copyfile(line, outpath) #---------------------------------- def show_help(): print("""Program to collect all files of an m3u playlist. Paths should be absolute in that playlist. Parameters: -p = prefix files with position in playlist""") #---------------------------------- read_args() collect() print("finished") Bye |
||
Das muss besser als perfekt! |
Übersicht Andere Programmiersprachen Allgemein
Powered by phpBB © 2001 - 2006, phpBB Group