Updated after tests with real Enigma machine
authorNeil Smith <neil.git@njae.me.uk>
Sat, 4 Jun 2016 20:55:02 +0000 (21:55 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Sat, 4 Jun 2016 20:55:02 +0000 (21:55 +0100)
Untitled.ipynb [deleted file]
bombe.ipynb
enigma.ipynb
enigma.py
test_enigma.py

diff --git a/Untitled.ipynb b/Untitled.ipynb
deleted file mode 100644 (file)
index 0a8a3e0..0000000
+++ /dev/null
@@ -1,181 +0,0 @@
-{
- "cells": [
-  {
-   "cell_type": "code",
-   "execution_count": 1,
-   "metadata": {
-    "collapsed": true
-   },
-   "outputs": [],
-   "source": [
-    "from enigma import *"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 2,
-   "metadata": {
-    "collapsed": true
-   },
-   "outputs": [],
-   "source": [
-    "e31 = Enigma(reflector_b_spec, \n",
-    "                 wheel_i_spec, wheel_i_pegs,\n",
-    "                 wheel_v_spec, wheel_v_pegs,\n",
-    "                 wheel_iii_spec, wheel_iii_pegs,\n",
-    "                 6, 20, 24, \n",
-    "                    'ua pf rq so ni ey bg hl tx zj')\n"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 3,
-   "metadata": {
-    "collapsed": true
-   },
-   "outputs": [],
-   "source": [
-    "def advance(e, n):\n",
-    "    def print_state(e):\n",
-    "        print('        self.assertEqual(self.enigma31.wheel_positions, {})'.format(e.wheel_positions))\n",
-    "        print(\"        self.assertEqual(cat(self.enigma31.wheel_positions_l), '{}')\".format(cat(e.wheel_positions_l)))\n",
-    "        print('        self.assertEqual(self.enigma31.peg_positions, {})'.format(e.peg_positions))\n",
-    "        print()\n",
-    "              \n",
-    "        \n",
-    "    \n",
-    "    print_state(e)\n",
-    "    for i in range(n):\n",
-    "        print('        self.engima31.advance()')\n",
-    "        e.advance()\n",
-    "        print_state(e)\n"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 4,
-   "metadata": {
-    "collapsed": false
-   },
-   "outputs": [
-    {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "        self.assertEqual(self.enigma31.wheel_positions, (21, 5, 22))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'ayt')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([16], [1], [2]))\n",
-      "\n",
-      "        self.engima31.advance()\n",
-      "        self.assertEqual(self.enigma31.wheel_positions, (21, 5, 23))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'ayu')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([16], [1], [1]))\n",
-      "\n",
-      "        self.engima31.advance()\n",
-      "        self.assertEqual(self.enigma31.wheel_positions, (21, 5, 24))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'ayv')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([16], [1], [0]))\n",
-      "\n",
-      "        self.engima31.advance()\n",
-      "        self.assertEqual(self.enigma31.wheel_positions, (21, 6, 25))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'azw')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([16], [0], [25]))\n",
-      "\n",
-      "        self.engima31.advance()\n",
-      "        self.assertEqual(self.enigma31.wheel_positions, (22, 7, 0))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'bax')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([15], [25], [24]))\n",
-      "\n",
-      "        self.engima31.advance()\n",
-      "        self.assertEqual(self.enigma31.wheel_positions, (22, 7, 1))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'bay')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([15], [25], [23]))\n",
-      "\n"
-     ]
-    }
-   ],
-   "source": [
-    "e31.set_wheels('a', 'y', 't')\n",
-    "advance(e31, 5)"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": 5,
-   "metadata": {
-    "collapsed": false
-   },
-   "outputs": [
-    {
-     "name": "stdout",
-     "output_type": "stream",
-     "text": [
-      "        self.assertEqual(self.enigma31.wheel_positions, (21, 6, 21))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'azs')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([16], [0], [3]))\n",
-      "\n",
-      "        self.engima31.advance()\n",
-      "        self.assertEqual(self.enigma31.wheel_positions, (22, 7, 22))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'bat')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([15], [25], [2]))\n",
-      "\n",
-      "        self.engima31.advance()\n",
-      "        self.assertEqual(self.enigma31.wheel_positions, (22, 7, 23))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'bau')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([15], [25], [1]))\n",
-      "\n",
-      "        self.engima31.advance()\n",
-      "        self.assertEqual(self.enigma31.wheel_positions, (22, 7, 24))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'bav')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([15], [25], [0]))\n",
-      "\n",
-      "        self.engima31.advance()\n",
-      "        self.assertEqual(self.enigma31.wheel_positions, (22, 8, 25))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'bbw')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([15], [24], [25]))\n",
-      "\n",
-      "        self.engima31.advance()\n",
-      "        self.assertEqual(self.enigma31.wheel_positions, (22, 8, 0))\n",
-      "        self.assertEqual(cat(self.enigma31.wheel_positions_l), 'bbx')\n",
-      "        self.assertEqual(self.enigma31.peg_positions, ([15], [24], [24]))\n",
-      "\n"
-     ]
-    }
-   ],
-   "source": [
-    "e31.set_wheels('a', 'z', 's')\n",
-    "advance(e31, 5)"
-   ]
-  },
-  {
-   "cell_type": "code",
-   "execution_count": null,
-   "metadata": {
-    "collapsed": true
-   },
-   "outputs": [],
-   "source": []
-  }
- ],
- "metadata": {
-  "kernelspec": {
-   "display_name": "Python 3",
-   "language": "python",
-   "name": "python3"
-  },
-  "language_info": {
-   "codemirror_mode": {
-    "name": "ipython",
-    "version": 3
-   },
-   "file_extension": ".py",
-   "mimetype": "text/x-python",
-   "name": "python",
-   "nbconvert_exporter": "python",
-   "pygments_lexer": "ipython3",
-   "version": "3.5.1+"
-  }
- },
- "nbformat": 4,
- "nbformat_minor": 0
-}
index 99770da11e6c9b203fb3b309c4ea06707ae77b9d..59442e4f4da5bf6481be0f923171939245d2c938 100644 (file)
@@ -17,7 +17,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": 2,
    "metadata": {
     "collapsed": true
    },
@@ -30,7 +30,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 3,
    "metadata": {
     "collapsed": true
    },
@@ -75,7 +75,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 4,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 5,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 6,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": 7,
    "metadata": {
     "collapsed": false
    },
        "'opgndxcrwomnlnecjz'"
       ]
      },
-     "execution_count": 8,
+     "execution_count": 7,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 9,
+   "execution_count": 8,
    "metadata": {
     "collapsed": false
    },
        "'aas'"
       ]
      },
