Skip to content

Instantly share code, notes, and snippets.

@ScalaWilliam
Last active December 28, 2016 15:08
Show Gist options
  • Save ScalaWilliam/4fd1048afab1517ebfacaae43bf03f7b to your computer and use it in GitHub Desktop.
Save ScalaWilliam/4fd1048afab1517ebfacaae43bf03f7b to your computer and use it in GitHub Desktop.
PyTest TDD + Jython/Python (2.7)

Using TDD, we create a small package to count the unregistered clanplayers from all of our games.

$ pip install pytest-watch
$ ptw
$ /usr/local/Cellar/jython/2.7.0/libexec/bin/pytest # If running pytest via Jython

This code will work in both Python and Jython - and thus allow us to integrate directly in the ActionFPS code.

Once Jython is set up,

jython -m ensurepip
jython -m pip install pytest-watch
package game
import scala.collection.JavaConverters._
case class Game(teams: List[String], winner: Option[String]) { g =>
// todo turn this into a macro
final class JythonGame {
def getWinner(): String = g.winner.orNull
def getAsScala(): Game = g
def getTeams(): java.util.List[String] = teams.asJava
def __iter__(): org.python.core.PyObject = new org.python.core.PyIterator {
private val objIterator = winner.map(_ => "winner").toIterator ++ Iterator("teams", "asScala")
override def __iternext__(): org.python.core.PyObject = if ( objIterator.hasNext ) new org.python.core.PyString(objIterator.next) else null
}
}
def this(teams: java.util.List[String], winner: String) = this(teams.asScala.toList, Option(winner))
def getJython(): JythonGame = new JythonGame()
}
/**
scalac -cp /usr/local/Cellar/jython/2.7.0/libexec/jython.jar Game.scala
jython -J-cp .:/usr/local/Cellar/scala/2.12.0/libexec/lib/scala-library.jar
>>> import game
>>> game.Game(["A","B","C"], None)
Game(List(A, B, C),None)
>>> game.Game(["A","B","C"], None).jython
game.Game$JythonGame@55a8dc49
>>> "winner" in game.Game(["A","B","C"], None).jython
False
>>> "winner" in game.Game(["A","B","C"], "John").jython
True
>>> game.Game(["A","B","C"], "John").jython.winner
u'John'
>>> game.Game(["A","B","C"], "John").jython.teams
[A, B, C]
>>> game.Game(["A","B","C"], "John").jython.asScala
Game(List(A, B, C),Some(John))
So from Jython, we can refer to both JSON-loaded (for development) and Scala objects very easily.
**/
import os, json, urllib
from argparse import Namespace
from collections import Counter
if not os.path.exists('all.ndjson'):
urllib.urlretrieve('http://actionfps.com/all/', 'all.ndjson')
def load_json_object(json_string):
return json.loads(json_string, object_hook=lambda d: Namespace(**d))
def load_games():
with open('all.ndjson') as file:
for line in file:
yield load_json_object(line.split("\t")[1])
def get_unregistered_clan_players(game):
for team in game.teams:
for player in team.players:
if "user" not in player and "clan" in player:
yield player.name
def count_unregistered_clan_players(games):
return Counter([name for game in games for name in get_unregistered_clan_players(game) ])
import pytest
import mhm
def test_get_players_list(capsys):
with capsys.disabled():
print mhm.count_unregistered_clan_players(mhm.load_games()).most_common(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment