开发者

XML-RPC method parameter data typing in Python

开发者 https://www.devze.com 2022-12-14 19:52 出处:网络
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 a开发者_StackOver

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 a开发者_StackOverflow社区nd 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.


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.


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.


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

number = re.escape(number)

This way things always gets converted to strings

0

精彩评论

暂无评论...
验证码 换一张
取 消