-     "execution_count": 9,
+     "execution_count": 8,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 10,
+   "execution_count": 9,
    "metadata": {
     "collapsed": false
    },
        " MenuIem(before='e', after='z', number=18)]"
       ]
      },
-     "execution_count": 10,
+     "execution_count": 9,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 11,
+   "execution_count": 10,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 12,
+   "execution_count": 11,
    "metadata": {
     "collapsed": false
    },
        " MenuIem(before='e', after='z', number=18)]"
       ]
      },
-     "execution_count": 12,
+     "execution_count": 11,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 13,
+   "execution_count": 12,
    "metadata": {
     "collapsed": false
    },
        "'s'"
       ]
      },
-     "execution_count": 13,
+     "execution_count": 12,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 14,
+   "execution_count": 13,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 15,
+   "execution_count": 14,
    "metadata": {
     "collapsed": false
    },
        "18"
       ]
      },
-     "execution_count": 15,
+     "execution_count": 14,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 16,
+   "execution_count": 15,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 17,
+   "execution_count": 16,
    "metadata": {
     "collapsed": false
    },
        "False"
       ]
      },
-     "execution_count": 17,
+     "execution_count": 16,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 18,
+   "execution_count": 17,
    "metadata": {
     "collapsed": false
    },
        " 'z': True}"
       ]
      },
-     "execution_count": 18,
+     "execution_count": 17,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 19,
+   "execution_count": 18,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 20,
+   "execution_count": 19,
    "metadata": {
     "collapsed": false
    },
        "('a', 'a', 'a')"
       ]
      },
-     "execution_count": 20,
+     "execution_count": 19,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 21,
+   "execution_count": 20,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 22,
+   "execution_count": 21,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 23,
+   "execution_count": 22,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 24,
+   "execution_count": 23,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 25,
+   "execution_count": 24,
    "metadata": {
     "collapsed": false
    },
        "1"
       ]
      },
-     "execution_count": 25,
+     "execution_count": 24,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 26,
+   "execution_count": 25,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 27,
+   "execution_count": 26,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 28,
+   "execution_count": 27,
    "metadata": {
     "collapsed": false
    },
        "('a', 'a', 'b')"
       ]
      },
