Пример #1
0
    def exec(self, payload: Log2ReqsAddOnPayload) -> TList[Request]:
        def line_to_request(line: str, seq: int) -> Request:
            logger.debug(f'{LOG_PREFIX} ---- {seq} ----')

            path = line.split('?')[0]
            logger.debug(f'{LOG_PREFIX} [path] {path}')

            url_encoding = 'utf-8'
            qs = {}
            if len(line.split('?')) > 1:
                url_encoding = guess_url_encoding(line.split('?')[1], self.config.candidate_for_url_encodings)\
                    .get_or('utf-8')
                qs = urlparser.parse_qs(
                    line.split('?')[1],
                    keep_blank_values=self.config.keep_blank,
                    encoding=url_encoding)
            logger.debug(f'{LOG_PREFIX} [qs] ({url_encoding}) {qs}')

            return Request.from_dict({
                "path": path,
                "qs": qs,
                "headers": {},
                "url_encoding": url_encoding
            })

        with open(payload.file, encoding=self.config.encoding) as f:
            requests: TList[Request] = TList([
                x.rstrip() for x in f if x != '\n'
            ]).emap(lambda x, i: line_to_request(x, i))

        return requests
Пример #2
0
    def fetch_all():
        offset = 0
        max_limit = 50

        while True:
            rs = TList(
                requests.get(
                    TODOIST_API_URL + '/completed/get_all',
                    params={
                        "token": todoist_token,
                        "since":
                        since.astimezone(utc).strftime('%Y-%m-%dT%H:%M'),
                        "offset": offset,
                        "limit": max_limit
                    }).json()['items'])
            yield rs

            if len(rs) != max_limit:
                break
            offset = offset + max_limit
Пример #3
0
    def from_dicts(cls, ds, force_snake_case=True):
        """From list of dict to list of instance

        :param ds: List of dict
        :type ds: List[dict]
        :param force_snake_case: Keys are transformed to snake case in order to compliant PEP8 if True
        :type force_snake_case: bool
        :return: List of instance
        :rtype: TList[T]

        Usage:

            >>> from owlmixin.samples import Human
            >>> humans = Human.from_dicts([
            ...    {"id": 1, "name": "Tom", "favorites": [{"name": "Apple"}]},
            ...    {"id": 2, "name": "John", "favorites": [{"name": "Orange"}]}
            ... ])
            >>> humans[0].name
            'Tom'
            >>> humans[1].name
            'John'
        """
        return TList([cls.from_dict(d, force_snake_case) for d in ds])
Пример #4
0
 def test_empty(self):
     assert TList([1, 2, 3, 4, 5]).not_intersection([1, 2, 3, 4, 5]) == []
Пример #5
0
 def test_normal(self):
     assert TList([1, 2, 3, 4, 5]).not_intersection([2, 4, 6]) == [1, 3, 5]
Пример #6
0
 def test_including_not_str(self):
     with pytest.raises(TypeError):
         TList(["1", 2, "3"]).join("---")
Пример #7
0
 def test_normal(self):
     assert TList(["a", "bc", "def"]).join("---") == "a---bc---def"
Пример #8
0
 def test_normal(self):
     assert TList([1, 2, 3]).sum() == 6
Пример #9
0
 def test_normal(self):
     assert TList([[1, 2], [3, 4]]).flatten() == [1, 2, 3, 4]
Пример #10
0
 def __extract_results(self, cls_: Type[T], path: str) -> TList[T]:
     return cls_.from_dicts(TList(
         self.__pagination_iterator(path)).flatten(),
                            restrict=False)