Code: Alles auswählen
#----------- Beispielliste -------------------------------
liste="('[S] Vorname', '[S] Nachname', '[I] Alter', '[S] Email', '[S] Abteilung', '[F] Gehalt', '[S] Projekt', '[F] Projektstatus', '[S] Letzter Statusbericht', '[S] Kontonummer', '[S] Adresse', '[S] Postleitzahl', '[S] Betriebssystem', '[I] Kinder', '[S] Angestellt seit', '[S] Kommentar')"
#
# Diese Liste wäre ein Tuble aus einer Listbox in Tkinter
#---------------------------------------------------------
#
# Handhabung / Hilfe :
# 1. Wenn der String unbestimmt viele Entrys (also ' '... ;) )
# so ermittelt das Programm automatisch mit dem Befehl
#
# encode(liste,0)
#
# einen Entrywert :)
# ['liste' ist ein Listenname der als Tuple ausgelesen wurde und die...]
# [... 0 weißt darauf hin das die zum Debugn nur die Anzahl der Eintraege...]
# [... geliefert werden soll]
#
# Kennt man die Anzahl kann man mit : encode(liste, 16) [hier 16]
# den Inhalt "encoden" lassen (also aus den Tupels in eine leicht zu
# bearbeitbaren einheit bringen.
#
# Rueckcodieren geht dann mit : decode(liste,16)
#
# Zu beachten ist : Die Liste enthält nach dem [x] zwei Leerstellen und jeder
# Eintrag endet mit einem | [alt + <]
#
# S = String I = Integer F = Float
#
# Fuer einen Datenbank eben ;)
#
#------------------------------------------------------------------------------
# Encoding und Decoding Funktion + Helper
#------------------------------------------------------------------------------
def encode(list,entrys):
global entrystr, dbdecode, dbencode
tmplist=list
entrysw= int(entrys)
entrystr=''
f = 0
x = 0
c = 'B'
if int(entrys) != 0:
c = 'A'
entrys = 9999999
entrysw= 9999999
while not int(f) == 1 or int(x) == int(entrysw) +1:
try:
if not int(f) == 1:
p1=pstr("]",tmplist,0)+3
p2=pstr("'",tmplist,2)
entryc=tmplist[p1-4]
entrya=tmplist[p1:p2]+entryc+'|'
entrystr=entrystr+entrya
entryl=p1+p2
tmplist=tmplist[entryl-2:]
x = x + 1
except:
f = 1
if str(c) != 'A':
print
print 'To assist you to determine the amount of entry tables the program will count them... counted '+str(x)
print 'Note : the counter function will be removed later to improve performance...'
print
if str(c) != 'B':
dbencode = str(entrystr)
return dbencode
def decode(list,entrys):
global entrystr, dbdecode, dbencode
tmplist= str(list)
entrystr=[]
x = 0
f = 0
while not int(x) == int(entrys) or not int(f) == 1:
try:
p1=pstr("|",tmplist,0)
tmpstr=tmplist[:p1]
entryc=tmpstr[p1-1]
entryn=tmpstr[0:p1-1]
entrync='['+entryc+'] '+entryn+'|'
entrystr.append(str(entrync))
tmplist=tmplist[p1+1:]
c = 'B'
x = x + 1
except:
f= 1
if str(c) =='B':
dbdecode = str(entrystr)
return dbdecode
def pstr(fc,pline,plc):
pos=pline.find(fc,plc)
return pos
#------------------------------------
# Demo
#------------------------------------
print
print 'Vorher :'
print str(liste)
print
print
print 'Dies wurde aus dem Tuple Format geholt :'
print encode(liste,16)
print
print
print 'Und hier die Tuple konvertierung :'
print decode(dbencode,16)
print
*EDIT* Ich habe den alten Code mal entfernt, da hat sich wirklich der Copy und Paste Fehlerteufel eingeschlichen gehabt...