-     "execution_count": 28,
+     "execution_count": 27,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 29,
+   "execution_count": 28,
    "metadata": {
     "collapsed": false
    },
        "False"
       ]
      },
-     "execution_count": 29,
+     "execution_count": 28,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 30,
+   "execution_count": 29,
    "metadata": {
     "collapsed": false
    },
        "('p', 'p', 'p')"
       ]
      },
-     "execution_count": 30,
+     "execution_count": 29,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 31,
+   "execution_count": 30,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 32,
+   "execution_count": 31,
    "metadata": {
     "collapsed": false
    },
        "17576"
       ]
      },
-     "execution_count": 32,
+     "execution_count": 31,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 33,
+   "execution_count": 32,
    "metadata": {
     "collapsed": false
    },
        "(('a', 'a', 'b'), True)"
       ]
      },
-     "execution_count": 33,
+     "execution_count": 32,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 34,
+   "execution_count": 33,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 35,
+   "execution_count": 34,
    "metadata": {
     "collapsed": false
    },
        "[('a', 'a', 'b')]"
       ]
      },
-     "execution_count": 35,
+     "execution_count": 34,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 36,
+   "execution_count": 35,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 37,
+   "execution_count": 36,
    "metadata": {
     "collapsed": false
    },
        "[('a', 'a', 'b')]"
       ]
      },
-     "execution_count": 37,
+     "execution_count": 36,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 38,
+   "execution_count": 37,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 39,
+   "execution_count": 38,
    "metadata": {
     "collapsed": false
    },
        "('e', 'l', 'e')"
       ]
      },
-     "execution_count": 39,
+     "execution_count": 38,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 40,
+   "execution_count": 39,
    "metadata": {
     "collapsed": false
    },
        "'dhnpforeeimgg'"
       ]
      },
-     "execution_count": 40,
+     "execution_count": 39,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 41,
+   "execution_count": 40,
    "metadata": {
     "collapsed": false
    },
        "('j', 'e', 'o')"
       ]
      },
-     "execution_count": 41,
+     "execution_count": 40,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 42,
+   "execution_count": 41,
    "metadata": {
     "collapsed": false
    },
        " MenuIem(before='t', after='g', number=13)]"
       ]
      },
-     "execution_count": 42,
+     "execution_count": 41,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 43,
+   "execution_count": 42,
    "metadata": {
     "collapsed": false,
     "scrolled": true
        " ('z', 'z', 'k')]"
       ]
      },
-     "execution_count": 43,
+     "execution_count": 42,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 44,
+   "execution_count": 43,
    "metadata": {
     "collapsed": false
    },
        "62"
       ]
      },
-     "execution_count": 44,
+     "execution_count": 43,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 45,
+   "execution_count": 44,
    "metadata": {
     "collapsed": false,
     "scrolled": true
        " ('y', 'n', 'c')]"
       ]
      },
-     "execution_count": 45,
+     "execution_count": 44,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 46,
+   "execution_count": 45,
    "metadata": {
     "collapsed": false
    },
        " ('y', 'n', 'c')]"
       ]
      },
-     "execution_count": 46,
+     "execution_count": 45,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 47,
+   "execution_count": 46,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 48,
+   "execution_count": 47,
    "metadata": {
     "collapsed": false
    },
        "13"
       ]
      },
-     "execution_count": 48,
+     "execution_count": 47,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 49,
+   "execution_count": 48,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 50,
+   "execution_count": 49,
    "metadata": {
     "collapsed": false
    },
        "Signal(bank='e', wire='e')"
       ]
      },
-     "execution_count": 50,
+     "execution_count": 49,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 51,
+   "execution_count": 50,
    "metadata": {
     "collapsed": false
    },
        "True"
       ]
      },
-     "execution_count": 51,
+     "execution_count": 50,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 52,
+   "execution_count": 51,
    "metadata": {
     "collapsed": false
    },
        "True"
       ]
      },
-     "execution_count": 52,
+     "execution_count": 51,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 53,
+   "execution_count": 52,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 54,
+   "execution_count": 53,
    "metadata": {
     "collapsed": false
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 55,
+   "execution_count": 54,
    "metadata": {
     "collapsed": false
    },
     {
      "data": {
       "text/plain": [
-       "{frozenset({'b', 'g'}),\n",
-       " frozenset({'e', 'y'}),\n",
-       " frozenset({'t', 'x'}),\n",
-       " frozenset({'m'}),\n",
+       "{frozenset({'m'}),\n",
        " frozenset({'i', 'n'}),\n",
-       " frozenset({'f', 'p'})}"
+       " frozenset({'f', 'p'}),\n",
+       " frozenset({'t', 'x'}),\n",
+       " frozenset({'e', 'y'}),\n",
+       " frozenset({'b', 'g'})}"
       ]
      },
-     "execution_count": 55,
+     "execution_count": 54,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 56,
+   "execution_count": 55,
    "metadata": {
     "collapsed": false
    },
        "True"
       ]
      },
