You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
>>> "ACGT" * 4.5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
>>> "ACGT" * 4.0
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't multiply sequence by non-int of type 'float'
It would be nice to define the relevant methods in the Seq object and UnknownSeq object (although it is less clear to me what that MutableSeq should do here - presumably the same, return a new object rather than acting in place?).
Likewise for the SeqRecord object, which should probably follow the existing meta-data behaviour for addition. i.e. record * 3`` and 3 * recordandrecord + record + record`` should all return the same thing.
This would be particularly useful for constructing test cases (e.g. making a SeqRecord of 1000 base pairs long using "ACGT" * 25 as the sequence).
Regarding your question, maybe MutableSeq should return a new object and not in in-place in the same way that in native Python my_list * 3 returns a new list and leaves the original unchanged. This also feels right in terms of methods, I guess __mul__ for record * 3, __rmul__ for 3 * record, and __imul__ for record *= 3 (the in-place case).
Good example Chris - yes, the behaviour of a Python list convinces me that left and right multiplication on a MutableSeq should also return a new object rather than be in place (i.e. __mul__ and __rmul__).
I had not considered in place multiplication, here I think like the Python string we should not support this for the (read only / conceptually immutable) Seq and UnknownSeq etc:
In the Python docs they say "sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods _add_(), _radd_(), _iadd_(), _mul_(), _rmul_() and _imul_()",
(but I don't know if Seq is meant to be implemented as a sequence type; isinstance(Seq('A'), collections.Sequence) is False)
That'd be great. This is not urgent, I filed this issue in part as a public TODO note for myself, but also in case someone else wanted to tackle it.
Note also we'd want to test this with the BioSQL subclasses - I have some thoughts on that, but in the first instance raising a not implemented exception might be simplest.
Activity
chris-rands commentedon May 22, 2018
Regarding your question, maybe
MutableSeqshould return a new object and not in in-place in the same way that in native Pythonmy_list * 3returns a new list and leaves the original unchanged. This also feels right in terms of methods, I guess__mul__forrecord * 3,__rmul__for3 * record, and__imul__forrecord *= 3(the in-place case).peterjc commentedon May 22, 2018
Good example Chris - yes, the behaviour of a Python list convinces me that left and right multiplication on a
MutableSeqshould also return a new object rather than be in place (i.e.__mul__and__rmul__).I had not considered in place multiplication, here I think like the Python string we should not support this for the (read only / conceptually immutable)
SeqandUnknownSeqetc:However, for the
MutableSeqagain following a Python list, supporting in-place multiplication (magic method__imul__) seems fine:chris-rands commentedon May 22, 2018
Thanks Peter, string literals don't support
*=, but strings assigned to variables do (as do other immutable types like tuples):Also
Seqobjects already support+=(without__iadd__defined inSeq.py, I think python automatically overides it?):In the Python docs they say "sequence types should implement addition (meaning concatenation) and multiplication (meaning repetition) by defining the methods _add_(), _radd_(), _iadd_(), _mul_(), _rmul_() and _imul_()",
(but I don't know if
Seqis meant to be implemented as a sequence type;isinstance(Seq('A'), collections.Sequence)isFalse)peterjc commentedon May 22, 2018
Good point - I've never used in-place sequence multiplication so missed that.
I would say
Seq,UnknownSeq,MutableSeqetc are all sequence types, even if not usingcollections.Sequencewhich is comparatively new in Python.Do you fancy working on this with a pull request?
chris-rands commentedon May 23, 2018
I would be happy to work on a pull request, but I'm fairly busy right now, can you wait a few weeks or more for this?
peterjc commentedon May 23, 2018
That'd be great. This is not urgent, I filed this issue in part as a public TODO note for myself, but also in case someone else wanted to tackle it.
Note also we'd want to test this with the
BioSQLsubclasses - I have some thoughts on that, but in the first instance raising a not implemented exception might be simplest.peterjc commentedon Jun 1, 2018
Closed via #1669, thanks Chris!