Python 如何使Robot框架函数接受作为字符串而不是默认unicode类型的参数
我正在编写一个新的RF库,它应该采用字符串参数,因为我使用的(预先存在的)Python库应该是字符串,而不是unicode。 当然,在调用仅支持字符串的现有函数之前,我可以将每个unicode转换为字符串Python 如何使Robot框架函数接受作为字符串而不是默认unicode类型的参数,python,unicode,robotframework,configparser,Python,Unicode,Robotframework,Configparser,我正在编写一个新的RF库,它应该采用字符串参数,因为我使用的(预先存在的)Python库应该是字符串,而不是unicode。 当然,在调用仅支持字符串的现有函数之前,我可以将每个unicode转换为字符串 import ConfigParser class RFConfigParser: def get (self,section, option): print type (section) #prints unicode section = str (section) #w
import ConfigParser
class RFConfigParser:
def get (self,section, option):
print type (section) #prints unicode
section = str (section) #works but I dont want to do this
return self._config.get (section, option) #this pre-existing function expect a string input
问题是我有很多类似的函数,在每一个函数中我都必须调用这个unicode到字符串的转换马戏团
是否有一种直接的方法来实现这一点,以便RF函数直接接受字符串格式
另一个问题是默认的unicode支持机器人框架功能还是骑乘功能?(我正在使用RIDE,这就是我遇到这个问题的原因)在将这些Unicode字符串传递到库之前,可以使用Evaluate关键字将其转换为普通字符串 大概是这样的: lib.py:
def foo(foo):
print type(foo)
test.txt
*** Settings ***
Library lib.py
*** Test Cases ***
demo
${bar} Evaluate str('bar')
foo ${bar}
最佳解决方案取决于具体情况。也许一种解决方案是编写一个关键字来为您进行转换,然后调用库函数。也许最好的选择仍然是修改库以接受Unicode字符串。这要视情况而定。在将这些Unicode字符串传递到库之前,可以使用Evaluate关键字将其转换为普通字符串 大概是这样的: lib.py:
def foo(foo):
print type(foo)
test.txt
*** Settings ***
Library lib.py
*** Test Cases ***
demo
${bar} Evaluate str('bar')
foo ${bar}
最佳解决方案取决于具体情况。也许一种解决方案是编写一个关键字来为您进行转换,然后调用库函数。也许最好的选择仍然是修改库以接受Unicode字符串。视情况而定。如果您使用的是远程库,请注意RobotFramework会检查要通过XmlRpc传输的数据的内容:
def _handle_binary_result(self, result):
if not self._contains_binary(result):
return result
try:
result = str(result)
...
如果数据恰好只包含ASCII,它将传输字符串,如果数据不能用ASCII编码,它将传输二进制数据。我不知道您如何强制使用unicode结果类型,并避免因比较操作而引起的投诉,例如关于不同数据类型的应等于:
Argument types are: <type 'str'> <type 'unicode'>
对于给定的输入,接收不同类型的结果
return unicode(u'test', 'utf-8') --> 'test'
return unicode(u'ü', 'utf-8') --> u'ü'
如果您使用的是远程库,请注意RobotFramework会检查要通过XmlRpc传输的数据的内容:
def _handle_binary_result(self, result):
if not self._contains_binary(result):
return result
try:
result = str(result)
...
如果数据恰好只包含ASCII,它将传输字符串,如果数据不能用ASCII编码,它将传输二进制数据。我不知道您如何强制使用unicode结果类型,并避免因比较操作而引起的投诉,例如关于不同数据类型的应等于:
Argument types are: <type 'str'> <type 'unicode'>
对于给定的输入,接收不同类型的结果
return unicode(u'test', 'utf-8') --> 'test'
return unicode(u'ü', 'utf-8') --> u'ü'