From d8f90b0980af657cbfb78c8db652a26c83a1c6bf Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sun, 10 Dec 2017 20:43:21 -0500 Subject: [PATCH] Make numpy an optional requirement --- pyfasta/fasta.py | 5 ++--- pyfasta/records.py | 21 +++++++++++++++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/pyfasta/fasta.py b/pyfasta/fasta.py index 05d1fa1..854a1dc 100644 --- a/pyfasta/fasta.py +++ b/pyfasta/fasta.py @@ -3,9 +3,8 @@ import os.path from collections import Mapping import sys -import numpy as np -from records import NpyFastaRecord +from records import np, DefaultRecord # string.maketrans is bytes.maketrans in Python 3, but # we want to deal with strings instead of bytes @@ -40,7 +39,7 @@ def __init__(self, header): Exception.__init__(self, 'headers must be unique: %s is duplicated' % header) class Fasta(Mapping): - def __init__(self, fasta_name, record_class=NpyFastaRecord, + def __init__(self, fasta_name, record_class=DefaultRecord, flatten_inplace=False, key_fn=None): """ >>> from pyfasta import Fasta, FastaRecord diff --git a/pyfasta/records.py b/pyfasta/records.py index 677c17a..bff814b 100644 --- a/pyfasta/records.py +++ b/pyfasta/records.py @@ -1,9 +1,20 @@ import cPickle -import numpy as np import sys import os -__all__ = ['FastaRecord', 'NpyFastaRecord', 'MemoryRecord'] +try: + import numpy as np +except ImportError as err: + class NotAvailable: + def __getattr__(self, key): + raise err + def __nonzero__(self): + return False + def __bool__(self): + return False + np = NotAvailable() + +__all__ = ['FastaRecord', 'NpyFastaRecord', 'MemoryRecord', 'DefaultRecord'] MAGIC = "@flattened@" @@ -293,3 +304,9 @@ def prepare(klass, fasta_obj, seqinfo_generator, flatten_inplace): __all__.append('TCRecord') except ImportError: pass + +if np: + DefaultRecord = NpyFastaRecord +else: + DefaultRecord = MemoryRecord +