Implementing POSIX file mode library I implemented a virtual file system library with POSIX file mode representation.
Any suggestions regarding optimization are welcome.

"""POSIX file system library."""

from collections import namedtuple

__all__ = ['PosixPermission', 'PosixMode']


MIN_PERM = 0b000
MAX_PERM = 0b111
DEF_PERM = 0b110    # rw-
MIN_MODE = 0o000
MAX_MODE = 0o777
DEF_MODE = 0o640


class PosixPermission(namedtuple('PosixPermission', 'read write execute')):
    """POSIX file permissions."""

    @classmethod
    def from_int(cls, perm=DEF_PERM):
        """Creates the file permissions from the respective integer value."""
        if MIN_PERM &lt;= perm &lt;= MAX_PERM:
            return cls(bool(perm &amp; 4), bool(perm &amp; 2), bool(perm &amp; 1))

        raise ValueError('Permission value must be between {} and {}.'.format(
            MIN_PERM, MAX_PERM))

    def __int__(self):
        """Returns the integer value of the file permission."""
        return (self.read and 4) + (self.write and 2) + (self.execute and 1)

    def __str__(self):
        """Returns binary representation of the file permission."""
        return ''.join(map(
            lambda perm: perm or '-',
            (self.read and 'r', self.write and 'w', self.execute and 'x')))


class PosixMode(namedtuple('PosixMode', 'user group other')):
    """POSIX file mode."""

    @classmethod
    def from_int(cls, mode=DEF_MODE):
        """Creates the file mode from the respective integer value."""
        if MIN_MODE &lt;= mode &lt;= MAX_MODE:
            return cls(
                PosixPermission.from_int(mode &gt;&gt; 6 &amp; 7),
                PosixPermission.from_int(mode &gt;&gt; 3 &amp; 7),
                PosixPermission.from_int(mode &amp; 7))

        raise ValueError('File mode must be between {} and {}.'.format(
            oct(MIN_MODE), oct(MAX_MODE)))

    def __int__(self):
        """Returns the integer value of the file mode."""
        return (int(self.user) &lt;&lt; 6) + (int(self.group) &lt;&lt; 3) + int(self.other)

    def __str__(self):
        """Returns the octal representation of the file mode."""
        return ''.join((str(self.user), str(self.group), str(self.other)))
I'm confused why you're using a named tuple for PosixPermission. To me an Enum would be a better choice:

class PosixPermission(Flag):
    READ = 4
    WRITE = 2
    EXECUTE = 1
    NONE = 0

    @property
    def perms(self):
        return ''.join(
            name if self &amp; val else '-'
            for name, val in zip('rwx', PosixPermission)
        )




&gt;&gt;&gt; PosixPermission(0)
&lt;PosixPermission.NONE: 0&gt;
&gt;&gt;&gt; PosixPermission(7)
&lt;PosixPermission.READ|WRITE|EXECUTE: 7&gt;
&gt;&gt;&gt; PosixPermission(5)
&lt;PosixPermission.READ|EXECUTE: 5&gt;
&gt;&gt;&gt; PosixPermission(5).perms
'r-x'
&gt;&gt;&gt; PosixPermission(5) &amp; PosixPermission.READ
&lt;PosixPermission.READ: 4&gt;
&gt;&gt;&gt; PosixPermission(5) &amp; PosixPermission.WRITE
&lt;PosixPermission.NONE: 0&gt;
&gt;&gt;&gt; PosixPermission(8)
Traceback (most recent call last):
ValueError: 8 is not a valid PosixPermission




Honestly PosixMode looks pretty good, I think it's reasonable to assume it to be a namedtuple, rather than an object with __slots__. I however wouldn't use __int__ and __str__, and would instead use properties for these.