Skip to content
Browse files

modifications in CompositeTuple (now CompositeField)

  • Loading branch information...
1 parent 23f4a92 commit 0510f1874bf32e0bc00d0a0c80c8aa5b10c12f40 @alvesjnr alvesjnr committed with gustavofonseca May 30, 2011
Showing with 13 additions and 13 deletions.
  1. +2 −2 isis/model/mapper.py
  2. +11 −11 isis/model/subfield.py
View
4 isis/model/mapper.py
@@ -19,7 +19,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from .ordered import OrderedProperty, OrderedModel
-from .subfield import CompositeString, CompositeTuple
+from .subfield import CompositeString, CompositeField
import json
import colander
import deform
@@ -270,7 +270,7 @@ def __set__(self, instance, value):
raise TypeError('%r value must be a key-value structure' % self.name)
try:
- value = CompositeTuple(value_as_dict, self.subkeys)
+ value = CompositeField(value_as_dict, self.subkeys)
except TypeError:
raise TypeError('%r got an unexpected keyword' % self.name)
View
22 isis/model/subfield.py
@@ -90,42 +90,42 @@ def __str__(self):
return str(self.__isis_raw)
-class CompositeTuple(object):
+class CompositeField(object):
''' Represent an Isis field, with subfields, using
Python native datastructures
- >>> author = CompositeTuple( [('name','Braz, Marcelo'),('role','writer')] )
+ >>> author = CompositeField( [('name','Braz, Marcelo'),('role','writer')] )
>>> print author['name']
Braz, Marcelo
>>> print author['role']
writer
>>> author
- CompositeTuple((('name', 'Braz, Marcelo'), ('role', 'writer')))
+ CompositeField((('name', 'Braz, Marcelo'), ('role', 'writer')))
'''
def __init__(self, value, subkeys=None):
if subkeys is None:
subkeys = [item[0] for item in value]
- composite = namedtuple('Composite', subkeys)
try:
value_as_dict = dict(value)
except TypeError:
raise TypeError('%r value must be a key-value structure' % self)
- try:
- self.value = composite(**value_as_dict)
- except TypeError:
- raise TypeError('%r got an unexpected keyword' % self)
+ for key in value_as_dict:
+ if key not in subkeys:
+ raise TypeError('Unexpected keyword %r' % key)
+
+ self.value = tuple([(key, value_as_dict.get(key,None)) for key in subkeys])
def __getitem__(self, key):
- return getattr(self.value,key)
+ return dict(self.value)[key]
def __repr__(self):
- return "CompositeTuple(%s)" % str(self.items())
+ return "CompositeField(%s)" % str(self.items())
def items(self):
- return tuple(zip(self.value._fields, self.value))
+ return self.value
def __unicode__(self):
unicode(self.items())

0 comments on commit 0510f18

Please sign in to comment.
Something went wrong with that request. Please try again.