Skip to content

Commit

Permalink
Small refactor to keep the Question2Tex interface consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Sassoulas committed Feb 24, 2020
1 parent 20ca6ce commit 8146e5b
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ tex/unicode.pdf
csv/multipage.csv
tex/multipage.tex
tex/q13_vs_q13.png
tex/q20_vs_q21.png
32 changes: 21 additions & 11 deletions survey/exporter/tex/question2tex_sankey.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ class Question2TexSankey(Question2Tex):
\\end{figure}
"""

def __init__(self, question, **options):
other_question = options.get("other_question")
if not isinstance(other_question, Question):
msg = "Expected a 'Question' and got '{}'".format(other_question)
msg += " (a '{}').".format(other_question.__class__.__name__)
raise TypeError(msg)
del options["other_question"]
super(Question2TexSankey, self).__init__(question, **options)
self.other_question = other_question

def get_caption_specifics(self):
caption = "%s '%s' (%s) " % (_("for the question"), Question2Tex.html2latex(self.question.text), _("left"))
caption += "%s '%s' (%s) " % (
Expand All @@ -35,7 +45,7 @@ def get_caption_specifics(self):
)
return caption

def tex(self, other_question):
def tex(self):
""" Return a tikz Sankey Diagram of two questions.
The question used during initialization will be left and down the other
Expand All @@ -46,27 +56,27 @@ def tex(self, other_question):
in order for it to work with your latex file.
:param Question other_question: the question we compare to. """
if not isinstance(other_question, Question):
msg = "Expected a 'Question' and got '{}'".format(other_question)
msg += " (a '{}').".format(other_question.__class__.__name__)
raise TypeError(msg)
self.other_question = other_question
self.cardinality = self.question.sorted_answers_cardinality(
self.min_cardinality,
self.group_together,
self.group_by_letter_case,
self.group_by_slugify,
self.filter,
self.sort_answer,
other_question=other_question,
other_question=self.other_question,
)
q1 = []
q2 = []
for answer_to_q1, cardinality_to_q2 in list(self.cardinality.items()):
for answer_to_q2, number_of_time in list(cardinality_to_q2.items()):
q1 += [answer_to_q1] * number_of_time
q2 += [answer_to_q2] * number_of_time
df = DataFrame(data={self.question.text: q1, other_question.text: q2})
name = "tex/q{}_vs_q{}".format(self.question.pk, other_question.pk)
sankey(df[self.question.text], df[other_question.text], aspect=20, fontsize=10, figureName=name)
return Question2TexSankey.TEX_SKELETON % (name[4:], self.question.pk, other_question.pk, self.get_caption())
df = DataFrame(data={self.question.text: q1, self.other_question.text: q2})
name = "tex/q{}_vs_q{}".format(self.question.pk, self.other_question.pk)
sankey(df[self.question.text], df[self.other_question.text], aspect=20, fontsize=10, figureName=name)
return Question2TexSankey.TEX_SKELETON % (
name[4:],
self.question.pk,
self.other_question.pk,
self.get_caption(),
)
4 changes: 2 additions & 2 deletions survey/exporter/tex/survey2tex.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def treat_question(self, question):
elif tex_type == "sankey":
other_question_text = opts["question"]
other_question = Question.objects.get(text=other_question_text)
q2tex = Question2TexSankey(question)
question_synthesis += q2tex.tex(other_question)
q2tex = Question2TexSankey(question, other_question=other_question)
question_synthesis += q2tex.tex()
elif tex_type in ["pie", "cloud", "square", "polar"]:
q2tex = Question2TexChart(question, latex_label=i, **opts)
question_synthesis += q2tex.tex()
Expand Down
8 changes: 4 additions & 4 deletions survey/locale/de/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ msgid "Left blank"
msgstr "Frei gelassen"

#: survey/exporter/tex/question2tex_chart.py:117
#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "for the question"
msgstr "für die Frage"

Expand All @@ -97,15 +97,15 @@ msgstr "Keine Antwort auf diese Frage."
msgid "Participant"
msgstr "Teilnehmer"

#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "left"
msgstr "links"

#: survey/exporter/tex/question2tex_sankey.py:32
#: survey/exporter/tex/question2tex_sankey.py:42
msgid "in relation with the question"
msgstr "in Zusammenhang mit der Frage"

#: survey/exporter/tex/question2tex_sankey.py:34
#: survey/exporter/tex/question2tex_sankey.py:44
msgid "right"
msgstr "rechts"

Expand Down
8 changes: 4 additions & 4 deletions survey/locale/es/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ msgid "Left blank"
msgstr "Dejada en blanco"

#: survey/exporter/tex/question2tex_chart.py:117
#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "for the question"
msgstr "para la pregunta"

Expand All @@ -99,16 +99,16 @@ msgid "Participant"
msgstr "Participante"

# as in "opposite of right"
#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "left"
msgstr "izquierda"

#: survey/exporter/tex/question2tex_sankey.py:32
#: survey/exporter/tex/question2tex_sankey.py:42
msgid "in relation with the question"
msgstr "en relación con la pregunta"

# as in "opposite of left"
#: survey/exporter/tex/question2tex_sankey.py:34
#: survey/exporter/tex/question2tex_sankey.py:44
msgid "right"
msgstr "derecha"

Expand Down
8 changes: 4 additions & 4 deletions survey/locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ msgid "Left blank"
msgstr "Laissé vide"

#: survey/exporter/tex/question2tex_chart.py:117
#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "for the question"
msgstr "pour la question"

Expand All @@ -96,15 +96,15 @@ msgstr "Aucune réponse pour cette question."
msgid "Participant"
msgstr "Participant"

#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "left"
msgstr "à gauche"

#: survey/exporter/tex/question2tex_sankey.py:32
#: survey/exporter/tex/question2tex_sankey.py:42
msgid "in relation with the question"
msgstr "mise en relation avec la question"

#: survey/exporter/tex/question2tex_sankey.py:34
#: survey/exporter/tex/question2tex_sankey.py:44
msgid "right"
msgstr "à droite"

Expand Down
8 changes: 4 additions & 4 deletions survey/locale/ja/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ msgid "Left blank"
msgstr "空白のまま"

#: survey/exporter/tex/question2tex_chart.py:117
#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "for the question"
msgstr "質問の為に"

Expand All @@ -96,15 +96,15 @@ msgstr "この質問に答えない"
msgid "Participant"
msgstr "参加者"

#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "left"
msgstr "左"

#: survey/exporter/tex/question2tex_sankey.py:32
#: survey/exporter/tex/question2tex_sankey.py:42
msgid "in relation with the question"
msgstr "質問に関連して"

#: survey/exporter/tex/question2tex_sankey.py:34
#: survey/exporter/tex/question2tex_sankey.py:44
msgid "right"
msgstr "右"

Expand Down
8 changes: 4 additions & 4 deletions survey/locale/ru/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ msgid "Left blank"
msgstr "Оставить пустым"

#: survey/exporter/tex/question2tex_chart.py:117
#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "for the question"
msgstr "для вопроса"

Expand All @@ -99,16 +99,16 @@ msgid "Participant"
msgstr "Участник"

# as in "opposite of right"
#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "left"
msgstr "не правильно"

#: survey/exporter/tex/question2tex_sankey.py:32
#: survey/exporter/tex/question2tex_sankey.py:42
msgid "in relation with the question"
msgstr "в связи с вопросом"

# as in "opposite of left"
#: survey/exporter/tex/question2tex_sankey.py:34
#: survey/exporter/tex/question2tex_sankey.py:44
msgid "right"
msgstr "правильно"

Expand Down
8 changes: 4 additions & 4 deletions survey/locale/zh/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ msgid "Left blank"
msgstr "留空"

#: survey/exporter/tex/question2tex_chart.py:117
#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "for the question"
msgstr "为此问题"

Expand All @@ -96,15 +96,15 @@ msgstr "该问题尚无回答"
msgid "Participant"
msgstr "参与者"

#: survey/exporter/tex/question2tex_sankey.py:30
#: survey/exporter/tex/question2tex_sankey.py:40
msgid "left"
msgstr "左"

#: survey/exporter/tex/question2tex_sankey.py:32
#: survey/exporter/tex/question2tex_sankey.py:42
msgid "in relation with the question"
msgstr "和此问题相关"

#: survey/exporter/tex/question2tex_sankey.py:34
#: survey/exporter/tex/question2tex_sankey.py:44
msgid "right"
msgstr "右"

Expand Down
16 changes: 8 additions & 8 deletions survey/tests/exporter/tex/test_question2tex_sankey.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@ class TestQuestion2TexSankey(TestManagement):
def test_other_question_type(self):
""" We get a type error if we do not give a Question. """
question = self.survey.questions.get(text="Aèbc?")
q2s = Question2TexSankey(question)
self.assertRaises(TypeError, Question2TexSankey.__init__, question, {"other_question": "other_question"})
other_question = self.survey.questions.get(text="Aèbc?")
self.assertRaises(TypeError, q2s.tex, "other_question")
self.assertIsNotNone(q2s.tex(other_question))
q2s = Question2TexSankey(question, other_question=other_question)
self.assertIsNotNone(q2s.tex())


# Creating a big ranking survey with user takes a long time
"""
def test_big_ranking_survey(self):
""" """
# Creating a big ranking survey with user takes a long time
self.create_big_ranking_survey(with_user=True)
qtext = "How much do you like question {} ?"
from survey.models import Question
q4 = Question.objects.get(text=qtext.format(4))
q5 = Question.objects.get(text=qtext.format(5))
q2tex_sankey = Question2TexSankey(q4, filter=["1"],
group_together={"A" : ["2", "3"]})
q2tex_sankey.tex(q5)
q2tex_sankey = Question2TexSankey(q4, filter=["1"], other_question=q5, group_together={"A": ["2", "3"]})
q2tex_sankey.tex()
"""

0 comments on commit 8146e5b

Please sign in to comment.