<commit_before>from pyramid.view import view_config
from sqlalchemy.sql import text

from ..models import (
    DBSession,
    Poll
    )

from .base import DefaultView
from .. import validation
from ..exceptions import HTTPNotFoundError
from ..validation import validator

import logging
LOG = logging.getLogger(__name__)

GET = "GET"

def get_counts(poll_id):
    params = {"poll_id": poll_id}
    query = text("""\
SELECT choice.id,
       choice.text,
       Count(DISTINCT response.ip_address) AS unique_count,
       Count(response.ip_address) AS raw_count
FROM choice
LEFT OUTER JOIN response ON response.choice_id = choice.id
WHERE choice.poll_id = :poll_id
GROUP BY choice.id;
""")
    result = DBSession.execute(query, params)
    if result:
        return [{"name": r.text,
                 "votes": r.raw_count,
                 "unique_votes": r.unique_count}
                for r in result.fetchall()]
    else:
        return []

@view_config(route_name="results", renderer="json", request_method="GET")
def get_result(request):
    poll_id = request.matchdict["id"]
    poll = DBSession.query(Poll).filter_by(_id=poll_id).first()
    if poll:
        return get_counts(poll.id)
    else:
        raise HTTPNotFoundError()<commit_msg>Added a minor comment about JSON list<commit_after>from pyramid.view import view_config
from sqlalchemy.sql import text

from ..models import (
    DBSession,
    Poll
    )

from .base import DefaultView
from .. import validation
from ..exceptions import HTTPNotFoundError
from ..validation import validator

import logging
LOG = logging.getLogger(__name__)

GET = "GET"

def get_counts(poll_id):
    params = {"poll_id": poll_id}
    query = text("""\
SELECT choice.id,
       choice.text,
       Count(DISTINCT response.ip_address) AS unique_count,
       Count(response.ip_address) AS raw_count
FROM choice
LEFT OUTER JOIN response ON response.choice_id = choice.id
WHERE choice.poll_id = :poll_id
GROUP BY choice.id;
""")
    result = DBSession.execute(query, params)
    if result:
        return [{"name": r.text,
                 "votes": r.raw_count,
                 "unique_votes": r.unique_count}
                for r in result.fetchall()]
    else:
        return []

@view_config(route_name="results", renderer="json", request_method="GET")
def get_result(request):
    poll_id = request.matchdict["id"]
    poll = DBSession.query(Poll).filter_by(_id=poll_id).first()
    #NOTE: Eh Personally I think returning a JSON list
    # isn't the best practice.
    if poll:
        return get_counts(poll.id)
    else:
        raise HTTPNotFoundError()
{'fraction_non_alphanumeric': 0.10383141762452107, 'fraction_numerical': 0.0, 'mean_word_length': 3.1313291139240507, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3707623', 'n_tokens_mistral': 809, 'n_tokens_neox': 800, 'n_words': 218}