Seite 1 von 1

Kleine Hilfe

Verfasst: Dienstag 14. August 2012, 19:16
von alexthebest27
Hallo erstmal an alle ich wollte fragen ob mir jemand helfen kann die Nervige AM/PM Funktion aus einem Plugin zu entfernen das ich aus dem Internet hab ??? Ich bin zwar nicht mehr der voll Noob in Python aber da schaltet sich mein hirn ab :) :) :) :) Wäre echt nett Ich versuch das Plugin nämlich vom Englischen ins Deutsche zu übersetzten ;)

Code: Alles auswählen

weekdaymatch = re.match('(?P<weekday>on (?P<dayofweek>(montag?|dienstag?|mittwoch?|donnerstag?|freitag?|samstag?|sontag?)) at ((?P<weekdayhour>([0-9]{1,2}):?\s*([0-9]{2})?\s*)(?P<weekdayampm>(am|pm))?|noon|midnight)(\s|$))', answer, re.IGNORECASE)
        if weekdaymatch is not None:
            #print "weekdayfound"
            timetype = "Hour"
            dayofweek = weekdaymatch.group('dayofweek')
            parsehour = weekdaymatch.group('weekdayhour')
            hour = weekdaymatch.group('weekdayhour')
            timehint = weekdaymatch.group('weekdayampm')
            #print timehint
            #print len(hour)
            #print len(hour)
            if dayofweek == "montag":
                dayofweek = 1
            if dayofweek == "dienstag":
                dayofweek = 2
            if dayofweek == "mittwoch":
                dayofweek = 3
            if dayofweek == "donnerstag":
                dayofweek = 4
            if dayofweek == "freitag":
                dayofweek = 5
            if dayofweek == "samstag":
                dayofweek = 6
            if dayofweek == "sontag":
                dayofweek = 7
            if len(str(hour)) < 4:
                for pmhints in pmWords:
                    if pmhints in timehint:
                        #print "using PM"
                        #print hour
                        if int(hour) == 12:
                            correctTime = x.replace(hour=int(hour), minute=0, second=0, microsecond=0)
                        if int(hour) != 12:
                            hour = int(hour) + 12
                            correctTime = x.replace(hour=hour, minute=0, second=0, microsecond=0)
                for amhints in amWords:
                    if amhints in timehint:
                        #print "using AM"
                        if int(hour) == 12:
                            correctTime = x.replace(hour=23, minute=59, second=0, microsecond=0)
                        if int(hour) != 12:
                            correctTime = x.replace(hour=int(hour), minute=0, second=0, microsecond=0)
            if len(str(hour)) > 3:
                if len(str(hour)) == 4:
                    hour = parsehour[0]
                    minutes = parsehour[1:3]
                if len(str(hour)) == 5:
                    hour = parsehour[0:2]
                    minutes = parsehour[2:4]
                #print hour
                #print minutes
                x = datetime.now(tz)
                for pmhints in pmWords:
                    if pmhints in timehint:
                        #print "using PM"
                        if int(hour) == 12:
                            correctTime = x.replace(hour=int(hour), minute=int(minutes), second=0, microsecond=0)
                        if int(hour) != 12:
                            hour = int(hour) + 12
                            correctTime = x.replace(hour=int(hour), minute=int(minutes), second=0, microsecond=0)
                for amhints in amWords:
                    if amhints in timehint:
                        #print "using AM"
                        if int(hour) == 12:
                            correctTime = x.replace(hour=23, minute=59, second=0, microsecond=0)
                        if int(hour) != 12:
                            correctTime = x.replace(hour=int(hour), minute=int(minutes), second=0, microsecond=0)
            #print x.isoweekday()
            #print dayofweek
            if x.isoweekday() < dayofweek:
                difference = dayofweek - x.isoweekday()
                correctTime = correctTime + timedelta(days=difference)
            if x.isoweekday() > dayofweek:
                difference = dayofweek - x.isoweekday() + 7
                correctTime = correctTime + timedelta(days=difference)
Dankeschön :)

Re: Kleine Hilfe

Verfasst: Dienstag 14. August 2012, 19:24
von Malachite
Könntest du bitte

Code: Alles auswählen

-Tags verwenden?

Re: Kleine Hilfe

Verfasst: Dienstag 14. August 2012, 20:19
von Py-Prog
Wenn du nur a.m. Zeit willst dann würde ich den block:

Code: Alles auswählen

                for pmhints in pmWords:
                    if pmhints in timehint:
                        #print "using PM"
                        #print hour
                        if int(hour) == 12:
                            correctTime = x.replace(hour=int(hour), minute=0, second=0, microsecond=0)
                        if int(hour) != 12:
                            hour = int(hour) + 12
                            correctTime = x.replace(hour=hour, minute=0, second=0, microsecond=0)
[/code]
raus nehmen. Da steht ja immer #print "using PM oder AM. Vergiss den Zweiten block nicht. Ich hoffe das funktioniert.

Re: Kleine Hilfe

Verfasst: Dienstag 14. August 2012, 20:25
von Hyperion
Py-Prog hat geschrieben: Aber in dem Fall löst man das so viel besser:

