0

in my helper.py I have a class and a function definition:

class Suites():
    NAME_SUITE = ['NATE', 'TOM', 'TED', 'JAN', 'SAM']

def check_suite(name, suite):
    my_suite = Suites[suite] #I want my_suite to = ['NATE', 'TOM', 'TED', 'JAN', 'SAM']
    return name in suite #I want to return True or False

and in views.py I'm trying to find out that user_in_suite is True

 from .helper import check_suite
 from .helper import Suites

 def apps(request):
    user_name = 'NATE'
    suite_to_check = 'NAME_SUITE'
    user_in_suite = check_suite(name=user_name, suite=suite_to_check)

But I'm getting an error for helper.py: 'classobj' object has no attribute '__getitem__

5
  • Maybe you should instantiate that class? Commented Sep 19, 2017 at 14:13
  • The short answer is name in getattr(Suites(), suite) but that looks like code nightmare to me... what are you actually trying to accomplish here? Commented Sep 19, 2017 at 14:18
  • I'm just trying to find a way in views.py, to see if a user (which will actually be more like: user_name = str(request.user.name)) is part of a given list (NAME_SUITE = ['NATE', 'TOM',..for example) Commented Sep 19, 2017 at 14:23
  • So why not just do that? why does NAME_SUITE need to be in a class of its own and why do you need to reference it via a string rather than just calling the variable? Commented Sep 19, 2017 at 14:24
  • I'm going to add more 'suites' to this eventually. I want an extendable way to see if person X is of list Y Commented Sep 19, 2017 at 14:26

1 Answer 1

1
def check_suite(name, suite):
    my_suite = Suites.__dict__[suite] # __dict__ gives you access to the dictionary of class.
    return name in my_suite

This should solve your problem.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.