-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.py
42 lines (32 loc) · 950 Bytes
/
Tools.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from functools import wraps
import psycopg2
def psycopg2_cursor(conn_info):
"""Wrap function to set up and tear down a Postgres connection while
providing a cursor object to make queries with.
"""
def wrap(f):
@wraps(f)
def wrapper(*args, **kwargs):
try:
connection = psycopg2.connect(**conn_info)
cursor = connection.cursor()
return_val = f(cursor, *args, **kwargs)
finally:
connection.commit()
connection.close()
return return_val
return wrapper
return wrap
PSQL_CONN = {
'host': '127.0.0.1',
'port': '5432',
'user': 'postgres',
'password': '2659',
'dbname': 'postgres'
}
# @psycopg2_cursor(PSQL_CONN)
# def tester(cursor):
# """Test function that uses our psycopg2 decorator
# """
# cursor.execute('SELECT 1 + 1')
# return cursor.fetchall()