tags:

views:

59

answers:

3

I have built an XML-RPC interface in Python and I need to enforce some stricter typing. For example, passing string '10' instead of int 10. I can clean this up with some type casting and a little exception handling, but I am wondering if there is any other way of forcing type integrity such as something XML-RPC specific, a decorator, or something else.

+1  A: 

It's always going to be converted to a string anyway, so why do you care what's being passed in? If you use "%s" % number or even just str(number), then it doesn't matter whether number is a string or an int.

A: 

XML-RPC methods (at least in xmlrpclib) are dispatched to Python functions or method, so you have to enforce type checking in them. There is a lot of recipes on doing this task with decorators: Type Enforcement (accepts/returns), typecheck module.

Note, that xmlrpclib lacks proper error handling, so you probably would like to implement your own.

Denis Otkidach
A: 

I always just use the re.escape function eg.:

number = re.escape(number)

This way things always gets converted to strings

lithorus