master
Raw Download raw file
  1{
  2 "metadata": {
  3  "name": ""
  4 },
  5 "nbformat": 3,
  6 "nbformat_minor": 0,
  7 "worksheets": [
  8  {
  9   "cells": [
 10    {
 11     "cell_type": "markdown",
 12     "metadata": {},
 13     "source": [
 14      "# ATARG\n",
 15      "\n",
 16      "### Config.py (example)"
 17     ]
 18    },
 19    {
 20     "cell_type": "code",
 21     "collapsed": false,
 22     "input": [
 23      "MYSQL_USER = \"admin\"\n",
 24      "MYSQL_PASS = \"docker\"\n",
 25      "MYSQL_ADDR = \"192.168.32.237\"\n",
 26      "MYSQL_DB   = \"test\"\n",
 27      "ALCHEMY_ECHO = False"
 28     ],
 29     "language": "python",
 30     "metadata": {},
 31     "outputs": [],
 32     "prompt_number": 15
 33    },
 34    {
 35     "cell_type": "heading",
 36     "level": 3,
 37     "metadata": {},
 38     "source": [
 39      "Test sqlalchemy connection"
 40     ]
 41    },
 42    {
 43     "cell_type": "code",
 44     "collapsed": false,
 45     "input": [
 46      "# requires sqlalchemy, mysql-python\n",
 47      "from sqlalchemy import create_engine\n",
 48      "from sqlalchemy.ext.declarative import declarative_base\n",
 49      "from sqlalchemy import Column, Integer, String, Enum, Date\n",
 50      "\n",
 51      "engine = create_engine('mysql://%s:%s@%s/%s' % (\n",
 52      "                       MYSQL_USER, \n",
 53      "                       MYSQL_PASS, \n",
 54      "                       MYSQL_ADDR,\n",
 55      "                       MYSQL_DB), echo=ALCHEMY_ECHO)\n",
 56      "Base = declarative_base()\n",
 57      "\n",
 58      "class Appointment(Base):\n",
 59      "  __tablename__ = 'appointments'\n",
 60      "  id         = Column(Integer, primary_key=True)\n",
 61      "  visitor    = Column(String(7))\n",
 62      "  submitter  = Column(String(7))\n",
 63      "  start_date = Column(Date) \n",
 64      "  end_date   = Column(Date)\n",
 65      "  location   = Column(Enum('upstairs', 'downstiars', name=\"loacation_type\"))\n",
 66      "  status     = Column(Enum('verifed', 'needs tickets', 'needs pnpi', name=\"status_type\"))\n",
 67      "\n",
 68      "  def __init__(self, visitor, submitter, start_date, end_date, location, status):\n",
 69      "    self.visitor = visitor\n",
 70      "    self.submitter = submitter\n",
 71      "    self.start_date = start_date\n",
 72      "    self.end_date = end_date\n",
 73      "    self.location = location\n",
 74      "    self.status = status\n",
 75      "\n",
 76      "Base.metadata.create_all(engine)"
 77     ],
 78     "language": "python",
 79     "metadata": {},
 80     "outputs": [],
 81     "prompt_number": 20
 82    },
 83    {
 84     "cell_type": "code",
 85     "collapsed": false,
 86     "input": [
 87      "from sqlalchemy.orm import sessionmaker\n",
 88      "Session = sessionmaker(bind=engine)\n",
 89      "session = Session()"
 90     ],
 91     "language": "python",
 92     "metadata": {},
 93     "outputs": [],
 94     "prompt_number": 21
 95    },
 96    {
 97     "cell_type": "code",
 98     "collapsed": false,
 99     "input": [
100      "test_appt = Appointment(visitor='vis', \n",
101      "             submitter='sub', \n",
102      "             start_date=0, \n",
103      "             end_date=0, \n",
104      "             location='upstairs',\n",
105      "             status='verified')"
106     ],
107     "language": "python",
108     "metadata": {},
109     "outputs": [],
110     "prompt_number": 22
111    },
112    {
113     "cell_type": "code",
114     "collapsed": false,
115     "input": [
116      "session.add(test_appt)"
117     ],
118     "language": "python",
119     "metadata": {},
120     "outputs": [],
121     "prompt_number": 27
122    },
123    {
124     "cell_type": "code",
125     "collapsed": false,
126     "input": [
127      "appt_test = session.query(Appointment).filter_by(location='upstairs')"
128     ],
129     "language": "python",
130     "metadata": {},
131     "outputs": [],
132     "prompt_number": 52
133    },
134    {
135     "cell_type": "code",
136     "collapsed": false,
137     "input": [
138      "for i in appt_test:\n",
139      "    print i.visitor"
140     ],
141     "language": "python",
142     "metadata": {},
143     "outputs": [
144      {
145       "output_type": "stream",
146       "stream": "stdout",
147       "text": [
148        "vis\n"
149       ]
150      }
151     ],
152     "prompt_number": 56
153    },
154    {
155     "cell_type": "code",
156     "collapsed": false,
157     "input": [
158      "result"
159     ],
160     "language": "python",
161     "metadata": {},
162     "outputs": [
163      {
164       "metadata": {},
165       "output_type": "pyout",
166       "prompt_number": 35,
167       "text": [
168        "<sqlalchemy.orm.query.Query at 0x10740f110>"
169       ]
170      }
171     ],
172     "prompt_number": 35
173    },
174    {
175     "cell_type": "code",
176     "collapsed": false,
177     "input": [],
178     "language": "python",
179     "metadata": {},
180     "outputs": []
181    }
182   ],
183   "metadata": {}
184  }
185 ]
186}