Skip to content

Instantly share code, notes, and snippets.

@wcmckee
Created December 14, 2018 13:01
Show Gist options
  • Select an option

  • Save wcmckee/33fe5c89d7c39ba5b80c6ed6006ec37d to your computer and use it in GitHub Desktop.

Select an option

Save wcmckee/33fe5c89d7c39ba5b80c6ed6006ec37d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Take Home Left\n",
"\n",
"controller for food leftovers. python functions."
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import sqlite3"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def createfoodb(namedb):\n",
" conn = sqlite3.connect('{}.db'.format(namedb))\n",
" c = conn.cursor()\n",
"\n",
" # Create table\n",
" c.execute('''CREATE TABLE foods\n",
" (name text, image text, datecook text, dateexpire text, \n",
" ingrediants text)''')\n",
"\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"def makefoodentry(name, image, datecooked, expiredate,\n",
" ingrediants):\n",
" conn = sqlite3.connect('foodentry.db')\n",
" c = conn.cursor()\n",
" c.execute(\"INSERT INTO foods VALUES ('{}','{}', '{}', '{}', '{}')\".format(name, \n",
" image, \n",
" datecooked, \n",
" expiredate, ingrediants))\n",
" \n",
" conn.commit()\n",
"\n",
" # We can also close the connection if we are done with it.\n",
" # Just be sure any changes have been committed or they will be lost.\n",
" conn.close()\n",
"\n",
" return({'name' : name, 'image' : image, 'datecooked' : datecooked,\n",
" 'expiredate' : expiredate, 'ingrediants' : ingrediants})"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def returnfoods():\n",
" conn = sqlite3.connect('foodentry.db')\n",
" c = conn.cursor()\n",
" allfod = c.execute('SELECT * FROM foods')\n",
" return(allfod.fetchall())\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def makefoodrequest(whatfood):\n",
" return('you requested {}'.format(whatfood))"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"createfoodb('hello')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"makefoodentry('pasta', 'me.com/img.png', '04/12/1999', '06/12/1999',\n",
" 'pork, pasta')"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[('pasta', 'me.com/img.png', '04-12-1988', '06-12-1988', 'pork, pasta')]"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"returnfoods()"
]
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment