Textdateien wie Datenkbanken benützen

Stellt hier eure Projekte vor.
Internetseiten, Skripte, und alles andere bzgl. Python.
Antworten
RebelliousByteZ

Donnerstag 22. Dezember 2005, 11:34

Hi Leutz, hackerz & coderz.

Bin seit ein paar monaten am pythonen, davor delphi, pure basic usw.

Mein erstes ernstzunehmendes Python projekt soll meine TODO listen (normale ascii dateien mit notepad) "synchronisieren".
Also nicht einfach kopieren sobald sich was ändert, sondern wirklich vergleichen: "Ist die zeile von text1 in text2? wenn nicht ergänze!" und umgekehrt, weil wenn die pcs mal nicht vernetzt sind und ich an verschiedenen orten zur gleichen zeit meine todo listen abändere, einträge verloren gehen. (!?)

Falls es keine C:\dbase1.txt und C:\dbase2.txt
gibt. Werden diese erstellt. (programm mehrmals ausführen!)

Das Problem mit "doppelte einträge löschen" hab ich mit code von jemand anders gelöst.

DAS PROGRAMM IST ELLEN LANG, IM NEWBIESTYLE GESCHRIEBEN UND FUNKTIONIERT JETZT!!! WAHOO! in der Versionsnummer_v20 und bin ein bisschen stolz drauf.

Verbesserungsvorschläge willkommen!
Wie hätte der Code-WIzzard das problem mit 5-Zeilen gelöst!??? =)

Code: Alles auswählen

# => DAVE'S ULTRA KORREKT 21.12.2005 19:21 <= "use text file like dbase1"-program
# ok it can "sync" text file databases, means: it compares the lines of two text files and adds missing lines where needed.
# ok it checks if a text files contains same entry twice.
# reads stuff from file and compare it to stuff in a other file
# if there are not matching lines, append the not-existing
# in other words: it syncs the content of two textfiles 
# good for: when you want to have up-todate todolists on different machines and every change is automatically updated on the other machine
# problem: 1.append only stuff to the file that is not allready in it! Don't append to list! Append to the file!
# problem: 2.if there is no line break at the end of the file, the dbas1.write alters the last entry of the list and the list gets completely messed up. For some reason listname.append(string) appends a line break \n at every entry but not at the last. !?

def opendbase(path,filename):
	"""opendbase(path,filename)\n This function opens text file(s) given as argument opendbase(path,filename)\n If the file does not exist, it will be created. \n In both cases it will print the content of the file. \n output of the function is the path, the filename and the text-content of the given ascii file. The return-output is a dictionary-variable with 3 values: string:path, string:filename, list:content. You can access it via: output[path] output[filename] or output[content]."""
	try:
		dbase1 = open(path+filename,"r") #tries to open the file, but does not create it
		print dbase1 #prints type of loaded file
	except: #could not open the file, so create it!
		print "Could not find file or directory =("
		print "creating file"
		dbase1 = open(path+filename,"w") #create it now!
		dbase1.close()
	else:
		print "File %s%s loaded successfully =)" % (path,filename)
		print "list of lines:"
		dbase1 = open(path+filename,"r")
		lineList = dbase1.readlines() # read lines from dbase1.txt into temporary list-variable (RAM)
		dbase1.close()
		for line in lineList:
	   		print line,
	   	print #creates 1 line break, while print("\n") creates 2 line breaks

	   	if "\n" not in lineList[-1]:
	   		lineList[-1] = lineList[-1]+"\n" 		# problem: 2.if there is no line break at the end of the file, the dbase1.write alters the last entry of the list and the list gets completely messed up. For some reason listname.append(string) appends a line break \n at every entry but not at the last. !?
		   	# problem: some numbers get multiple line breaks some not => are not same, get added! => double entries
			print "...ADDED LINE BREAK AT END OF LIST"
		else:
			print "...LINE BREAK AT END OF LIST"

	   	output = {"path":path,"filename":filename,"content":lineList}	   	

	   	return output #output of the function is the path, the filename and the text-content of the given ascii file. The return-output is a dictionary-variable with 3 values: path, filename, content


def savedbase(path,filename,content):
	"""savedbase(path,filename,content)\n takes in 3 parameters 1:string, 2:string, 3:list. What it does it opens/creates the file given with path+filename and adds/writes/appends the lines of the list in the file. mfg"""
	try:
		dbase1 = open(path+filename,"w") #opens file for appending text
		for line in content:
			dbase1.write(line),
	except:
		print "Could not save altered dbase %s%s"	%	(path,filename)
	else:
		print "Database %s%s saved succesfuly =)"	%	(path,filename)
	dbase1.close()


