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

Concise STV #6

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open

Concise STV #6

wants to merge 4 commits into from

Conversation

user2589
Copy link

I would like you guys to review this implementation of STV. I will be happy to give you a code walkthrough in a personal or an online meeting.

Why Python

  • existing elections platform is written in Rails, which is a pretty much dead platform. Ruby itself lacks a lot of capabilities that would be handful to have, such as good data processing libraries.
  • Every year we struggle to find a person who could support this platform, and at least for two consequent years we weren't very successful at this.
  • Existing platform has very limited administration capabilities, and Rails barely can help with this. A lot of times administration is done by directly changing values in MySQL console.
  • I an demonstrate that Rails community has negative dynamics (this is a topic of my research), so we can expect support problems in the future.

The proposed solution is to gradually port elections platform to Python, namely Django. There are much more people familiar with Python on campus, and it offers decent administration console out of the box.

As a first step, I am going to port create a baseline project, which will not be exposed outside but will allow us to perform some administrative tasks, such as creating/editing elections and calculating election results. Since fair implementation of STV was a big concern in Senate last year (which I totally understand), I would like you to review my implementation.

Why not existing implementation

code in electionspy is overly verbose and is not very Pythonic. It doesn't follow many conventions, excessively uses classes, misses logging, and can be best described by a God-method pattern (Elections.compute_results is 240LOC + service classes and methods). After giving a try to make it up to standard, I found it will take less time to restart.

Testing and debugging

All existing tests are included as doctests. Run: python -m doctest stv.py

Concise debugging output is generated if logging is configured at INFO level. DEBUG produces even more verbose output. Simply config logging before calling stv: logging.basicConfig(level=logging.INFO)

@@ -21,50 +21,20 @@ class Candidate:
"""

def __init__(self, uid, name=None):
"""Initializes Candidate with name and uid.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

standard methods don't really need docstrings

@@ -11,7 +11,7 @@
__status__ = "Production"


class Candidate:
class Candidate(object):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

old style classes might cause issues on some platforms

return 'VoteTracker(votes_for_candidate={!r}, votes_cast={!r})'.format(
self._votes_for_candidate, self.votes_cast)

def decription(self):
"""Returns a printable long-form user representation of the VoteTracker.

Returns:
String containing the printable representation of the VoteTracker.
:return String containing the printable representation of the
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a standard for autodocumentation:
https://thomas-cokelaer.info/tutorials/sphinx/docstring_python.html

@@ -863,7 +851,7 @@ def test_cgp_grey_animal_kingdom(self):
tiger = Candidate('tiger', name='Tiger')
lynx = Candidate('lynx', name='Lynx')

expected_winners = set([gorilla, monkey, tiger])
expected_winners = {gorilla, monkey, tiger}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests.py Outdated
candidates.append(candidate_for_id[candidate_id])
return candidates

return [Candidate(CANDIDATE_IDS[candidate_id])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant