Skip to content

Commit

Permalink
030
Browse files Browse the repository at this point in the history
  • Loading branch information
pybites committed Apr 28, 2017
1 parent dbc9e32 commit 20661a2
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 1 deletion.
1 change: 1 addition & 0 deletions 030/.gitignore
@@ -0,0 +1 @@
movies.sqlite
93 changes: 93 additions & 0 deletions 030/movies.py
@@ -0,0 +1,93 @@
import csv
from contextlib import contextmanager
import os
from pprint import pprint as pp
import sqlite3
import sys

# Useful:
# A thorough guide to SQLite database operations in Python
# http://sebastianraschka.com/Articles/2014_sqlite_in_python_tutorial.html

DB = 'movies.sqlite'
CSV_FILE = 'data.csv'

# thanks Julian
# https://github.com/pybites/100DaysOfCode/blob/master/025/gen_testdb.py


@contextmanager
def conn_db():
try:
conn = sqlite3.connect(DB)
conn.row_factory = sqlite3.Row # want dict rows, thanks Row
cursor = conn.cursor()
yield cursor
finally:
conn.commit()
conn.close()


def clean_name(input_var):
# from rasbt article
return ''.join(char for char in input_var if char.isalnum())


def read_csv(cf=CSV_FILE):
with open(cf, 'r') as csvfile:
return list(csv.DictReader(csvfile))


def create_db(table, col_names, overwrite=False):
if overwrite:
print('Recreating DB')
if os.path.isfile(DB):
os.remove(DB)

idx = 'id INTEGER PRIMARY KEY AUTOINCREMENT,'

with conn_db() as c:
try:
c.execute('CREATE TABLE {} ({} {})'.format(
table, idx, ', '.join(col_names)))
except sqlite3.OperationalError:
print('Table already exists')


def insert_movies(data):
cols = ', '.join(data[0].keys())
placeholders = ', '.join(['?'] * len(data[0]))

with conn_db() as c:
rows = [list(d.values()) for d in data]

c.executemany('INSERT INTO movies ({}) VALUES ({})'.format(
cols, placeholders), rows)


def get_movies():
with conn_db() as c:
c.execute("SELECT * FROM movies;")
for row in c.fetchall():
yield dict(row)


def get_movie_info(idx):
idx = clean_name(str(idx))
with conn_db() as c:
c.execute("SELECT * FROM movies WHERE id LIKE ?", (idx, ))
return dict(c.fetchone())


if __name__ == '__main__':
overwrite = '-r' in sys.argv[1:]

data = read_csv()
col_names = data[0].keys()

create_db('movies', col_names, overwrite)

insert_movies(data)

movie = get_movie_info(111)
pp(movie)
2 changes: 1 addition & 1 deletion LOG.md
Expand Up @@ -31,7 +31,7 @@
| 027 | Apr 25, 2017 | [rough script to query the #warcraft #API for a character's mounts](027) | A VERY rough and simple script to query the World of Warcraft API and pull the collected mounts of a specific character. Lots of room to expand this with stripping of JSON data and specifying which data to pull. Just some fun with a new and unconmmon API! |
| 028 | Apr 26, 2017 | [Jupyter notebook to plot and list new #Python titles on @safari by month](028) | Learning: parsing a Twitter CSV backup dump (@newsafaribooks account), matplotlib, collections, hacking iPython css |
| 029 | Apr 27, 2017 | [Traffic Lights script to demo #itertools cycle](029) | A nice and easy script to simulate traffic lights using `itertools.cycle` and other stdlib modules cycle. Itertools rocks! |
| 030 | Apr 28, 2017 | [TITLE](030) | LEARNING |
| 030 | Apr 28, 2017 | [Script to import movie csv file into an sqlite database](030) | sqlite3, csv, nice groundwork for Flask auto-complete I am working on |
| 031 | Apr 29, 2017 | [TITLE](031) | LEARNING |
| 032 | Apr 30, 2017 | [TITLE](032) | LEARNING |
| 033 | May 01, 2017 | [TITLE](033) | LEARNING |
Expand Down

0 comments on commit 20661a2

Please sign in to comment.