# Calculating chmod based on user input [closed]

This program asks the user for input three times, and based on that input, calculates the chmod. I have included comments with questions and clarifications in the code.

I would like to have these questions answered and help with making the if ... in ... part shorter, if that's possible.

'''
This program asks the user for input three times, and based on that input, calculates the chmod.
'''

'''
This is a simple function that prevents me from repeating myself.
Usage:
whoCan('execute')
will return 'Who can execute?'
'''

def whoCan(doThis):
'''
If I use print instead of return here, this is the output:

None

Why is that?
'''
return "Who can %s?" % doThis

'''
Ask the user for input in the form of 'ow', 'gr' and 'ot'.
FYI:
ow = owner
gr = group
ot = other

chmod 777 would be  owgrot
owgrot
owgrot
'''

write = raw_input(whoCan('write'))
execute = raw_input(whoCan('execute'))

'''
I didn't have any success with
Why?
'''

writeValue = 0
executeValue = 0

# There must be a shorter way to write this...

if 'ow' in write:
writeValue += 4
if 'gr' in write:
writeValue += 2
if 'ot' in write:
writeValue += 1

if 'ow' in execute:
executeValue += 4
if 'gr' in execute:
executeValue += 2
if 'ot' in execute:
executeValue += 1

# And print the chmod



## closed as off-topic by 200_successApr 28 '16 at 18:21

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Questions containing broken code or asking for advice about code not yet written are off-topic, as the code is not ready for review. After the question has been edited to contain working code, we will consider reopening it." – 200_success
If this question can be reworded to fit the rules in the help center, please edit the question.

• If you answer "Who can read?" ow, "Who can write?" ow, "Who can execute?" ow, then the answer should be 700, but you actually print 4 4 4. – 200_success Apr 28 '16 at 18:22
• I think this link with an interactive calculator may help you understand it better ss64.com/bash/chmod.html – Caridorc Apr 28 '16 at 18:24

### Concise coding

After moving the documentation of the function after the line containing def as the convention stands we have:

def whoCan(doThis):
'''
This is a simple function that prevents me from repeating myself.
Usage:
whoCan('execute')
will return 'Who can execute?'

If I use print instead of return here, this is the output:

None

Why is that?
'''
return "Who can %s?" % doThis


The second paragraph is just a doubt that is not really relevant to this function (the reason is that: using print creates a function with no return of which the return is implicitly None, that is printed by raw_input), so we can remove it:

def whoCan(doThis):
'''
This is a simple function that prevents me from repeating myself.
Usage:
whoCan('execute')
will return 'Who can execute?'
'''
return "Who can %s?" % doThis


I can already see that it is a (simple) function, and functions are usually used to avoid repetition so we can remove it too:

def whoCan(doThis):
'''
Usage:
whoCan('execute')
will return 'Who can execute?'
'''
return "Who can %s?" % doThis


In fact we may remove this whole function as it is just doing "Who can {}?".format(action) where x is the input.

To avoid repetition we will use a generator expression:

read, write, execute = (raw_input("Who can {}?".format(action)) for action in ('read', 'write', 'execute'))


The last part can be shortened with a dictionary and a function that calls sum on a generator expression:

name_to_val = {'ow' : 4, 'gr' : 2, 'ot' : 1}

def chmod_value(x):
return sum(name_to_val(name) for name in name_to_val.keys() if name in x)


You also do not need to initialize variables with zero, in fact you do not need variables at all:

print [chmod_value(x) for x in (read, write, execute)]

• Iterating a dictionary goes through its keys. Using .keys() in the generator expression is redundant. – zondo Apr 29 '16 at 13:09