当前位置: 首页>>代码示例>>Python>>正文


Python BuiltIn.replace_variables方法代码示例

本文整理汇总了Python中robot.libraries.BuiltIn.BuiltIn.replace_variables方法的典型用法代码示例。如果您正苦于以下问题:Python BuiltIn.replace_variables方法的具体用法?Python BuiltIn.replace_variables怎么用?Python BuiltIn.replace_variables使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在robot.libraries.BuiltIn.BuiltIn的用法示例。


在下文中一共展示了BuiltIn.replace_variables方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Utility

# 需要导入模块: from robot.libraries.BuiltIn import BuiltIn [as 别名]
# 或者: from robot.libraries.BuiltIn.BuiltIn import replace_variables [as 别名]
class Utility(object):
    """Utility keywords for Requests operations."""

    def __init__(self):
        self._builtin = BuiltIn()
        self._os = OperatingSystem()

    def get_json_file(self, path):
        """Returns [http://goo.gl/o0X6Pp|JSON] object from [http://goo.gl/o0X6Pp|JSON] file
        with all variables replaced.

        Arguments:
        - ``path``: The path to JSON file.

        Examples:

        _request.json_
        | [{
        |     "bad": ${false},
        |     "good": ${true},
        |     "key": 5.5,
        |     "key2": "value2"
        | }]

        | @{var} = | Get JSON File | request.json |
        | # [{'key2': 'value2', 'bad': False, 'good': True, 'key': Decimal('5.5')}] |
        """
        content = self._os.get_file(path)
        content = self._builtin.replace_variables(content)
        content = sub(r'(False|True)', lambda match: match.group(1).lower(), content)
        logger.debug(content)
        return self.json_loads(content)

    def json_loads(self, text):
        # pylint: disable=line-too-long
        """Returns [http://goo.gl/o0X6Pp|JSON] object from [http://goo.gl/o0X6Pp|JSON] string
        with object restoration support.

        Arguments:
        - ``text``: JSON string.

        Supported object restoration:
        | `py/dict`                    |
        | `py/tuple`                   |
        | `py/set`                     |
        | `py/collections.namedtuple`  |
        | `py/collections.OrderedDict` |

        Examples:
        | @{var} = | JSON Loads | [{"key":"value"}] |
        | @{var} = | JSON Loads | [{"py/dict":{"key":"value"}}] |
        | @{var} = | JSON Loads | [{"py/tuple":(1,2,3)}] |
        | @{var} = | JSON Loads | [{"py/set":[1,2,3]}] |
        | @{var} = | JSON Loads | [{"py/collections.namedtuple":{"fields":"a b c","type":"NAME","values":(0,1,2)}}] |
        | @{var} = | JSON Loads | [{"py/collections.OrderedDict":[("key2",2),("key1",1)]}] |
        """
        # pylint: disable=line-too-long
        return loads(text, object_hook=self._restore, parse_float=Decimal)

    def natural_sort_list_of_dictionaries(self, items, key):
        """Returns natural sorted list of dictionaries.

        Arguments:
        - ``items``: List of dictionaries to be sorted.
        - ``key``: The dictionary key to be used to sort.

        Examples:
        | @{var} = | Natural Sort List Of Dictionaries | ${list} | key |
        """
        def _natural_sorter(item):
            """Returns splitted aphanumeric value list of given dictionary key."""
            return [self._cast_alphanumeric(text) for text in split('([0-9]+)', item[key])]
        return sorted(items, key=_natural_sorter)

    @staticmethod
    def _cast_alphanumeric(text):
        """Casts alphanumeric value."""
        return int(text) if text.isdigit() else text.lower()

    @staticmethod
    def _restore(dct):
        """Returns restored object."""
        if "py/dict" in dct:
            return dict(dct["py/dict"])
        if "py/tuple" in dct:
            return tuple(dct["py/tuple"])
        if "py/set" in dct:
            return set(dct["py/set"])
        if "py/collections.namedtuple" in dct:
            data = dct["py/collections.namedtuple"]
            return namedtuple(data["type"], data["fields"])(*data["values"])
        # if "py/numpy.ndarray" in dct:
        #     data = dct["py/numpy.ndarray"]
        #     return np.array(data["values"], dtype=data["dtype"])
        if "py/collections.OrderedDict" in dct:
            return OrderedDict(dct["py/collections.OrderedDict"])
        return dct
开发者ID:rickypc,项目名称:robotframework-extendedrequestslibrary,代码行数:99,代码来源:utility.py


注:本文中的robot.libraries.BuiltIn.BuiltIn.replace_variables方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。