Permalink
Browse files

added default kw to leafattributes and default property to containers

  • Loading branch information...
1 parent cf584ea commit 67d3da88b7dd4866094675934918096b566df7f4 Tim Parkin committed Feb 10, 2010
Showing with 66 additions and 17 deletions.
  1. +5 −0 CHANGELOG
  2. +1 −1 schemaish.egg-info/PKG-INFO
  3. +36 −12 schemaish/attr.py
  4. +23 −3 schemaish/tests/test_attr.py
  5. +1 −1 setup.py
View
@@ -2,6 +2,11 @@
Changlelog
==========
+0.5.5 (2010-02-10)
+------------------
+
+* Added default values
+
0.5.4 (2009-06-30)
------------------
@@ -1,6 +1,6 @@
Metadata-Version: 1.0
Name: schemaish
-Version: 0.5.4b
+Version: 0.5.5
Summary: Schemaish is a simple schema library.
Home-page: http://schema.ish.io
Author: Tim Parkin, Matt Goodall
View
@@ -78,7 +78,8 @@ def __init__(self, **k):
validator = k.pop('validator', _MISSING)
if validator is not _MISSING:
self.validator = validator
- self.default = k.pop('default', None)
+ if k:
+ raise TypeError('unsupported keywords passed to schemaish.attr.Attribute or subclass')
def validate(self, value):
"""
@@ -99,61 +100,70 @@ def __repr__(self):
attributes.append('description=%r' % self.description)
if self.validator:
attributes.append('validator=%r'% self.validator)
- attributes.append('default=%r' % self.default)
+ if hasattr(self, 'default'):
+ attributes.append('default=%r' % self.default)
return 'schemaish.%s(%s)' % (self.__class__.__name__,
', '.join(attributes))
-class String(Attribute):
+class LeafAttribute(Attribute):
+
+ def __init__(self, **k):
+ self.default = k.pop('default', None)
+ super(LeafAttribute, self).__init__(**k)
+
+
+
+class String(LeafAttribute):
"""
A Python unicode instance.
"""
type = 'String'
-class Integer(Attribute):
+class Integer(LeafAttribute):
"""
A Python integer.
"""
type='Integer'
-class Float(Attribute):
+class Float(LeafAttribute):
"""
A Python float.
"""
type='Float'
-class Decimal(Attribute):
+class Decimal(LeafAttribute):
"""
A decimal.Decimal instance.
"""
type='Decimal'
-class Date(Attribute):
+class Date(LeafAttribute):
"""
A datetime.date instance.
"""
type='Date'
-class Time(Attribute):
+class Time(LeafAttribute):
"""
A datetime.time instance.
"""
type='Time'
-class DateTime(Attribute):
+class DateTime(LeafAttribute):
"""
A datetime.datetime instance.
"""
type='DateTime'
-class Boolean(Attribute):
+class Boolean(LeafAttribute):
"""
A Python Boolean instance.
"""
@@ -162,6 +172,7 @@ class Boolean(Attribute):
class Container(Attribute):
type='Container'
+
class Sequence(Container):
"""
A sequence (Python list) of attributes of a specific type.
@@ -180,6 +191,7 @@ def __init__(self, attr=None, **k):
super(Sequence, self).__init__(**k)
if attr is not None:
self.attr = attr
+ self.defaults = k.pop('default', None)
def validate(self, value):
"""
@@ -206,10 +218,14 @@ def validate(self, value):
if error_dict:
raise Invalid(error_dict)
+ @property
+ def default(self):
+ return []
+
def __repr__(self):
return 'schemaish.Sequence(%r)'%self.attr
-class Tuple(Attribute):
+class Tuple(Container):
"""
A Python tuple of attributes of specific types.
@@ -240,6 +256,10 @@ def add(self, attr):
else:
self.attrs.append(attr)
+ @property
+ def default(self):
+ return tuple( [a.default for a in self.attrs] )
+
def validate(self, value):
"""
Validate the tuple's items and the tuple itself.
@@ -338,6 +358,10 @@ def get(self, name):
return attr
raise KeyError(name)
+ @property
+ def default(self):
+ return dict( [(name, a.default) for name, a in self.attrs] )
+
def validate(self, value):
"""
Validate all items in the sequence and then validate the Sequence
@@ -368,7 +392,7 @@ def __repr__(self):
return 'schemaish.Structure(%s)'%(', '.join(attrstrings))
-class File(Attribute):
+class File(LeafAttribute):
"""
A File Object
"""
@@ -2,8 +2,8 @@
class TestAttribute(unittest.TestCase):
def _getTargetClass(self):
- from schemaish.attr import Attribute
- return Attribute
+ from schemaish.attr import LeafAttribute
+ return LeafAttribute
def _makeOne(self, **kw):
return self._getTargetClass()(**kw)
@@ -38,7 +38,7 @@ def test__repr__(self):
description='description',
validator=required)
begin_expected = (
- "schemaish.Attribute(title='title', "
+ "schemaish.LeafAttribute(title='title', "
"description='description', validator=<function required")
r = repr(attr)
self.assertEqual(r[:len(begin_expected)], begin_expected)
@@ -423,6 +423,26 @@ class DummyAttribute(Attribute):
type = 'Dummy'
return DummyAttribute(*arg, **kw)
+
+class TestDefaults(unittest.TestCase):
+
+ def test_defaults(self):
+ import schemaish
+ address = schemaish.Structure()
+ address.add('street', schemaish.String(default='Lidgett Lane'))
+ address.add('city', schemaish.String(default='Leeds'))
+ schema = schemaish.Structure()
+ schema.add('first_names', schemaish.String(default='Tim'))
+ schema.add('last_name', schemaish.String(default='Parkin'))
+ schema.add('code', schemaish.Tuple( [schemaish.String(default='legs'), schemaish.Integer(default=11)] ))
+ schema.add('address',address)
+ self.assertEqual(schema.default, {'address': {'city': 'Leeds', 'street': 'Lidgett Lane'},
+ 'code': ('legs', 11),
+ 'first_names': 'Tim',
+ 'last_name': 'Parkin'})
+
+
+
if __name__ == "__main__":
unittest.main()
View
@@ -1,7 +1,7 @@
from setuptools import setup, find_packages
import sys, os
-version = '0.5.4b'
+version = '0.5.5'
setup(name='schemaish',
version=version,

0 comments on commit 67d3da8

Please sign in to comment.