Python 7.0pl5 Benutzerhandbuch

Seite von 124
User Defined Types
83
"
xsd:hexBinary
" will also be added at some point in the future as well. If you wish to send a Uni-
code string, you should convert it into a string using UTF-8 encoding.
User Defined Types
The intent with the XML Schema Datatypes specification is that additional scalar data types can be
introduced by assigning a new name scoped within a distinct namespace. In respect of the types defined
by this specification, the namespace "
xsd
" is used. Note that within this implementation, the name-
space is not linked to a URI containing any form of definition for that type. If sending a data value of
your own type, it is up to your code to ensure that both ends know what the type means.
The simplest way of adding your own types is by using the
Opaque
class. When initialised this takes
two values, a string identifying the type of value and a string representing the value in its encoded form.
It is not necessary to escape any characters in the encoded value which may be special to XML as such
values will be automatically escaped as necessary.
data = complex(1,1)
type = "python:complex"
self.publishReport("complex",netsvc.Opaque(type,data))
In reality, it isn’t actually necessary to encode a Python
complex
value in the way shown as a special
mapping is by default installed for this type. For this Python type the namespace "
python
" is used.
If defining your own type it is recommended you use some other namespace value which is in some
way specifically associated with your application or some third party standard relating to additional
XML types.
As a special mapping is provided for the Python
complex
type, it will be decoded into an instance of
the Python
complex
type on reception. If however a mapping is not available for a specified type, the
value will be converted back into an instance of the
Opaque
type. The type associated with the value
can then be queried using the "
type
" attribute and the actual encoded data using the "
data
" attribute.
def dump(self,object):
if isinstance(object,netsvc.Opaque):
print object.type,object.data
The
Opaque
class provides a means of sending a value without a defined mapping, or of you being
able to receive values for which no mapping is defined. If necessary the interface of the
Opaque
class
can be used to dynamically handle such unknown values and perhaps still make some sense of them.
Adding New Mappings
Mappings for new types can be added at two levels. These are at global scope or such that they only
apply within the scope of a single service. If a type mapping is added at global scope, you should realise
that such a mapping will be applied to any service. Adding new mappings with global scope should
therefore be carefully considered as it may inadvertently affect the operation of another service.