Code: Alles auswählen

dayofweek = {'montag': 1, 'dienstag': 2, ...}[dayofweek]
Also da es sich um einen numerischen Index handelt, nimmt man besser eine Liste und generiert sich den Index dazu (`enumerate`) oder mittels `list.index`:

Code: Alles auswählen

In [2]: days = ["Mo", "Di", "Mi", "Do", "Fr", "Sa", "So"]

In [3]: days.index("Mi")
Out[3]: 2

In [4]: days.index("Mi") + 1
Out[4]: 3

In [5]: list(enumerate(days, 1))
Out[5]: [(1, 'Mo'), (2, 'Di'), (3, 'Mi'), (4, 'Do'), (5, 'Fr'), (6, 'Sa'), (7, 'So')]

Re: Kleine Hilfe

Verfasst: Dienstag 14. August 2012, 23:50
von alexthebest27
Dankeeeeschööön :) euch beiden funkt. perfekt :) :) :) :) :) Doch jetzt tretet mir ein weiteres Problem auf. Den ich versuch das ganze nicht nur ins Deutsche sondern in andere Sprachen zu übersetzten (MultiKulti halt :) )
Zum Beispiel im Deutschen sage ich, wenn ein ereignis zeitbasiert ist nur die zwei Zahlen (z.b 14 26 uhr) aber im Polnischen zum Beispiel sagt man die zahl, aber ein bisschen verändert (Sowas wie die Merzahl der Zahl). Ist schwer zu verstehen ich weiß... und jetzt bräucht ich wieder eure hilfe .... Ich bräucht sowas wie einen code der zuerst das Gesprochene aufnimmt, es in das was ich will verändert und dann einfach weitergibt sodas das Plugin normal laufen kann. Wäre wieder echt nett ;)

Hier ist der Code Endlich ohne AM/PM :)

Code: Alles auswählen

            if regex.group('hour'):
                timetype = "Hour"
                ##print "Using hour"
                ##print regex.group('ampm')
                parsehour = regex.group('hour')
                hour = regex.group('hour')
                timehint = regex.group('ampm')
                ##print len(str(hour))
                if len(str(hour)) < 4:
                    for amhints in amWords:
                        if amhints in timehint:
                            ##print "using AM"
                            if int(hour) == 24:
                                correctTime = x.replace(hour=23, minute=59, second=0, microsecond=0)
                            if int(hour) != 24:
                                correctTime = x.replace(hour=int(hour), minute=0, second=0, microsecond=0)
                if len(str(hour)) > 3:
                    if len(str(hour)) == 4:
                        hour = parsehour[0]
                        minutes = parsehour[1:3]
                    if len(str(hour)) == 5:
                        hour = parsehour[0:2]
                        minutes = parsehour[2:4]
                    x = datetime.now(tz)
                    for pmhints in pmWords:
                        if pmhints in timehint:
                            ##print "using PM"
                            if int(hour) == 12:
                                correctTime = x.replace(hour=int(hour), minute=int(minutes), second=0, microsecond=0)
                            if int(hour) != 12:
                                hour = int(hour) + 12
                                correctTime = x.replace(hour=int(hour), minute=int(minutes), second=0, microsecond=0)
                    for amhints in amWords:
                        if amhints in timehint:
                            ##print "using AM"
                            if int(hour) == 12:
                                correctTime = x.replace(hour=23, minute=59, second=0, microsecond=0)
                            if int(hour) != 12:
                                correctTime = x.replace(hour=int(hour), minute=int(minutes), second=0, microsecond=0)
                if lastword == "by":
                    correctTime = correctTime - timedelta(minutes=30)
                if correctTime < x:
                    correctTime = correctTime + timedelta(days=1)

Re: Kleine Hilfe

Verfasst: Dienstag 21. August 2012, 16:08
von Leonidas
alexthebest27 hat geschrieben:Zum Beispiel im Deutschen sage ich, wenn ein ereignis zeitbasiert ist nur die zwei Zahlen (z.b 14 26 uhr) aber im Polnischen zum Beispiel sagt man die zahl, aber ein bisschen verändert (Sowas wie die Merzahl der Zahl). Ist schwer zu verstehen ich weiß...
Ne ist nicht kompliziert. Polnisch hat einfach zwei verschiedene Pluralformen, je nachdem ob die Zahl mit 1-4 endet oder 5-9 (und 0). Und damit ist es AFAIK gar nicht die komplizierteste osteuropäische Sprache was Pluralformen angeht.
alexthebest27 hat geschrieben:und jetzt bräucht ich wieder eure hilfe .... Ich bräucht sowas wie einen code der zuerst das Gesprochene aufnimmt, es in das was ich will verändert und dann einfach weitergibt sodas das Plugin normal laufen kann. Wäre wieder echt nett ;)
Mag das jemand in einen verständlichen Satz übersetzen?

Re: Kleine Hilfe

Verfasst: Dienstag 21. August 2012, 16:28
von webspider
Leonidas hat geschrieben:Mag das jemand in einen verständlichen Satz übersetzen?
Das Problem ist wohl, dass er uns nicht verrät was er will.