Skip to content

Commit

Permalink
Removing unnecessary closing code
Browse files Browse the repository at this point in the history
  • Loading branch information
tavinathanson committed Nov 24, 2014
1 parent 6e5c531 commit 5b2ed1c
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 117 deletions.
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,4 @@ voluptuous==0.8.5
wsgiref==0.1.2
dpxdt==0.1.5
psycopg2==2.5.4
testing.postgresql
git+git://github.com/hammerlab/pyensembl
92 changes: 0 additions & 92 deletions tests/python/test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import nose
from nose.tools import eq_, ok_
from sqlalchemy import create_engine
import testing.postgresql as testpg
from unittest import TestCase

from cycledash.helpers import underscorize
from workers.gene_annotator import closing
from workers.relational_vcfs import vcf_to_csv


Expand Down Expand Up @@ -34,90 +29,3 @@ def test_vcf_to_csv():
assert rows[0] == ['44', '', '81', 'True']
assert rows[-1] == ['40', '', '74', 'True']
assert len(rows) == 20


def test_closing_file():
file_name = 'tests/js/data/snv.vcf'

# Without the closing function
file = open(file_name)
_ok_open(file)
with file:
pass
_ok_closed(file)

# Using the closing function
file = open(file_name)
_ok_open(file)
with closing(file):
pass
_ok_closed(file)


def test_closing_pre_finally():
file = open('tests/js/data/snv.vcf')

def add_to_dict(d):
d[0] = 1

d = {}
ok_(not d, "d should be empty")
with closing(file, pre_finally_fun=add_to_dict, d=d):
pass
eq_(len(d), 1, "d should have one element")
_ok_closed(file)


class TestClosingDB(TestCase):
@classmethod
def setUpClass(cls):
# TODO(tavi) Make this faster. Perhaps mock it out?
cls.pg = testpg.Postgresql()
cls.engine = create_engine(cls.pg.url())

@classmethod
def tearDownClass(cls):
cls.pg.stop()

def setUp(self):
with testpg.Postgresql() as pg:
self.connection = self.engine.connect()
self.cursor = self.connection.connection.cursor()

def tearDown(self):
if self.cursor:
self.cursor.close()
if self.connection:
self.connection.close()

def test_connection_without_closing(self):
_ok_open(self.connection)
with self.connection:
pass
_ok_closed(self.connection)

def test_connection_with_closing(self):
_ok_open(self.connection)
with closing(self.connection):
pass
_ok_closed(self.connection)

def test_cursor_without_closing(self):
_ok_open(self.cursor)
with self.cursor:
pass
_ok_closed(self.cursor)

def test_cursor_with_closing(self):
_ok_open(self.cursor)
with closing(self.cursor):
pass
_ok_closed(self.cursor)


def _ok_closed(thing):
ok_(thing.closed, "The object should have gotten closed")


def _ok_open(thing):
ok_(not thing.closed, "The object should be open")
29 changes: 5 additions & 24 deletions workers/gene_annotator.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,8 @@
# TODO(tavi) Handle inconsistent states and retries.
@worker.task
def annotate(vcf_id):
engine, connection, metadata = initialize_database(DATABASE_URI)

with closing(connection, pre_finally_fun=discard_temp,
connection=connection):
_, connection, metadata = initialize_database(DATABASE_URI)
try:
genotypes = metadata.tables.get('genotypes')
stmt = select([genotypes.c.contig, genotypes.c.position]).where(
genotypes.c.vcf_id == vcf_id)
Expand All @@ -51,16 +49,14 @@ def annotate(vcf_id):

# Open file for both writing (the gene annotations) and reading that
# out to Postgres
csv_file = temp_csv(mode='r+', tmp_dir=TEMPORARY_DIR)
with closing(csv_file):
with temp_csv(mode='r+', tmp_dir=TEMPORARY_DIR) as csv_file:
# Don't use commas as a delim, as commas are part of gene_names
csv.writer(csv_file, delimiter='\t').writerows(gene_names)

# Back to the beginning of the file
csv_file.seek(0, 0)

cursor = connection.connection.cursor()
with closing(cursor):
with connection.connection.cursor() as cursor:
tmp_table = Table('gene_annotations',
metadata,
Column('contig', Text, nullable=False),
Expand All @@ -82,20 +78,5 @@ def annotate(vcf_id):

# Update the list of extant columns for the UI
update_extant_columns(metadata, connection, vcf_id)

def discard_temp(**kwargs):
connection = kwargs.get('connection', None)
if connection:
connection.execute("DISCARD TEMP")
connection.connection.commit()

@contextmanager
def closing(thing, pre_finally_fun=None, **kwargs):
try:
yield thing
finally:
try:
if pre_finally_fun:
pre_finally_fun(**kwargs)
finally:
thing.close()
connection.execute("DISCARD TEMP")

0 comments on commit 5b2ed1c

Please sign in to comment.