Skip to content

Instantly share code, notes, and snippets.

@WorryingWonton
Created September 14, 2017 08:49
Show Gist options
  • Save WorryingWonton/7220bb556739185311200fd0b114705c to your computer and use it in GitHub Desktop.
Save WorryingWonton/7220bb556739185311200fd0b114705c to your computer and use it in GitHub Desktop.
Answer to Steve's gist.
class QuestionA:
def __init__(self, question_dict):
self.question_dict = question_dict
class QuestionB:
def __init__(self, question_text, answer_dict):
self.question_text = question_text
self.answer_dict = answer_dict
qa = QuestionA({'What is a question?':'You just asked one.'})
qb = QuestionB('What is a question?', {'You just asked one.': True})
qa_text = [key for key in qa.question_dict.keys()]
# for key, value in qa_text.items():
# print(key)
print(qa_text[0])
qb_text = qb.question_text
print(qb_text)
@m0xb
Copy link

m0xb commented Sep 14, 2017

>>> qa
<__main__.QuestionA object at 0x10e65dbe0>
>>> 
>>> qa.question_dict
{'What is a question?': 'You just asked one.'}
>>> 
>>> qa.question_dict.keys()
dict_keys(['What is a question?'])
>>> 
>>> list(qa.question_dict.keys())
['What is a question?']
>>> 
>>> list(qa.question_dict.keys())[0]
'What is a question?'
  • qa is an object (an instance of the QuestionA class – an instance of a class is called an object)
  • qa.question_dict is a dict object
  • qa.question_dict.keys() is a dict_keys object. It can be iterated over: https://docs.python.org/3.6/library/stdtypes.html#dict-views. It cannot be subscripted, i.e. qa.question_dict.keys()[0] doesn't work.
  • list is a class
  • a list is subscriptable, so list(qa.question_dict.keys())[0] will work, as long as there is at least one element
  • list(...) converts the expression ... (whatever it is) into an instance of list, if possible

The last point could be implemented like so:

class list:
  def __init__(self, iterable):
    self.storage = []
    for el in iterable:
      self.storage.append(el)

@WorryingWonton
Copy link
Author

WorryingWonton commented Sep 14, 2017

    def __init__(self, question_dict):
        self.question_dict = question_dict


class QuestionB:
    def __init__(self, question_text, answer_dict):
        self.question_text = question_text
        self.answer_dict = answer_dict


qa = QuestionA({'What is a question?':'You just asked one.'})
qb = QuestionB('What is a question?', {'You just asked one.': True})

qa_text = [key for key in qa.question_dict.keys()]
qa_answers = list(qa.question_dict.values())[0]



# for key, value in qa_text.items():
#     print(key)

print(qa_text[0])
print(qa_answers)
qb_text =  qb.question_text
print(qb_text)```

@WorryingWonton
Copy link
Author

class QuestionA:
    def __init__(self, question_dict):
        self.question_dict = question_dict


class QuestionB:
    def __init__(self, question_text, answer_dict):
        self.question_text = question_text
        self.answer_dict = answer_dict


qa = QuestionA({'What is a question?':'You just asked one.'})
qb = QuestionB('What is a question?', {'You just asked one.': True})

qa_text = [key for key in qa.question_dict.keys()]
qa_answers = list(qa.question_dict.values())[0]



# for key, value in qa_text.items():
#     print(key)

print(qa_text[0])
print(qa_answers)
qb_text =  qb.question_text
print(qb_text)```


@WorryingWonton
Copy link
Author

WorryingWonton commented Sep 14, 2017

class Quiz:
    def __init__(self, name, topic, difficulty, questions):
        self.name = name
        self.topic = topic
        self.difficulty = difficulty
        self.questions = questions

class Question:
    def __init__(self, question_text, answer_dict):
        self.question_text = question_text
        self.answer_dict = answer_dict
        
    def question_input(self):    
        return question_text
    
    def answer_input(self):
        return answer_dict
    

@m0xb
Copy link

m0xb commented Sep 14, 2017

import json

class ObjectEncoder(json.JSONEncoder):
  def default(self, obj):
    return obj.__dict__

quiz = Quiz('My first quiz', 'basics', 1, [Question("What is the name of this quiz?", {"my first quiz": True})])
print(json.dumps(quiz, cls=ObjectEncoder))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment