some code to get started

This commit is contained in:
Ward Wouts 2018-03-23 12:30:43 +01:00
parent b0afc0f3e6
commit 7e68ef0f17
4 changed files with 162 additions and 0 deletions

0
lib/__init__.py Normal file
View file

90
lib/causal.py Normal file
View file

@ -0,0 +1,90 @@
import sys
if sys.version_info[0] < 3:
raise "Must be using Python 3"
class Causal:
def __init__(self, causalcode=""):
self.causalcode = causalcode
self._description = ""
self._title = ""
self._prechac = ""
self._sequence = ""
self._fourhanded = ""
# https://docs.python.org/3/library/functions.html#property
@property
def description(self):
return self._description
@description.setter
def description(self, value):
self._description = value
@property
def title(self):
return self._title
@title.setter
def title(self, value):
self._title = value
@property
def prechac(self):
return self._prechac
@prechac.setter
def prechac(self, value):
self._prechac = value
@property
def sequence(self):
return self._sequence
@sequence.setter
def sequence(self, value):
self._sequence = value
@property
def fourhanded(self):
return self._fourhanded
@fourhanded.setter
def fourhanded(self, value):
if self.isvalid_fourhanded(value):
self._fourhanded = str(value)
else:
raise Exception("Can't parse as fourhanded siteswap")
# convert to prechac and fill prechac value
def isvalid_fourhanded(self, value):
if type(value) == int:
value = str(value)
elif type(value) == str:
if value.isdigit():
next
else:
return False
else:
return False
# inspired by https://icculus.org/jugglemaster/docs/validate.txt
lst = [1] * len(value)
for i, c in enumerate(value):
j = ( i + int(c) ) % len(value)
lst[j] = lst[j] - 1
for i in lst:
if i != 0:
return False
return True
def fourhanded_to_local(self, value):
# local notation from https://jonglieren-jena.de/ppa/anthology.pdf
# This is the local siteswap of the pattern accompanying a global siteswap. Subscripts
# indicate the starting positions of the different jugglers. So than in
# A7B65 the juggler A starts with a 7 while B starts with a 6
if not self.isvalid_fourhanded(value):
raise Exception("Can't parse as fourhanded siteswap")
return

0
test/__init__.py Normal file
View file

72
test/test_causal.py Normal file
View file

@ -0,0 +1,72 @@
import unittest
import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
if sys.version_info[0] < 3:
raise "Must be using Python 3"
from lib.causal import Causal
# The main methods that we make use of in unit testing for Python are:
#
# assert: base assert allowing you to write your own assertions
# assertEqual(a, b): check a and b are equal
# assertNotEqual(a, b): check a and b are not equal
# assertIn(a, b): check that a is in the item b
# assertNotIn(a, b): check that a is not in the item b
# assertFalse(a): check that the value of a is False
# assertTrue(a): check the value of a is True
# assertIsInstance(a, TYPE): check that a is of type "TYPE"
# assertRaises(ERROR, a, args): check that when a is called with args that it raises ERROR
#
# There are more methods available to us, which you can view - see
# https://docs.python.org/2/library/unittest.html
class TestCausal(unittest.TestCase):
def setUp(self):
self.causal = Causal()
def test_init(self):
causal = Causal()
self.assertIsInstance(causal, Causal)
def test_description_properties(self):
self.causal.description = "blaat"
self.assertEqual(self.causal.description, "blaat")
def test_title_properties(self):
self.causal.title = "blaat"
self.assertEqual(self.causal.title, "blaat")
def test_prechac_properties(self):
self.causal.prechac = "blaat"
self.assertEqual(self.causal.prechac, "blaat")
def test_sequence_properties(self):
self.causal.sequence = "blaat"
self.assertEqual(self.causal.sequence, "blaat")
def test_fourhanded_properties(self):
self.causal.fourhanded = "7746666"
self.assertEqual(self.causal.fourhanded, "7746666")
self.assertRaises(Exception, self.causal.fourhanded, "blaat")
self.assertRaises(Exception, self.causal.fourhanded, 8.5)
self.assertRaises(Exception, self.causal.fourhanded, "7746666")
def test_isvalid_fourhanded(self):
self.assertEqual(self.causal.isvalid_fourhanded(7746666), True) # Jim's three count
self.assertEqual(self.causal.isvalid_fourhanded("7746666"), True) # Jim's three count
self.assertEqual(self.causal.isvalid_fourhanded("8746666"), False)
self.assertEqual(self.causal.isvalid_fourhanded("blaat"), False)
self.assertEqual(self.causal.isvalid_fourhanded(8.5), False)
def test_fourhanded_to_local(self):
self.assertRaises(Exception, self.causal.fourhanded_to_local, "blaat")
self.assertEqual(self.causal.fourhanded_to_local(7746666), "[A]7466[B]766") # Jim's three count
self.assertEqual(self.causal.fourhanded_to_local(772626), "[A]722/[B]766") # "Skip" 5c pzz/pss
self.assertEqual(self.causal.fourhanded_to_local(777726), "[A]772/[B]776") # "Jonix" 6c ppz/pps
if __name__ == '__main__':
unittest.main()