This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import galtonboard | |
import galtonboardview | |
import sys | |
import os | |
def main(): | |
""" | |
Creates and runs a GaltonBoard index object |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import psycopg2 | |
def get_connection(dbname): | |
connect_str = "dbname={} host='localhost' user='user' password='password'".format(dbname) | |
return psycopg2.connect(connect_str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT | |
import psycopg2 | |
import pgconnection | |
def main(): | |
""" | |
Demonstrate creation of PostgreSQL database, tables and views using psycopg2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import psycopg2 | |
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT | |
import pgconnection | |
def main(): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insert_galleries(): | |
galleries = ({"name": "London 2018", "description": "Photos of London in 2018"}, | |
{"name": "Paris 2016", "description": "Photos of Paris in 2016"}, | |
{"name": "Oslo 2018", "description": "Photos of Oslo in 2018"}, | |
{"name": "Copenhagen 2017", "description": "Photos of Copenhagen in 2017"}, | |
{"name": "Edinburgh 2015", "description": "Photos of Edinburgh in 2015"}) | |
try: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def update_photos(): | |
update_dict = {"description": "Edinburgh Photo 2", "photoid": 10} | |
try: | |
conn = pgconnection.get_connection("codeinpython") | |
cursor = conn.cursor() | |
cursor.execute("UPDATE photos SET description = %(description)s WHERE photoid = %(photoid)s;", update_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def delete_photos(): | |
delete_dict = {"photoid": 10} | |
try: | |
conn = pgconnection.get_connection("codeinpython") | |
cursor = conn.cursor() | |
cursor.execute("DELETE FROM photos WHERE photoid = %(photoid)s;", delete_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def select_photos(): | |
# Wildcards: | |
# percent sign % for 0 or more characters | |
# underscore _ for exactly 1 character | |
select_dict = {"where_like": '%Oslo%'} | |
try: | |
conn = pgconnection.get_connection("codeinpython") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insert_photo_invalid_fk(): | |
invalid_photo = {"galleryid": 6, "title": "Hong Kong Photo 1", "description": "Hong Kong Photo 1", "photographer": "Chris Webb", "datetaken": datetime.date(2018, 8, 27)} | |
try: | |
conn = pgconnection.get_connection("codeinpython") | |
cursor = conn.cursor() | |
cursor.execute("""INSERT INTO photos(galleryid, title, description, photographer, datetaken) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def reset_serial(): | |
try: | |
conn = pgconnection.get_connection("codeinpython") | |
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) | |
cursor = conn.cursor() | |
cursor.execute("ALTER SEQUENCE photos_photoid_seq RESTART WITH 1") |