-     "execution_count": 56,
+     "execution_count": 55,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 57,
+   "execution_count": 56,
    "metadata": {
     "collapsed": false
    },
     {
      "data": {
       "text/plain": [
-       "({frozenset({1, 2}), frozenset({2, 3}), frozenset({3, 4})},\n",
+       "({frozenset({1, 2}), frozenset({3, 4}), frozenset({2, 3})},\n",
        " frozenset({1, 2}),\n",
        " frozenset({3, 4}),\n",
        " frozenset({2, 3}))"
       ]
      },
-     "execution_count": 57,
+     "execution_count": 56,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 58,
+   "execution_count": 57,
    "metadata": {
     "collapsed": false
    },
        "False"
       ]
      },
-     "execution_count": 58,
+     "execution_count": 57,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 59,
+   "execution_count": 58,
    "metadata": {
     "collapsed": false
    },
        "False"
       ]
      },
-     "execution_count": 59,
+     "execution_count": 58,
      "metadata": {},
      "output_type": "execute_result"
     }
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.5.1+"
+   "version": "3.4.3+"
   }
  },
  "nbformat": 4,
index 086aa468ce72215d66d3cde54eb25b97f82caf5b..06f684a426cccb6ec23ed46fe3ee2651ab03a755 100644 (file)
    "name": "python",
    "nbconvert_exporter": "python",
    "pygments_lexer": "ipython3",
-   "version": "3.5.1+"
+   "version": "3.4.3+"
   }
  },
  "nbformat": 4,
index 8c72f22bf558887842061541b64bfbb7918139f2..a0056e4aae61d34725d63ee82846d29f455859f2 100644 (file)
--- a/enigma.py
+++ b/enigma.py
@@ -182,7 +182,11 @@ class SimpleWheel(LetterTransformer):
             return object.__getattribute__(self, name)
     
     def set_position(self, position):
-        self.position = ord(position) - ord('a')
+        if isinstance(position, str):
+            # self.position = ord(position) - ord('a')
+            self.position = pos(position)
+        else:
+            self.position = position
     
     def forward(self, letter):
         if letter in string.ascii_lowercase:
@@ -234,8 +238,12 @@ class Wheel(SimpleWheel):
             return object.__getattribute__(self, name)
 
     def set_position(self, position):
-        self.position = (pos(position) - self.ring_setting + 1) % 26
-        self.peg_positions = [(pos(p) - pos(position)) % 26  for p in self.ring_peg_letters]
+        if isinstance(position, str):
+            self.position = (pos(position) - self.ring_setting + 1) % 26
+        else:
+            self.position = (position - self.ring_setting) % 26
+        # self.peg_positions = [(pos(p) - pos(position)) % 26  for p in self.ring_peg_letters]
+        self.peg_positions = [(pos(p) - (self.position + self.ring_setting - 1)) % 26  for p in self.ring_peg_letters]
         
     def advance(self):
         super(Wheel, self).advance()
index 830bb2b1fdda74d4a415396edff434c2afc97052..9b59edb392d25fd5daebed18c9964fd34e549f7e 100644 (file)
@@ -93,6 +93,29 @@ class SimpleWheelTest(unittest.TestCase):
                 for l in string.ascii_lowercase),
             'ajpczwrlfbdkotyuqgenhxmivs')
 
+    def test_set_position(self):
+        wheel_3 = SimpleWheel(wheel_iii_spec)
+        wheel_3.set_position('a')
+        self.assertEqual(wheel_3.position, 0)
+        self.assertEqual(wheel_3.position_l, 'a')
+
+        wheel_3.set_position('f')
+        self.assertEqual(wheel_3.position, 5)
+        self.assertEqual(wheel_3.position_l, 'f')
+
+        wheel_3.advance()
+        self.assertEqual(wheel_3.position, 6)
+        self.assertEqual(wheel_3.position_l, 'g')
+
+        wheel_3.set_position(12)
+        self.assertEqual(wheel_3.position, 12)
+        self.assertEqual(wheel_3.position_l, 'm')
+
+        wheel_3.advance()
+        self.assertEqual(wheel_3.position, 13)
+        self.assertEqual(wheel_3.position_l, 'n')
+
+
     def test_advance(self):
         wheel_3 = SimpleWheel(wheel_iii_spec)
         wheel_3.set_position('a')
