Verfasst: Donnerstag 14. Februar 2008, 09:06
Hab das mal auf der Wiki Seite übernommen...
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Code: Alles auswählen
def symbolic_notation1(mode):
"""
>>> symbolic_notation1(644)
u'rw-r--r--'
>>> symbolic_notation1(40755)
u'rwxr-xr-x'
>>> symbolic_notation1("777")
u'rwxrwxrwx'
"""
return ''.join(
mode & 0400 >> i and x or '-' for i, x in enumerate('rwxrwxrwx')
)
def symbolic_notation2(mode):
"""
>>> symbolic_notation2(644)
u'rw-r--r--'
>>> symbolic_notation2(40755)
u'rwxr-xr-x'
>>> symbolic_notation2("777")
u'rwxrwxrwx'
"""
result = []
for i, x in enumerate('rwxrwxrwx'):
m = mode & 0400 >> i
symbol = m and x or '-'
result.append(symbol)
return ''.join(result)
CHMOD_TRANS_DATA = (
u"---", u"--x", u"-w-", u"-wx", u"r--", u"r-x", u"rw-", u"rwx",
)
def symbolic_notation3(mode):
"""
>>> symbolic_notation3(644)
u'rw-r--r--'
>>> symbolic_notation3(40755)
u'rwxr-xr-x'
>>> symbolic_notation3("777")
u'rwxrwxrwx'
"""
mode = mode & 0777 # strip "meta info"
mode_string = u"%o" % mode
return u''.join(CHMOD_TRANS_DATA[int(num)] for num in mode_string)
if __name__ == "__main__":
import doctest
print doctest.testmod(verbose=False)
Code: Alles auswählen
...
3 items had failures:
3 of 3 in __main__.symbolic_notation1
3 of 3 in __main__.symbolic_notation2
3 of 3 in __main__.symbolic_notation3
***Test Failed*** 9 failures.
TestResults(failed=9, attempted=9)
Code: Alles auswählen
CHMOD_TRANS_DATA = (
u"---", u"--x", u"-w-", u"-wx", u"r--", u"r-x", u"rw-", u"rwx"
)
def chmod_symbol(octal_value):
"""
Transform a os.stat().st_octal_value octal value to a symbolic string.
ignores meta infromation like SUID, SGID or the Sticky-Bit.
e.g. 40755 -> rwxr-xr-x
>>> chmod_symbol(644)
u'rw-r--r--'
>>> chmod_symbol(40755)
u'rwxr-xr-x'
>>> chmod_symbol("777")
u'rwxrwxrwx'
"""
octal_value_string = str(octal_value)[-3:] # strip "meta info"
return u''.join(CHMOD_TRANS_DATA[int(num)] for num in octal_value_string)
Code: Alles auswählen
def chmod_sym(val):
def tripple(v):
return "-r"[bool(v & 4)] + "-w"[bool(v & 2)] + "-x"[bool(v & 1)]
return tripple(val / 64) + tripple(val / 8) + tripple(val)
Code: Alles auswählen
def chmod_sym(val):
"""
>>> chmod_sym(644)
u'rw-r--r--'
>>> chmod_sym(40755)
u'rwxr-xr-x'
>>> chmod_sym("777")
u'rwxrwxrwx'
"""
val = int(val)
def tripple(v):
return "-r"[bool(v & 4)] + "-w"[bool(v & 2)] + "-x"[bool(v & 1)]
return tripple(val / 64) + tripple(val / 8) + tripple(val)
if __name__ == "__main__":
import doctest
print doctest.testmod(verbose=False)
Code: Alles auswählen
**********************************************************************
File "test.py", line 4, in __main__.chmod_sym
Failed example:
chmod_sym(644)
Expected:
u'rw-r--r--'
Got:
'-w----r--'
**********************************************************************
File "test.py", line 6, in __main__.chmod_sym
Failed example:
chmod_sym(40755)
Expected:
u'rwxr-xr-x'
Got:
'r--rw--wx'
**********************************************************************
File "test.py", line 8, in __main__.chmod_sym
Failed example:
chmod_sym("777")
Expected:
u'rwxrwxrwx'
Got:
'r----x--x'
**********************************************************************
1 items had failures:
3 of 3 in __main__.chmod_sym
***Test Failed*** 3 failures.
TestResults(failed=3, attempted=3)
Der Code von sma hätte die Zahlen gerne explizit als Oktalzahl angegeben. Damit schlägt zwangsläufig jeder der bestehenden Tests fehl.jens hat geschrieben:Tut's nicht
Hm. Die kann man allerdings nicht wirklich gut unterscheiden, oder? Also das man die Funktion mit allem füttern kann...sma hat geschrieben:also beispielsweise 420 oder 0x1a4 oder eben 0644. Das sind ja nur drei verschiedene Repräsentationen für die Zahl
OK, mein Fehler...jens hat geschrieben:Hab mit http://wiki.python.de/755%20-%3E%20rwxr-xr-x nochmal angesehen.
Was soll ich sagen, alle drei Beispiele funktionieren nicht wirklich:
Das ist ein Missverständnis. Das gibt in Python keine Zahlen in Okal, Hex, oder was auch immer. Es sind einfach Zahlen, also Objekte vom Typ "int". Wenn etwas als "rw-r--r--" per "ls" angezeigt wird, liefert stat().st_mode da die Zahl 420, die man auch als 0644 ausdrücken kann, aber die immer noch 420 ist. Äh, tatsächlich kommt da 33188 zurück, jedenfalls bei mir, weil da wohl noch irgend so ein OS/X-spezifisches Bit gesetzt ist, aber das ist auch nur 0b1000000110100100 und da sieht man rechts die üblichen Access-Bits "644".jens hat geschrieben:Es geht ja eigentlich um die Umwandlung von os.stat().st_mode und dabei bekommt man halt die Zahl in Octal...
Das ist der Sinn des Wikissma hat geschrieben:Ich habe mir erlaubt, dass mal im Wiki anzupassen.