Added file reading section to Python and added animal data files.
[swc-testing-nose.git] / Python / MakeAnimals.ipynb
diff --git a/Python/MakeAnimals.ipynb b/Python/MakeAnimals.ipynb
new file mode 100644 (file)
index 0000000..143e083
--- /dev/null
@@ -0,0 +1,133 @@
+{
+ "metadata": {
+  "name": "MakeAnimals"
+ }, 
+ "nbformat": 2, 
+ "worksheets": [
+  {
+   "cells": [
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "animals = ('Moose', 'Elk', 'Fox', 'Owl', 'Wolf', 'Muskox', 'Ptarmigan', 'Grizzly', 'Wolverine')"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 14
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "import datetime as dt", 
+      "import random"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 15
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": false, 
+     "input": [
+      "def make_animals(filename, lines=100):", 
+      "    date = dt.date(2011, 4, 22)", 
+      "    one_day = dt.timedelta(days=1)", 
+      "", 
+      "    f = open(filename, 'w')", 
+      "", 
+      "    for _ in xrange(lines):", 
+      "        animal = random.choice(animals)", 
+      "        hour = random.randint(0, 23)", 
+      "        minute = random.randint(0, 59)", 
+      "        number = random.randint(1, 42)", 
+      "    ", 
+      "        date_str = '{}-{:02d}-{:02d}'.format(date.year, date.month, date.day)", 
+      "    ", 
+      "        time_str = '{:02d}:{:02d}'.format(hour, minute)", 
+      "    ", 
+      "        line = '{} {} {} {}\\n'.format(date_str, time_str, animal, number)", 
+      "    ", 
+      "        f.write(line)", 
+      "    ", 
+      "        if random.choice((True, False)):", 
+      "            date = date + one_day", 
+      "    ", 
+      "    f.close()"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 22
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "make_animals('animals.txt', 5)"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 24
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "make_animals('big_animals.txt')"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 23
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "make_animals('merida_animals.txt')"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 25
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "make_animals('fergus_animals.txt')"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 26
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "make_animals('macguffin_animals.txt')"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 27
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [
+      "make_animals('dingwall_animals.txt')"
+     ], 
+     "language": "python", 
+     "outputs": [], 
+     "prompt_number": 28
+    }, 
+    {
+     "cell_type": "code", 
+     "collapsed": true, 
+     "input": [], 
+     "language": "python", 
+     "outputs": []
+    }
+   ]
+  }
+ ]
+}
\ No newline at end of file