Hallo ich bin gerade dabei ein altes perl script zu pythonisieren. Hierbei treffe ich auf folgendes Konstrukt (ps: leider wenig Ahnung was das macht): #################################### my $ts=time; print $ts; my @ts = ( (($ts & 0xff000000) >> 24), (($ts & 0xff0000) >> 16), (($ts & 0xff00) >> 8), (($ts & 0xff)) ); print "\n"; print @ts; print "\n"; ################################### Kann mir jemand sagen was das macht, und wie kann man das in Python umsetzen? mfg Christian _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Hi Christian, --On Mittwoch, 5. Januar 2005 11:21 Uhr +0100 Christian Klinger <cklinger@novareto.de> wrote:
Hallo
ich bin gerade dabei ein altes perl script zu pythonisieren.
Hierbei treffe ich auf folgendes Konstrukt (ps: leider wenig Ahnung was das macht):
#################################### my $ts=time; print $ts; my @ts = ( (($ts & 0xff000000) >> 24), (($ts & 0xff0000) >> 16), (($ts & 0xff00) >> 8), (($ts & 0xff)) );
print "\n"; print @ts; print "\n";
Offenbar wird hier ein 4-Byte String in ein 4-Tupel von Ints aufgedröselt. In Python sollte das irgendwie so gehen: tp = (ord(ts[0]), ord(ts[1]), ord(ts[2]), ord(ts[3])) Andreas _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Andreas Jung wrote:
Hi Christian,
--On Mittwoch, 5. Januar 2005 11:21 Uhr +0100 Christian Klinger <cklinger@novareto.de> wrote:
Hallo
ich bin gerade dabei ein altes perl script zu pythonisieren.
Hierbei treffe ich auf folgendes Konstrukt (ps: leider wenig Ahnung was das macht):
#################################### my $ts=time; print $ts; my @ts = ( (($ts & 0xff000000) >> 24), (($ts & 0xff0000) >> 16), (($ts & 0xff00) >> 8), (($ts & 0xff)) );
print "\n"; print @ts; print "\n";
Offenbar wird hier ein 4-Byte String in ein 4-Tupel von Ints aufgedröselt. In Python sollte das irgendwie so gehen:
tp = (ord(ts[0]), ord(ts[1]), ord(ts[2]), ord(ts[3]))
Andreas
Wie ist das mit den 4-Byte String zu verstehen ? Ich habs so versucht: ts=str(int(mktime(gmtime()))) print ts tp = (ord(ts[0]), ord(ts[1]), ord(ts[2]), ord(ts[3])) print tp mit diesem ergebnis oben das perl ergebnis unten python: # perl -w kti.pl 1104926156 65219213204 # python t.py 1104922560 (49, 49, 48, 52) mfg Christian _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
--On Mittwoch, 5. Januar 2005 12:13 Uhr +0100 Christian Klinger <cklinger@novareto.de> wrote:
Andreas Jung wrote:
Offenbar wird hier ein 4-Byte String in ein 4-Tupel von Ints aufgedröselt.
Oder eher ein Integer aus 4 Byte, wobei der Code Krampf ist, weil er nicht die byte-order beachtet. Andreas _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Meinst Du vielleicht so etwas: ostr=("%d%d%d%d" % ( (ts & 0xff000000) >> 24 , (ts & 0xff0000) >> 16 , ~ (ts & 0xff00) >> 8 , (ts & 0xff))) print ostr Da ich den Sinn nicht verstehe, halte ich eine 1:1 Umsetzung am sinnvollsten. Gerhard - ------------ Christian Klinger wrote: | Andreas Jung wrote: | |> Hi Christian, |> |> --On Mittwoch, 5. Januar 2005 11:21 Uhr +0100 Christian Klinger |> <cklinger@novareto.de> wrote: |> |>> Hallo |>> |>> ich bin gerade dabei ein altes perl script zu pythonisieren. |>> |>> Hierbei treffe ich auf folgendes Konstrukt (ps: leider wenig Ahnung was |>> das macht): |>> |>> #################################### |>> my $ts=time; |>> print $ts; |>> my @ts = ( (($ts & 0xff000000) >> 24), |>> (($ts & 0xff0000) >> 16), |>> (($ts & 0xff00) >> 8), |>> (($ts & 0xff)) ); |>> |>> print "\n"; |>> print @ts; |>> print "\n"; |>> |> |> Offenbar wird hier ein 4-Byte String in ein 4-Tupel von Ints |> aufgedröselt. |> In Python sollte das irgendwie so gehen: |> |> tp = (ord(ts[0]), ord(ts[1]), ord(ts[2]), ord(ts[3])) |> |> Andreas |> |> | Wie ist das mit den 4-Byte String zu verstehen ? | | Ich habs so versucht: | | ts=str(int(mktime(gmtime()))) | print ts | tp = (ord(ts[0]), ord(ts[1]), ord(ts[2]), ord(ts[3])) | print tp | | mit diesem ergebnis oben das perl ergebnis unten python: | | # perl -w kti.pl | 1104926156 | 65219213204 | | # python t.py | 1104922560 | (49, 49, 48, 52) | | mfg Christian | | _______________________________________________ | python-de maillist - python-de@python.net | http://python.net/mailman/listinfo/python-de | - -- - ------------------------------------------------------ skequell ------ ~ Gerhard Quell Software & Knowledge Engineering ~ Schützenweg 3 eMail: gquell@skequell.de Fon: 0731-26400651 ~ 89275 Elchingen web : http://www.skequell.de Fax: 0731-26400652 - --------------- pgp: 473EC53C - http://www.keyserver.net/en--------- -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.3.92 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iD8DBQFB29G9vHbZD0c+xTwRAk4SAKC4xCRGdLMwn++/V/6pstNvHT2+6wCggW2Y ruTS9tWHzIVEe9YAeQh83QE= =6UCU -----END PGP SIGNATURE----- _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Christian Klinger <cklinger@novareto.de> wrote:
mit diesem ergebnis oben das perl ergebnis unten python:
# perl -w kti.pl 1104926156 65219213204
# python t.py 1104922560 (49, 49, 48, 52)
Aha, Perl gibt das so aus -- ich habe diese Sprache seit Jahren nicht mehr benutzt ;-)) --, also z.B.: #! /usr/bin/env python import time,struct jetzt = int(time.time()) print jetzt print "%d%d%d%d" % tuple([ord(i) for i in struct.pack(">i", jetzt)]) ./t.py 1104924973 6521920945 Detlef _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Danke für die vielen Antworten. Nu geht er! Christian _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Christian Klinger schrieb:
#################################### my $ts=time; print $ts; my @ts = ( (($ts & 0xff000000) >> 24), (($ts & 0xff0000) >> 16), (($ts & 0xff00) >> 8), (($ts & 0xff)) );
print "\n"; print @ts; print "\n";
###################################
Wird vielleicht die Zeitumgerechnet??? Sieht mir so aus, als wenn man das mit divmod() umsetzten könnte?!?! Das hab ich dazu gefunden: http://de.wikibooks.org/wiki/Python-Programmierung:_Datentypen_Operationen#d... Nur so ein Schuß ins Blaue... -- Genrich _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Christian Klinger <cklinger@novareto.de> wrote:
#################################### my $ts=time; print $ts; my @ts = ( (($ts & 0xff000000) >> 24), (($ts & 0xff0000) >> 16), (($ts & 0xff00) >> 8), (($ts & 0xff)) );
print "\n"; print @ts; print "\n";
###################################
Kann mir jemand sagen was das macht, und wie kann man das in Python umsetzen?
Anscheinend wird hier ein Array erzeugt (und ausgegeben), das die einzelnen Bytes der Integerdarstellung der aktuellen Zeit enthält (vermutlich in Sekunden(?)). Etwas Entsprechendes kannst Du in Python erhalten mit import time, struct bytes = list(struct.pack(">i", time.time())) und beispielsweise (als 4 Integers) in ähnlicher Form ausgeben wie P*rl das vermutlich macht mit jetzt = int(time.time()) print jetzt for i in list(struct.pack(">i", jetzt)): print ord(i), print Hilft das weiter? Detlef _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Am Mittwoch, 5. Januar 2005 11:21 schrieb Christian Klinger:
Hallo
ich bin gerade dabei ein altes perl script zu pythonisieren.
Hierbei treffe ich auf folgendes Konstrukt (ps: leider wenig Ahnung was das macht):
#################################### my $ts=time; print $ts; my @ts = ( (($ts & 0xff000000) >> 24), (($ts & 0xff0000) >> 16), (($ts & 0xff00) >> 8), (($ts & 0xff)) );
print "\n"; print @ts; print "\n";
###################################
Kann mir jemand sagen was das macht, und wie kann man das in Python umsetzen?
Die wörtliche Übersetzung lautet wohl import time ts = int(time.time()) print ts ts = [((ts & 0xff000000) >> 24), ((ts & 0xff0000) >> 16), ((ts & 0xff00) >> 8), ((ts & 0xff))] print "".join(map(str, ts)) Eine Zeitangabe (Sekunden seit "epoch") als 32-Bit Integer wird in Bytes zerlegt, deren Dezimaldarstellung dann zu einem String zusammengeklebt. Die resultierende Ziffernfolge ist ziemlich sinnlos, da sich die ursprüngliche Zeit daraus nicht mehr rekonstruieren lässt. Peter _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Hallo, Am Mittwoch, 05. Jan 2005, 11:21:23 +0100 schrieb Christian Klinger:
ich bin gerade dabei ein altes perl script zu pythonisieren.
#################################### my $ts=time; print $ts; my @ts = ( (($ts & 0xff000000) >> 24), (($ts & 0xff0000) >> 16), (($ts & 0xff00) >> 8), (($ts & 0xff)) );
print "\n"; print @ts; print "\n";
###################################
Kann mir jemand sagen was das macht, und wie kann man das in Python umsetzen?
Vielleicht so: def timesplittobytes(): import time t = int( time.time()) r = [] for i in [ j*8 for j in reversed( range( 4))]: r.append( int( (t & (0xff << i)) >> i)) return r Ein wenig eleganter wäre r.append( int( t >> i) & 0xff) Gruß Bertram -- Bertram Scharpf Stuttgart, Deutschland/Germany http://www.bertram-scharpf.de _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
participants (7)
-
Andreas Jung
-
Bertram Scharpf
-
Christian Klinger
-
Detlef Lannert
-
genrich
-
Gerhard Quell
-
Peter Otten