def unique(s):
    """Return a list of the elements in s, but without duplicates.

    For example, unique([1,2,3,1,2,3]) is some permutation of [1,2,3],
    unique("abcabc") some permutation of ["a", "b", "c"], and
    unique(([1, 2], [2, 3], [1, 2])) some permutation of
    [[2, 3], [1, 2]].

    For best speed, all sequence elements should be hashable.  Then
    unique() will usually work in linear time.

    If not possible, the sequence elements should enjoy a total
    ordering, and if list(s).sort() doesn't raise TypeError it's
    assumed that they do enjoy a total ordering.  Then unique() will
    usually work in O(N*log2(N)) time.

    If that's not possible either, the sequence elements must support
    equality-testing.  Then unique() will usually work in quadratic
    time.
    """

    n = len(s)
    if n == 0:
        return []

    # Try using a dict first, as that's the fastest and will usually
    # work.  If it doesn't work, it will usually fail quickly, so it
    # usually doesn't cost much to *try* it.  It requires that all the
    # sequence elements be hashable, and support equality comparison.
    u = {}
    try:
        for x in s:
            u[x] = 1
    except TypeError:
        del u  # move on to the next method
    else:
        return u.keys()

    # We can't hash all the elements.  Second fastest is to sort,
    # which brings the equal elements together; then duplicates are
    # easy to weed out in a single pass.
    # NOTE:  Python's list.sort() was designed to be efficient in the
    # presence of many duplicate elements.  This isn't true of all
    # sort functions in all languages or libraries, so this approach
    # is more effective in Python than it may be elsewhere.
    try:
        t = list(s)
        t.sort()
    except TypeError:
        del t  # move on to the next method
    else:
        assert n > 0
        last = t[0]
        lasti = i = 1
        while i < n:
            if t[i] != last:
                t[lasti] = last = t[i]
                lasti += 1
            i += 1
        return t[:lasti]

    # Brute force is all that's left.
    u = []
    for x in s:
        if x not in u:
            u.append(x)
    return u


	

file1 = opendbase("C:/","dbase1.txt")
linelist1 = file1["content"]

file2 = opendbase("C:/","dbase2.txt")
linelist2 = file2["content"]





print "FROM: %s%s ---TEXTFILE--SYNC---> TO: %s%s" % (file1["path"],file1["filename"],file2["path"],file2["filename"])
print

for line in linelist1: #goes through every element in linelist1 and tries to find a matching entry in linelist2
	if not line in linelist2:
		print "Entry %s is not in the list %s, appending entry." % (line,file2["filename"])
		linelist2.append(line)

	else:
		print "Entry %s \n is allready in the list %s." % (line,file2["filename"])
	
print "Delete duplicates in the linelist2"
linelist2 = unique(linelist2)
print "content of linelist2:" 
print linelist2
print


savedbase("C:/","dbase2.txt",linelist2) #openening text files to save altered lists in them





print "FROM: %s%s ---TEXTFILE--SYNC---> TO: %s%s" % (file2["path"],file2["filename"],file1["path"],file1["filename"])

for line in linelist2: #goes through every element in linelist2 and tries to find a matching entry in linelist1
	if not line in linelist1:
		print "Entry %s is not in the list %s, appending entry." % (line,file1["filename"])
		linelist1.append(line)

	else:
		print "Entry %s is allready in the list %s." % (line,file1["filename"])
	
print "Delete duplicates in the linelist2"
linelist1 = unique(linelist1)
print "content of linelist1:" 
print linelist1

savedbase("C:/","dbase1.txt",linelist1) #openening text files to save altered lists in them

Benutzeravatar
jens
Moderator
Beiträge: 8482
Registriert: Dienstag 10. August 2004, 09:40
Wohnort: duisburg
Kontaktdaten:

Donnerstag 22. Dezember 2005, 11:54

Ich hab mir jetzt dein Programm nicht näher angeschaut... Allerdings gibt es da gleich ein paar Sachen die dich interessieren dürften:

difflib: http://www.python-forum.de/viewtopic.php?p=27970#27970

und evtl. auch der ConfigParser:

http://www.python-forum.de/viewtopic.php?t=4676
http://www.python-forum.de/viewtopic.php?t=4553


EDIT: Ich hab das Thema mal nach "Showcase" verschoben.

CMS in Python: http://www.pylucid.org
GitHub | Open HUB | Xing | Linked in
Bitcoins to: 1JEgSQepxGjdprNedC9tXQWLpS424AL8cd
Antworten