Skip to content

Commit

Permalink
Docs/test for undo_monkey_patch
Browse files Browse the repository at this point in the history
  • Loading branch information
Lenna Peterson committed Jul 9, 2012
1 parent 6b1fa89 commit 817f5e9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions vcf/sample_filter.py
@@ -1,5 +1,3 @@
#!/usr/bin/env python

# Author: Lenna X. Peterson # Author: Lenna X. Peterson
# github.com/lennax # github.com/lennax
# arklenna at gmail dot com # arklenna at gmail dot com
Expand All @@ -13,6 +11,13 @@




class SampleFilter(object): class SampleFilter(object):
"""
Modifies the vcf Reader to filter each row by sample as it is parsed.
When using the class, be sure to call `undo_monkey_patch()` to restore
the original functionality to the Reader.
"""

def __init__(self, infile, outfile=None, filters=None, invert=False): def __init__(self, infile, outfile=None, filters=None, invert=False):
# Methods to add to Reader # Methods to add to Reader
def get_filter(self): def get_filter(self):
Expand Down Expand Up @@ -99,5 +104,5 @@ def write(self, outfile=None):
writer.write_record(row) writer.write_record(row)


def undo_monkey_patch(self): def undo_monkey_patch(self):
delattr(Reader, 'sample_filter')
Reader._parse_samples = self._orig_parse_samples Reader._parse_samples = self._orig_parse_samples
delattr(Reader, 'sample_filter')
2 changes: 1 addition & 1 deletion vcf/test/test_vcf.py
Expand Up @@ -669,10 +669,10 @@ def testSampleFilterModule(self):
#print(buf.getvalue()) #print(buf.getvalue())
# undo monkey patch # undo monkey patch
filt.undo_monkey_patch() filt.undo_monkey_patch()
self.assertTrue('sample_filter' not in dir(vcf.Reader))
# read output # read output
reader = vcf.Reader(buf) reader = vcf.Reader(buf)
self.assertEqual(reader.samples, ['NA00001']) self.assertEqual(reader.samples, ['NA00001'])
print(dir(reader))
rec = reader.next() rec = reader.next()
self.assertEqual(len(rec.samples), 1) self.assertEqual(len(rec.samples), 1)


Expand Down

0 comments on commit 817f5e9

Please sign in to comment.