Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor MultiResultCard to remove implicit state #194

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 32 additions & 11 deletions app/logic/resultsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,31 @@
from six.moves import map
from six.moves import zip

class CardResult:
"""
A class to encapsulate the result of a card evaluation.

This class holds the card, the evaluation result, the input representation,
and the components related to the card evaluation. It also implements an
equality check to compare CardResult instances based on their evaluation result.

Attributes:
card (ResultCard): The card that was evaluated.
result (Any): The result of the card evaluation.
input_repr (str): The string representation of the input expression.
components (dict): The components related to the card evaluation.
"""

def __init__(self, card, result, input_repr, components):
self.card = card
self.result = result
self.input_repr = input_repr
self.components = components

def __eq__(self, other):
if not isinstance(other, CardResult):
return False
return self.result == other.result

class ResultCard(object):
"""
Expand Down Expand Up @@ -113,20 +138,17 @@ def eval(self, evaluator, components, parameters):
self.cards_used = []
results = []

# TODO Implicit state is bad, come up with better API
# in particular a way to store variable, cards used
for card in self.cards:
try:
result = card.eval(evaluator, components, parameters)
except ValueError:
continue
if result != None:
if not any(result == r[1] for r in results):
if result is not None:
card_result = CardResult(card, result, evaluator.get("input_evaluated"), components)
if card_result not in results:
self.cards_used.append(card)
results.append((card, result))
results.append(card_result)
if results:
self.input_repr = evaluator.get("input_evaluated")
self.components = components
return results
return "None"

Expand All @@ -137,17 +159,16 @@ def format_output(self, output, formatter):
if not isinstance(output, list):
return output
html = ["<ul>"]
for card, result in output:
for card_result in output:
html.append("<li>")
html.append('<div class="cell_input">')
html.append(card.format_input(self.input_repr, self.components))
html.append(card_result.card.format_input(card_result.input_repr, card_result.components))
html.append('</div>')
html.append(card.format_output(result, formatter))
html.append(card_result.card.format_output(card_result.result, formatter))
html.append("</li>")
html.append("</ul>")
return "\n".join(html)


# Decide which result card set to use

def is_derivative(input_evaluated):
Expand Down
72 changes: 36 additions & 36 deletions docker/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@
FROM python:3.7-slim-buster

RUN apt-get update \
# dependencies for building Python packages
&& apt-get install -y build-essential \
&& apt-get install -y python-dev \
&& apt-get install -y wget \
&& apt-get install -y zip unzip

WORKDIR /usr/src/app

COPY requirements.txt ./requirements.txt

RUN pip install -r requirements.txt

# Install PhantomJs and Casperjs for Testing
ENV PHANTOM_JS="phantomjs-1.9.8-linux-x86_64"
RUN apt-get install -y chrpath libssl-dev libxft-dev \
&& apt-get install -y libfreetype6 libfreetype6-dev \
&& apt-get install -y libfontconfig1 libfontconfig1-dev \
&& apt-get install -y git

RUN cd ../ && wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
RUN mv ../$PHANTOM_JS.tar.bz2 /usr/local/share/
RUN cd /usr/local/share/ && tar xvjf $PHANTOM_JS.tar.bz2
RUN ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/share/phantomjs
RUN ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin/phantomjs
RUN ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/bin/phantomjs
RUN rm -rf /usr/local/share/$PHANTOM_JS.tar.bz2

RUN cd ../ && git clone git://github.com/casperjs/casperjs.git \
&& cd casperjs && ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs

ENV PYTHONPATH="/usr/src/app"
ENV PYTHONUNBUFFERED=1
COPY . .
FROM python:3.7-slim-buster
RUN apt-get update \
# dependencies for building Python packages
&& apt-get install -y build-essential \
&& apt-get install -y python-dev \
&& apt-get install -y wget \
&& apt-get install -y zip unzip
WORKDIR /usr/src/app
COPY requirements.txt ./requirements.txt
RUN pip install -r requirements.txt
# Install PhantomJs and Casperjs for Testing
ENV PHANTOM_JS="phantomjs-1.9.8-linux-x86_64"
RUN apt-get install -y chrpath libssl-dev libxft-dev \
&& apt-get install -y libfreetype6 libfreetype6-dev \
&& apt-get install -y libfontconfig1 libfontconfig1-dev \
&& apt-get install -y git
RUN cd ../ && wget https://bitbucket.org/ariya/phantomjs/downloads/$PHANTOM_JS.tar.bz2
RUN mv ../$PHANTOM_JS.tar.bz2 /usr/local/share/
RUN cd /usr/local/share/ && tar xvjf $PHANTOM_JS.tar.bz2
RUN ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/share/phantomjs
RUN ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/local/bin/phantomjs
RUN ln -sf /usr/local/share/$PHANTOM_JS/bin/phantomjs /usr/bin/phantomjs
RUN rm -rf /usr/local/share/$PHANTOM_JS.tar.bz2
RUN cd ../ && git clone https://github.com/casperjs/casperjs.git \
&& cd casperjs && ln -sf `pwd`/bin/casperjs /usr/local/bin/casperjs
ENV PYTHONPATH="/usr/src/app"
ENV PYTHONUNBUFFERED=1
COPY . .