Skip to content

Instantly share code, notes, and snippets.

@brydavis
Created August 3, 2019 08:54
Show Gist options
  • Save brydavis/7d06af0ca4ab0c400c84e5243e214d0f to your computer and use it in GitHub Desktop.
Save brydavis/7d06af0ca4ab0c400c84e5243e214d0f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"opening assignment/data/product.csv\n",
"{'product_id': 'prd001', 'description': '60-inch TV stand', 'product_type': 'livingroom', 'quantity_available': '3'}\n",
"{'product_id': 'prd002', 'description': 'L-shaped sofa', 'product_type': 'livingroom', 'quantity_available': '0'}\n",
"{'product_id': 'prd003', 'description': 'Acacia kitchen table', 'product_type': 'kitchen', 'quantity_available': '7'}\n",
"{'product_id': 'prd004', 'description': 'Queen bed', 'product_type': 'bedroom', 'quantity_available': '10'}\n",
"{'product_id': 'prd005', 'description': 'Reading lamp', 'product_type': 'bedroom', 'quantity_available': '20'}\n",
"{'product_id': 'prd006', 'description': 'Portable heater', 'product_type': 'bathroom', 'quantity_available': '14'}\n",
"{'product_id': 'prd007', 'description': 'Ballerina painting', 'product_type': 'livingroom', 'quantity_available': '0'}\n",
"{'product_id': 'prd008', 'description': 'Smart microwave', 'product_type': 'kitchen', 'quantity_available': '30'}\n",
"{'product_id': 'prd009', 'description': 'Popcorn machine', 'product_type': 'kitchen', 'quantity_available': '0'}\n",
"{'product_id': 'prd010', 'description': '60-inch TV', 'product_type': 'livingroom', 'quantity_available': '3'}\n",
"opening assignment/data/customers.csv\n",
"{'user_id': 'user001', 'name': 'Elisa Miles', 'address': '4490 Union Street', 'zip_code': '98109', 'phone_number': '206-922-0882', 'email': '[email protected]'}\n",
"{'user_id': 'user002', 'name': 'Maya Data', 'address': '4936 Elliot Avenue', 'zip_code': '98115', 'phone_number': '206-777-1927', 'email': '[email protected]'}\n",
"{'user_id': 'user003', 'name': 'Andy Norris', 'address': '348 Terra Street', 'zip_code': '98501', 'phone_number': '206-309-2533', 'email': '[email protected]'}\n",
"{'user_id': 'user004', 'name': 'Flor Matatena', 'address': '885 Boone Crockett Lane', 'zip_code': '97209', 'phone_number': '206-414-2629', 'email': '[email protected]'}\n",
"{'user_id': 'user005', 'name': 'Dan Sounders', 'address': '861 Honeysuckle Lane', 'zip_code': '98244', 'phone_number': '206-279-1723', 'email': '[email protected]'}\n",
"{'user_id': 'user006', 'name': 'Leo Dembele', 'address': '2725 Mutton Town Road', 'zip_code': '98368', 'phone_number': '206-203-1294', 'email': '[email protected]'}\n",
"{'user_id': 'user007', 'name': 'Pete Nicholas', 'address': '668 Elliot Avenue', 'zip_code': '98115', 'phone_number': '206-279-8759', 'email': '[email protected]'}\n",
"{'user_id': 'user008', 'name': 'Shirlene Harris', 'address': '4329 Honeysuckle Lane', 'zip_code': '98055', 'phone_number': '206-279-5340', 'email': '[email protected]'}\n",
"{'user_id': 'user009', 'name': 'Nick Rather', 'address': '4679 Goodwin Avenue', 'zip_code': '98619', 'phone_number': '206-777-1965', 'email': '[email protected]'}\n",
"{'user_id': 'user010', 'name': 'Jose Garza', 'address': '2717 Raccoon Run', 'zip_code': '98116', 'phone_number': '206-946-8200', 'email': '[email protected]'}\n",
"opening assignment/data/rental.csv\n",
"{'product_id': 'prd003', 'user_id': 'user004'}\n",
"{'product_id': 'prd002', 'user_id': 'user008'}\n",
"{'product_id': 'prd002', 'user_id': 'user005'}\n",
"{'product_id': 'prd005', 'user_id': 'user001'}\n",
"{'product_id': 'prd010', 'user_id': 'user002'}\n",
"{'product_id': 'prd007', 'user_id': 'user002'}\n",
"{'product_id': 'prd006', 'user_id': 'user003'}\n",
"{'product_id': 'prd005', 'user_id': 'user003'}\n",
"{'product_id': 'prd001', 'user_id': 'user010'}\n"
]
}
],
"source": [
"import csv\n",
"\n",
"from pymongo import MongoClient\n",
"\n",
"mongo = MongoClient(\"mongodb://localhost:27017\")\n",
"db = mongo[\"assignment\"]\n",
"\n",
"def import_data(data_dir, *files):\n",
" for filepath in files:\n",
" collection_name = filepath.split(\".\")[0]\n",
" \n",
" print(\"opening\", \"/\".join([data_dir, filepath]))\n",
" with open(\"/\".join([data_dir, filepath])) as file:\n",
" reader = csv.reader(file, delimiter=\",\")\n",
" \n",
" header = False\n",
" for row in reader:\n",
" if not header:\n",
" header = [h.strip(\"\\ufeff\") for h in row]\n",
" else:\n",
" data = {header[i]:v for i,v in enumerate(row)}\n",
" print(data)\n",
" cursor = db[collection_name]\n",
" cursor.insert_one(data)\n",
" \n",
"import_data(\"assignment/data\", \"product.csv\", \"customers.csv\", \"rental.csv\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'_id': ObjectId('5d454bac5cbed50c9fa5e3de'),\n",
" 'user_id': 'user001',\n",
" 'name': 'Elisa Miles',\n",
" 'address': '4490 Union Street',\n",
" 'zip_code': '98109',\n",
" 'phone_number': '206-922-0882',\n",
" 'email': '[email protected]'}]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cursor = db[\"customers\"]\n",
"list(cursor.find({\"email\":\"[email protected]\"}))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"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.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment