HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Efficient dictionary printing

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
dictionaryprintingefficient

Problem

Here's what my task is:


Dependant on the "Logged In" user, they'll have different access rights and will see different menus.

I have tried using the .get() function.

choices = {1: "| Student Details", 2: "| Student Grades", 3: "| Finance", 4:"| Staff Details", 5:"| Staff Payments"}

print(choices.get(1))
print(choices.get(4))

GIVEN OUTPUT >>

| Student Details
| Staff Details


But it will look very inefficient with all the Access Rights. Some access rights have access to different menus e.g (Access Level 1 = Menu 1 and Menu 4). So I cannot just use a for loop to print all menus like so.

for option,num in choices.items():
    print(option,num)


I've also attempted using the itemgetter() function which is available in the operator module, but I was unable of figuring out how to:
  1. Print the outcome in lines, 2. Numerate the outcome in order (1,2,3)



choices = {1: "|Student Details", 2: "| Student Grades", 3: "| Finance", 4:"| Staff Details", 5:"| Staff Payments"}

print(itemgetter(3,5)(choices))

GIVEN OUTPUT >>
('| Finance', '| Staff Payments')

REQUIRED OUTPUT >>
1 | Finance
2 | Staff Payments


Here's an example of the inefficient code I have at the moment:

if user_name == "Mike":
    print("-Managing Menu-")
    print("1 | Staff Payments. ")
    print("2 | Finance. ")
    option = int(input("Option Choice (1-2): "))
    while option not in range(1,2):
        option = int(input("Option Choice (1-2): "))
    if option == 1:
        print("Welcome to Staff Payments")
    elif option == 2:
        print("Welcome to Finance.")
    else:
        print("Error.")


I'd really appreciate any feedback and help with this task.

Solution

You say that using itemgetter is better than using choices.get, as you don't have to call get multiple times,
however this is wrong.

If we look at itemgetter it says that it uses __getitem__.
Python's function get also uses __getitem__ but allows a default value.
And so the following give the same:

print(itemgetter(3, 5)(choices))
print(tuple(map(choices.get, [3, 5])))


As you say using the above is bad, as it doesn't print things on new lines, you could use str.join.
As you want to join each one with a newline or '\n'.

print('\n'.join(map(choices.get, [3, 5])))


In both examples this won't give the same output as your inefficient code.
As there are no numbers before the choice.

To improve your inefficient code, you could alternately make an options dictionary,
and use options.get but with a supplied default value.

options = {
    1: "Welcome to Staff Payments",
    2: "Welcome to Finance"
}
print(options.get(option, "Error."))


Alternately to your current code pattern, you can make a dictionary holding users with places they are allowed to.
This would be by supplying user-names, and the allowed places in a dictionary.

# Access control list
ACL = {
    "Mike": [3, 5],
    "Joe": [1]
}

places = ACL[user_name]
options = dict(enumerate(map(choices.get, places)))
print("-Managing Menu-")
for option, num in choices.items():
    print("{} {}".format(option, num))

option = int(input("Option Choice (1-2): "))
while option not in range(1,2):
    option = int(input("Option Choice (1-2): "))

if option == 1:
    print("Welcome to Staff Payments")
elif option == 2:
    print("Welcome to Finance.")
else:
    print("Error.")


As you can see, whilst we took advantage of a new dictionary to print the allowed places,
you would still need boilerplate for all the locations.
Instead you could use str.format to display the places, just like I did for the menu.

choices = {
    1: "Student Details",
    2: "Student Grades",
    3: "Finance",
    4: "Staff Details",
    5: "Staff Payments"
}

def managing_menu(user_name):
    places = ACL[user_name]
    options = dict(enumerate(map(choices.get, places)))
    print("-Managing Menu-")
    for option, place in choices.items():
        print("{} | {}".format(option, place))

    option = int(input("Where do you wish to go: "))
    while option not in options:
        option = int(input("Where do you wish to go: "))

    location = options.get(option, None)
    if location is None:
        print("Error.")
        return

    print("Welcome to {}.".format(location))


There are some other ways that you can improve the code:

-
don't use raw int(input())'s instead use them with a try and except.
This will cause the program to not crash if you enter Finance, rather than 2.

-
You also are probably calling a function after printing "Welcome to Finance",
and so you would need to add that functionality to the options dictionary.
(It's not hard)

-
You should change the place you define option to become a function,
this is as you are likely to need the function in more than that one location.

Code Snippets

print(itemgetter(3, 5)(choices))
print(tuple(map(choices.get, [3, 5])))
print('\n'.join(map(choices.get, [3, 5])))
options = {
    1: "Welcome to Staff Payments",
    2: "Welcome to Finance"
}
print(options.get(option, "Error."))
# Access control list
ACL = {
    "Mike": [3, 5],
    "Joe": [1]
}

places = ACL[user_name]
options = dict(enumerate(map(choices.get, places)))
print("-Managing Menu-")
for option, num in choices.items():
    print("{} {}".format(option, num))

option = int(input("Option Choice (1-2): "))
while option not in range(1,2):
    option = int(input("Option Choice (1-2): "))

if option == 1:
    print("Welcome to Staff Payments")
elif option == 2:
    print("Welcome to Finance.")
else:
    print("Error.")
choices = {
    1: "Student Details",
    2: "Student Grades",
    3: "Finance",
    4: "Staff Details",
    5: "Staff Payments"
}

def managing_menu(user_name):
    places = ACL[user_name]
    options = dict(enumerate(map(choices.get, places)))
    print("-Managing Menu-")
    for option, place in choices.items():
        print("{} | {}".format(option, place))

    option = int(input("Where do you wish to go: "))
    while option not in options:
        option = int(input("Where do you wish to go: "))

    location = options.get(option, None)
    if location is None:
        print("Error.")
        return

    print("Welcome to {}.".format(location))

Context

StackExchange Code Review Q#117009, answer score: 2

Revisions (0)

No revisions yet.