allpy
changeset 823:0192c5c09ce8
allpy.base: added parameter use_existing to *.add_markup() to allow same markup to be added multiple times explicitly [see #95]
author | Daniil Alexeyevsky <dendik@kodomo.fbb.msu.ru> |
---|---|
date | Fri, 15 Jul 2011 17:02:09 +0400 |
parents | d87129162eb4 |
children | 41f7241c2aeb |
files | allpy/base.py test/test_markups.py |
diffstat | 2 files changed, 24 insertions(+), 2 deletions(-) [+] |
line diff
1.1 --- a/allpy/base.py Fri Jul 15 16:43:03 2011 +0400 1.2 +++ b/allpy/base.py Fri Jul 15 17:02:09 2011 +0400 1.3 @@ -109,11 +109,13 @@ 1.4 """Hook to be called from __init__ of actual class.""" 1.5 self.markups = {} 1.6 1.7 - def add_markup(self, name, markup_class=None, **markup_kwargs): 1.8 + def add_markup(self, name, markup_class=None, use_existing=False, **kws): 1.9 """Create a markup object, add to self. Return the created markup. 1.10 1.11 - `name` is name for markup in `self.markups` dictionary 1.12 - optional `markup_class` is class for created markup 1.13 + - if optional `use_existing` is true, it is no error, if same named 1.14 + markup already exists (in this case, nothing is changed) 1.15 - optional keyword arguments are passed on to the markup constructor 1.16 1.17 For user markups you have to specify `name` and `markup_class`, 1.18 @@ -127,8 +129,11 @@ 1.19 if markup_class is None: 1.20 kind = self.kind + "_" + "markup" 1.21 markup_class = markups.by_name[kind, name] 1.22 + if use_existing and name in self.markups: 1.23 + assert self.markups[name].__class__ is markup_class 1.24 + return 1.25 assert name not in self.markups 1.26 - markup = markup_class(self, name, caller='container', **markup_kwargs) 1.27 + markup = markup_class(self, name, caller='container', **kws) 1.28 self.markups[name] = markup 1.29 return markup 1.30
2.1 --- a/test/test_markups.py Fri Jul 15 16:43:03 2011 +0400 2.2 +++ b/test/test_markups.py Fri Jul 15 17:02:09 2011 +0400 2.3 @@ -15,6 +15,23 @@ 2.4 assert seq[5].number == 6 2.5 assert seq.markups["number"][seq[5]] == 6 2.6 2.7 + try: 2.8 + seq.add_markup('number') 2.9 + except AssertionError: 2.10 + pass 2.11 + else: 2.12 + raise AssertionError("Silently created same named alignment") 2.13 + 2.14 + seq.add_markup('number', use_existing=True) 2.15 + 2.16 + try: 2.17 + other = markups.SequenceIndexMarkup 2.18 + seq.add_markup('number', use_existing=True, markup_class=other) 2.19 + except AssertionError: 2.20 + pass 2.21 + else: 2.22 + raise AssertionError("Created same named different class alignment") 2.23 + 2.24 def test_case(): 2.25 seq = protein.Sequence.from_string('seQVEncE', name='sequence') 2.26 seq.add_markup('case')