@@ -131,7 +154,7 @@ class SimpleWheelTest(unittest.TestCase):
             'tagbpcsdqeufvnzhyixjwlrkom')
 
 
-class SimpleWheelTest(unittest.TestCase):
+class WheelTest(unittest.TestCase):
     def test_init1(self):
         wheel = Wheel(wheel_iii_spec, wheel_iii_pegs, position='b', 
             ring_setting=1)
@@ -158,7 +181,6 @@ class SimpleWheelTest(unittest.TestCase):
                 for l in string.ascii_lowercase),
             'ptlyrmidoxbswhnfckquzgeavj')
 
-
     def test_advance(self):
         wheel = Wheel(wheel_vi_spec, wheel_vi_pegs, position='b', 
             ring_setting=3)
@@ -243,7 +265,6 @@ class SimpleWheelTest(unittest.TestCase):
                 for l in string.ascii_lowercase),
             'ptlyrmidoxbswhnfckquzgeavj')
 
-
     def test_advance_27(self):
         wheel = Wheel(wheel_vi_spec, wheel_vi_pegs, position='b', 
             ring_setting=3)
@@ -261,6 +282,54 @@ class SimpleWheelTest(unittest.TestCase):
                 for l in string.ascii_lowercase),
             'skxqlhcnwarvgmebjptyfdzuio')
 
+    def test_set_position(self):
+        wheel_3 = Wheel(wheel_iii_spec, wheel_iii_pegs, ring_setting=3)
+        wheel_3.set_position('a')
+        self.assertEqual(wheel_3.position, 24)
+        self.assertEqual(wheel_3.position_l, 'a')
+        self.assertEqual(wheel_3.peg_positions, [21])
+
+        wheel_3.set_position('z')
+        self.assertEqual(wheel_3.position, 23)
+        self.assertEqual(wheel_3.position_l, 'z')
+        self.assertEqual(wheel_3.peg_positions, [22])
+
+        wheel_3.set_position(26)
+        self.assertEqual(wheel_3.position, 23)
+        self.assertEqual(wheel_3.position_l, 'z')
+        self.assertEqual(wheel_3.peg_positions, [22])
+
+        wheel_3.set_position(27)
+        self.assertEqual(wheel_3.position, 24)
+        self.assertEqual(wheel_3.position_l, 'a')
+        self.assertEqual(wheel_3.peg_positions, [21])
+
+        wheel_3.set_position('f')
+        self.assertEqual(wheel_3.position, 3)
+        self.assertEqual(wheel_3.position_l, 'f')
+        self.assertEqual(wheel_3.peg_positions, [16])
+
+        wheel_3.set_position(6)
+        self.assertEqual(wheel_3.position, 3)
+        self.assertEqual(wheel_3.position_l, 'f')
+        self.assertEqual(wheel_3.peg_positions, [16])
+
+        wheel_3.advance()
+        self.assertEqual(wheel_3.position, 4)
+        self.assertEqual(wheel_3.position_l, 'g')
+        self.assertEqual(wheel_3.peg_positions, [15])
+
+        wheel_3.set_position(12)
+        self.assertEqual(wheel_3.position, 9)
+        self.assertEqual(wheel_3.position_l, 'l')
+        self.assertEqual(wheel_3.peg_positions, [10])
+
+        wheel_3.advance()
+        self.assertEqual(wheel_3.position, 10)
+        self.assertEqual(wheel_3.position_l, 'm')
+        self.assertEqual(wheel_3.peg_positions, [9])
+
+
 class EnigmaTest(unittest.TestCase):
 
     def setUp(self):
@@ -281,6 +350,14 @@ class EnigmaTest(unittest.TestCase):
                 6, 20, 24, 
                 'ua pf rq so ni ey bg hl tx zj')
 
+        # Settings for Bletchley Park outreach department's Enigma
+        self.enigma_bp = Enigma(reflector_b_spec, 
+                 wheel_i_spec, wheel_i_pegs,
+                 wheel_iii_spec, wheel_iii_pegs,
+                 wheel_ii_spec, wheel_ii_pegs,
+                 1, 26, 26, 
+                 'qm we ro tu zj ps dl fg')
+
 
     def test_middle_advance(self):
         self.enigma.set_wheels('a', 'a', 't')