| |
- NamedValueAccess
-
- NamedValueAccessWrapper
- exceptions.LookupError(exceptions.StandardError)
-
- NamedValueAccessError
-
- ValueForKeyError
class NamedValueAccess |
|
This class is intended to be ancestor class such that you can say:
from NamedValueAccess import *
age = someObj.valueForName("age")
name = someObj.valueForName("info.fields.name")
This can be useful in setups where you wish to textually refer to the objects
in a program, such as an HTML template processed in the context of an
object-oriented framework.
Keys can be matched to either methods or ivars and with or without underscores.
valueForName() can also traverse bona fide dictionaries (DictType).
You can safely import * from this module. Only the NamedValueAccess class is exported
(other than typical things like string and sys).
There is no __init__() method and never will be.
You can run the test suite by running this module as a program.
You'll see the terms 'key' and 'name' in the class and its documentation. A 'key'
is a single identifier such as 'foo'. A name could be key, or a qualified key,
such as 'foo.bar.boo'. Names are generally more convenient and powerful, while
key-oriented methods are more efficient and provide the atomic functionality that
name-oriented methods are built upon. From a usage point of view, you normally
just use the 'name' methods and forget about the 'key'.
@@ 2000-05-21 ce: This class causes problems when used in WebKit for logging.
Perhaps circular references?
Involving self?
Having to do with methods bound to their objects?
@@ 2000-03-03 ce: document ivars
@@ 2000-04-24 ce: Some classes like UserDict need to use getitem()
instead of getattr() and don't need to deal with _bindingForGetKey().
@@ 2000-05-31 ce: Rename this class to NamedValues, NamedValueAccess, ValuesByName
@@ This class probably needs to be in MiscUtils, as it's being used in that way
while MiddleKit was intended for "enterprise/business objects". |
|
Methods defined here:
- hasValueForKey(self, key)
- Returns true if the key is available, although that does not
guarantee that there will not be errors caused by retrieving the key.
- hasValueForName(self, keysString)
- resetKeyBindings(self)
- setValueForKey(self, key, value)
- Suppose key is 'foo'. This method sets the value with the following precedence:
1. Public attributes before private attributes
2. Methods before non-methods
More specifically, this method then uses one of the following:
@@ 2000-03-04 ce: fill in
...or invokes handleUnknownSetKey().
- valueForKey(self, key, default=<class MiscUtils.NoDefault>)
- Suppose key is 'foo'. This method returns the value with the following precedence:
1. Methods before non-methods
2. Public attributes before private attributes
More specifically, this method then returns one of the following:
* foo()
* _foo()
* self.foo
* self._foo
...or default, if it was specified,
otherwise invokes and returns result of valueForUnknownKey().
Note that valueForUnknownKey(), normally returns an exception.
See valueForName() which is a more advanced version of this method that allows
multiple, qualified keys.
- valueForKeySequence(self, listOfKeys, default=None)
- valueForName(self, keysString, default=None)
- Returns the value for the given keysString. This is the more advanced version of
valueForKey(), which can only handle single names. This method can handle
'foo', 'foo1.foo2', 'a.b.c.d', etc. It will traverse dictionaries if needed.
- valueForUnknownKey(self, key, default)
- # Errors
- valuesForNames(self, keys, default=None, defaults=None, forgive=0, includeNames=0)
- Returns a list of values that match the given keys, each of which is passed
through valueForName() and so could be of the form 'a.b.c'.
keys is a sequence. default is any kind of object. defaults is a sequence.
forgive and includeNames is a flag.
If default is not None, then it is substituted when a key is not found.
Otherwise, if defaults is not None, then it's corresponding/parallel value
for the current key is substituted when a key is not found.
Otherwise, if forgive=1, then unknown keys simply don't produce any values.
Otherwise, if default and defaults are None, and forgive=0, then the unknown
keys will probably raise an exception through valueForUnknownKey() although
that method can always return a final, default value.
if keys is None, then None is returned. If keys is an empty list, then None
is returned.
Often these last four arguments are specified by key.
Examples:
names = ['origin.x', 'origin.y', 'size.width', 'size.height']
obj.valuesForNames(names)
obj.valuesForNames(names, default=0.0)
obj.valuesForNames(names, defaults=[0.0, 0.0, 100.0, 100.0])
obj.valuesForNames(names, forgive=0)
@@ 2000-03-04 ce: includeNames is only supported when forgive=1.
It should be supported for the other cases.
It should be documented.
It should be included in the test cases.
|
class NamedValueAccessWrapper(NamedValueAccess) |
|
This provides a wrapper around an existing object which will respond
to the methods of NamedValueAccess. By using the wrapper, you can
stick with objects and methods such as obj.valueForName('x.y') (as
opposed to functions like valueForName()) and refrain from modifying
the existing class hierarchy with NamedValueAccess.
Example:
wrapper = NamedValueAccessWrapper(obj)
print wrapper.valueForName('manager.name') |
|
Methods defined here:
- __init__(self, object)
- hasValueForKey(self, key)
- valueForKey(self, key, default=<class MiscUtils.NoDefault>)
- valueForName(self, key, default=<class MiscUtils.NoDefault>)
Methods inherited from NamedValueAccess:
- hasValueForName(self, keysString)
- resetKeyBindings(self)
- setValueForKey(self, key, value)
- Suppose key is 'foo'. This method sets the value with the following precedence:
1. Public attributes before private attributes
2. Methods before non-methods
More specifically, this method then uses one of the following:
@@ 2000-03-04 ce: fill in
...or invokes handleUnknownSetKey().
- valueForKeySequence(self, listOfKeys, default=None)
- valueForUnknownKey(self, key, default)
- # Errors
- valuesForNames(self, keys, default=None, defaults=None, forgive=0, includeNames=0)
- Returns a list of values that match the given keys, each of which is passed
through valueForName() and so could be of the form 'a.b.c'.
keys is a sequence. default is any kind of object. defaults is a sequence.
forgive and includeNames is a flag.
If default is not None, then it is substituted when a key is not found.
Otherwise, if defaults is not None, then it's corresponding/parallel value
for the current key is substituted when a key is not found.
Otherwise, if forgive=1, then unknown keys simply don't produce any values.
Otherwise, if default and defaults are None, and forgive=0, then the unknown
keys will probably raise an exception through valueForUnknownKey() although
that method can always return a final, default value.
if keys is None, then None is returned. If keys is an empty list, then None
is returned.
Often these last four arguments are specified by key.
Examples:
names = ['origin.x', 'origin.y', 'size.width', 'size.height']
obj.valuesForNames(names)
obj.valuesForNames(names, default=0.0)
obj.valuesForNames(names, defaults=[0.0, 0.0, 100.0, 100.0])
obj.valuesForNames(names, forgive=0)
@@ 2000-03-04 ce: includeNames is only supported when forgive=1.
It should be supported for the other cases.
It should be documented.
It should be included in the test cases.
|
|