354: Added NameSpace.__todict__() method that returns copy of NameSpace.__map; updated NameSpace unit test to also test __todict__()

This commit is contained in:
Jason Gerard DeRose 2008-09-24 23:49:44 +00:00
parent be2e323bbf
commit f531f7da81
3 changed files with 20 additions and 8 deletions

View File

@ -521,8 +521,13 @@ class Object(plugable.Plugin):
takes_params = tuple()
def __create_params(self):
for param in self.takes_params:
yield create_param(param)
props = self.properties.__todict__()
for spec in self.takes_params:
if type(spec) is str and spec.rstrip('?*+') in props:
yield props.pop(spec.rstrip('?*+')).param
else:
yield create_param(spec)
def set_api(self, api):
super(Object, self).set_api(api)

View File

@ -545,13 +545,13 @@ class NameSpace(ReadOnly):
def __len__(self):
"""
Returns the number of members.
Return the number of members.
"""
return len(self.__members)
def __iter__(self):
"""
Iterates through the member names.
Iterate through the member names.
If this instance was created with ``sort=True``, the names will be in
alphabetical order; otherwise the names will be in the same order as
@ -564,7 +564,7 @@ class NameSpace(ReadOnly):
def __call__(self):
"""
Iterates through the members.
Iterate through the members.
If this instance was created with ``sort=True``, the members will be
in alphabetical order by name; otherwise the members will be in the
@ -577,13 +577,13 @@ class NameSpace(ReadOnly):
def __contains__(self, name):
"""
Returns True if namespace has a member named ``name``.
Return True if namespace has a member named ``name``.
"""
return name in self.__map
def __getitem__(self, spec):
"""
Returns a member by name or index, or returns a slice of members.
Return a member by name or index, or returns a slice of members.
:param spec: The name or index of a member, or a slice object.
"""
@ -597,7 +597,7 @@ class NameSpace(ReadOnly):
def __repr__(self):
"""
Returns a pseudo-valid expression that could create this instance.
Return a pseudo-valid expression that could create this instance.
"""
return '%s(<%d members>, sort=%r)' % (
self.__class__.__name__,
@ -605,6 +605,12 @@ class NameSpace(ReadOnly):
self.__sort,
)
def __todict__(self):
"""
Return a copy of the private dict mapping name to member.
"""
return dict(self.__map)
class Registrar(DictProxy):
"""

View File

@ -586,6 +586,7 @@ class test_NameSpace(ClassChecker):
else:
ordered = members
names = tuple(m.name for m in ordered)
assert o.__todict__() == dict((o.name, o) for o in ordered)
# Test __len__:
assert len(o) == cnt