[Python-ideas] More friendly access to chmod

Ram Rachum ram at rachum.com
Mon Jan 11 07:02:44 EST 2016


Hi everyone,

I spent some time thinking about this. I come up with a big and impressive
API, then figured it's overkill, shelved it and made a simpler one :)

Here's my new preferred API. Assume that `path` is a `pathlib.Path` object.

        Checking the chmod of the file:
            int(path.chmod) # Get an int 393 which in octal is 0o611
            oct(path.chmod) # Get a string '0o611'
            str(path.chmod) # Get a string 'rw-r--r--'
            repr(path.chmod) # Get a string '<Chmod: rw-r--r-- / 0o611>

        Modifying the chmod of the file:
            path.chmod(0o611) # Set chmod to 0o611 (for backward
compatibility)
            path.chmod = 0o611 # Set chmod to 0o611
            path.chmod = 393 # Set chmod to 0o611, which is 393 in decimal
            path.chmod = other_path.chmod # Set chmod to be the same as
that of some other file
            path.chmod = 'rw-r--r--' # Set chmod to 0o611
            path.chmod += '--x--x--x' # Add execute permission to everyone
            path.chmod -= '----rwx' # Remove all permissions from others

I've chosen += and -=, despite the fact they're not set operations, because
Python doesn't have __inand__. On an unrelated note, maybe we should have
__inand__? (I mean x ^~= y)

What do you think?


On Sat, Jan 9, 2016 at 6:11 PM, Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, Jan 10, 2016 at 3:06 AM, Ram Rachum <ram at rachum.com> wrote:
> > Thanks for the reference. Personally I think that
> `my_path.stat().st_mode &
> > stat.S_IXGRP` is not human-readable enough. I'll work on a nicer API.
> > Probably this for the same action you described:
> >
> > 'x' in my_path.chmod()['g']
> >
> >
>
> Okay. I'm not sure how popular that'll be, but sure.
>
> As an alternative API, you could have it return a tuple of permission
> strings, which you'd use thus:
>
> 'gx' in my_path.mode() # Group eXecute permission is set
>
> But scratch your own itch, and don't give in to the armchair advisers.
>
> ChrisA
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160111/54cb7c98/attachment.html>


More information about the Python-ideas mailing list