Seite 1 von 1

Tupple Konverter für eine Datenbank

Verfasst: Sonntag 13. Juli 2003, 21:40
von Bugfix

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
Verbesserungsvorschläge sind immer gerne gesehen :)

*EDIT* Ich habe den alten Code mal entfernt, da hat sich wirklich der Copy und Paste Fehlerteufel eingeschlichen gehabt... ;)

Verfasst: Sonntag 13. Juli 2003, 22:40
von Dookie
Hi Bugfix,

wenn man da mal Python mit seinen mächtigen Strings ranlässt, bleibt von deinen Funktionen nicht mehr viel über ;)

Code: Alles auswählen

def encode(liste,n):
    global dbencode
    dbencode =  liste[1:-1].replace("'","").replace(", ","|")
    return dbencode

def decode(liste,n):
    global dbdecode
    dbdecode = liste.split("|")
    return dbdecode
wenn Du aber gleich das Tuple encoden willst, kannst Du das auch so machen

Code: Alles auswählen

def encode(liste):
    return "|".join(liste)
Ja, bei Python sind die Batterien eben schon dabei :D

Eine Variable oder ein Parameter sollte nicht list heissen, da dieser Name schon anderweitig benutzt wird.
Auch ist die Verwendung und Zuweisung an eine Globale Variable nicht OOP-like.


Gruß

Dookie

Verfasst: Sonntag 13. Juli 2003, 23:02
von Bugfix
Langsam glaube ich das ich die Tuts da draußen mit zu wenig liebe geschrieben sind...

Aber dann kann ich gleich nochmal die "time.clock()" Funktion austesten und noch ein paar Geschwindigkeitstest machen ;)

Verfasst: Sonntag 13. Juli 2003, 23:15
von Dookie
naja, erstmal sollte man sich die Doku vornemen, und da du ja linux verwendest, starte ein Konsolefenster und da drinn mal python. Dann kannst Du wunderbar die Sachen interaktiv ausprobieren.
Und noch ein Tipp, starte vorher, auch aus dem Konsolefenster "pydoc -g&" dann kannst Du in der Pythondokumentation stöbern.


Gruß

Dookie

Verfasst: Sonntag 13. Juli 2003, 23:40
von Bugfix
Also hier meine Ergebnisse auf meinem 450er mit MP3s im Hintergrund :

Encode Variante 1 ( 1x [Durschnitt]) :0.0
Encode Variante 1 (5000x:0.013
Encode Variante 2 ( 1x [Durschnitt]) :0.0
Encode Variante 2 (5000x) :0.217666666667

Decode Variante 1 ( 1x [Durschnitt]) :0.0
Decode Variante 1 (5000x) :0.0153333333333
Decode Variante 2 ( 1x [Durschnitt]) :0.000166666666667
Decode Variante 2 (5000x) :0.243

Code hier :

Code: Alles auswählen

import time

def encode2(liste,n):
    global dbencode
    dbencode =  liste[1:-1].replace("'","").replace(", ","|")
    return dbencode

def decode2(liste,n):
    global dbdecode
    dbdecode = liste.split("|")
    return dbdecode


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


lister="('[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')"

ta1 = 0.000
tb1 = 0.000
ta2 = 0.000
tb2 = 0.000
tg1 = 0.000
tg2 = 0.000
tg3 = 0.000
tg4 = 0.000
tg5 = 0.000
tg6 = 0.000
tg7 = 0.000
tg8 = 0.000

tl  = 5000

for x in range(0,tl):
    ta1 = time.clock()
    encode2(lister,16)
    ta2 = time.clock()
    tg1 = float(ta2) - float(ta1)
    tg2 = float(tg2) + float(tg1)
    
    tb1 = time.clock()
    decode2(dbencode,16)
    tb2 = time.clock()
    tg3 = float(tb2) - float(tb1)
    tg4 = float(tg4) + float(tg3)

tg1 = float(tg1) / 60.000
tg2 = float(tg2) / 60.000
tg3 = float(tg3) / 60.000
tg4 = float(tg4) / 60.000
    
for x in range(0,tl):
    ta5 = time.clock()
    encode(lister,16)
    ta6 = time.clock()
    tg5 = float(ta6) - float(ta5)
    tg6 = float(tg6) + float(tg5)
    
    tb1 = time.clock()
    decode(dbencode,16)
    tb2 = time.clock()
    tg7 = float(tb2) - float(tb1)
    tg8 = float(tg8) + float(tg7)

tg5 = float(tg5) / 60.000
tg6 = float(tg6) / 60.000
tg7 = float(tg7) / 60.000
tg8 = float(tg8) / 60.000

print 'Encode Variante 1 (  1x [Durschnitt]) :' + str(tg1)
print 'Encode Variante 1 ('+str(tl)+'x:' + str(tg2)
print 'Encode Variante 2 (  1x [Durschnitt]) :' + str(tg5)
print 'Encode Variante 2 ('+str(tl)+'x) :' + str(tg6)
print
print 'Decode Variante 1 (  1x [Durschnitt]) :' + str(tg3)
print 'Decode Variante 1 ('+str(tl)+'x) :' + str(tg4)
print 'Decode Variante 2 (  1x [Durschnitt]) :' + str(tg7)
print 'Decode Variante 2 ('+str(tl)+'x) :' + str(tg8)
Arbeite mit IdleFork ;) - Python ist bestimmt echt klasse wenn man alle "Kniffe" kennt :)

Verfasst: Montag 14. Juli 2003, 09:48
von Milan
Also wenn ich das jetzt richtig aus deinem Code gelsesn habe, ist Dookie seine Variante "Decode Variante 1" usw. --> dann empfiehlt es sich auch die Funktionen entsprechend zu benennen, weil man sonst leicht durcheinander kommt. Auch solltest du bei Geschwindigkeitstests möglichst keine Hintergrundprozesse laufen haben, das beeinflusst die Ergebnisse ganz schön doll... :wink:

Milan

Verfasst: Montag 14. Juli 2003, 13:44
von Dookie
Unter For oder While gibts auch ein paar schöne Beispiele für Performancetests.

Gruß

Dookie