Permalink
Browse files

Making Seq.data read only

  • Loading branch information...
1 parent 9d9e46f commit 362d4c57b907dac52a12876dac9d31fa67c0fe83 @peterjc peterjc committed Nov 25, 2009
Showing with 35 additions and 17 deletions.
  1. +32 −13 Bio/Seq.py
  2. +3 −4 DEPRECATED
View
45 Bio/Seq.py
@@ -89,16 +89,32 @@ def __init__(self, data, alphabet = Alphabet.generic_alphabet):
self.alphabet = alphabet # Seq API requirement
# A data property is/was a Seq API requirement
- def _set_data(self, value):
- #TODO - In the next release, actually raise an exception?
- #The Seq object is like a python string, it should be read only!
- import warnings
- warnings.warn("Writing to the Seq object's .data propery is deprecated.",
- DeprecationWarning)
- self._data = value
- data = property(fget= lambda self : str(self),
- fset=_set_data,
- doc="Sequence as a string (DEPRECATED)")
+ # Note this is read only since the Seq object is meant to be imutable
+ @property
+ def data(self) :
+ """Sequence as a string (OBSOLETE/DEPRECATED).
+
+ This is a read only property provided for backwards compatility with
+ older versions of Biopython (as is the tostring() method). We now
+ encourage you to use str(my_seq) instead of my_seq.data or the method
+ my_seq.tostring().
+
+ In recent releases of Biopython it was possible to change a Seq object
+ by updating its data property, but this triggered a deprecation warning.
+ Now the data property is read only, since Seq objects are meant to be
+ immutable:
+
+ >>> from Bio.Seq import Seq
+ >>> from Bio.Alphabet import generic_dna
+ >>> my_seq = Seq("ACGT", generic_dna)
+ >>> str(my_seq) == my_seq.tostring() == my_seq.data == "ACGT"
+ True
+ >>> my_seq.data = "AAAA"
+ Traceback (most recent call last):
+ ...
+ AttributeError: can't set attribute
+ """
+ return str(self)
def __repr__(self):
"""Returns a (truncated) representation of the sequence for debugging."""
@@ -114,7 +130,7 @@ def __repr__(self):
repr(self.data),
repr(self.alphabet))
def __str__(self):
- """Returns the full sequence as a python string.
+ """Returns the full sequence as a python string, use str(my_seq).
Note that Biopython 1.44 and earlier would give a truncated
version of repr(my_seq) for str(my_seq). If you are writing code
@@ -127,9 +143,12 @@ def __str__(self):
# __hash__ and therefore use as dictionary keys. See also:
# http://mail.python.org/pipermail/python-dev/2002-December/031455.html
- def __len__(self): return len(self._data) # Seq API requirement
+ def __len__(self):
+ """Returns the length of the sequence, use len(my_seq)."""
+ return len(self._data) # Seq API requirement
def __getitem__(self, index) : # Seq API requirement
+ """Returns a subsequence of single letter, use my_seq[index]."""
#Note since Python 2.0, __getslice__ is deprecated
#and __getitem__ is used instead.
#See http://docs.python.org/ref/sequence-methods.html
@@ -236,7 +255,7 @@ def __radd__(self, other):
raise TypeError
def tostring(self): # Seq API requirement
- """Returns the full sequence as a python string.
+ """Returns the full sequence as a python string (OBSOLETE).
Although not formally deprecated, you are now encouraged to use
str(my_seq) instead of my_seq.tostring()."""
View
7 DEPRECATED
@@ -79,15 +79,14 @@ use Bio.Motif instead.
Numeric support
===============
-Following the release of 1.48, Numeric support in Biopython is discontinued.
+Following the Release of 1.48, Numeric support in Biopython is discontinued.
Please move to NumPy for Biopython 1.49 or later.
Bio.Seq and the data property
=============================
Direct use of the Seq object (and MutableSeq object) .data property is
-discouraged. As of release 1.49, writing to the Seq object's .data property
-triggers a warning, and this property is likely to be made read only in the
-next release.
+discouraged. As of Release 1.49, writing to the Seq object's .data property
+triggered a warning, and this property was made read only in Release 1.53.
Bio.Transcribe and Bio.Translate
================================

0 comments on commit 362d4c5

Please sign in to comment.