Strange behavior with len and str.splitlines ()

I am making an HTTP request using requests and I want to create a simple response object that parses my data. The problem I am facing is as follows. When passing a response object to the ApiResponse, I split the Response.text in strings to count them and see if it has multiple or single strings. the problem is the split list gives me an error when applying len () to this, but in the console it works fine.

This is the class:

class ApiResponse(object):
pattern = re.compile(r'([a-zA-Z]+): ([a-zA-Z0-9]+)')  # original ([a-zA-Z]+)\: ([a-zA-Z0-9]+)
response_type = ResponseType.OK
response_mode = ResponseMode.SINGLE

def __init__(self, r: resp):
    self.r = r
    self.parse(r)
    self.data = None

def parse(self, r: resp):
    """
    Method that parses the response from the API
    :param r:Response
    """
    if r.status_code != 200:
        self.response_type = ResponseType.ERR

    if len(r.text.splitlines()) > 1:
        self.response_mode = ResponseMode.MULTI

    for line in r.text.splitlines():
        match = self.pattern.search(line)
        if match is None:
            break
        print(match.group(1, 2))  # REMOVE testing
        self.response_type = ResponseType[match.group(1)]

      

This is the console output:

>>> import sys
>>> print(sys.version)
3.4.2 (default, Oct  8 2014, 13:08:17) 
[GCC 4.9.1]
>>> import requests
>>> from clickapy.response import ApiResponse
>>> r = requests.get(API_URL, {'user': USER, 'password': PASS, 'api_id': API_ID})
>>> api_response = ApiResponse(r)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/home/eefret/PycharmProjects/clickapy/clickapy/response.py", line 32, in __init__
    self.parse(r)
  File "/home/eefret/PycharmProjects/clickapy/clickapy/response.py", line 43, in parse
    if len(r.text.splitlines()) > 1:
TypeError: object of type 'builtin_function_or_method' has no len()
>>> len(r.text.splitlines())
1

      

Why is this happening? It doesn't make sense to me, I am following the same code snippets, welcome any help or feedback.

+3


source to share


1 answer


A friend of mine (all credits ) helped me and since he doesn't have an SO account, I'll post what solved this behavior, my console was applying , but internally the text had to be encoded anyway, so solved it by changing this to this Mariano Garcia

utf-8

if len(r.text.splitlines()) > 1:

if len(r.text.encode("utf-8").splitlines()) > 1:

Complete code:



class ApiResponse(object):
    pattern = re.compile(r'([a-zA-Z]+): ([a-zA-Z0-9]+)')  # original ([a-zA-Z]+)\: ([a-zA-Z0-9]+)
    response_type = ResponseType.OK
    response_mode = ResponseMode.SINGLE

    def __init__(self, r: resp):
        self.r = r
        self.parse(r)
        self.data = None

    def parse(self, r: resp):
        """
        Method that parses the response from the API
        :param r:Response
        """
        if r.status_code != 200:
            self.response_type = ResponseType.ERR

        if len(r.text.encode("utf-8").splitlines()) > 1:
            self.response_mode = ResponseMode.MULTI

        for line in r.text.splitlines():
            match = self.pattern.search(line)
            if match is None:
                break
            print(match.group(1, 2))  # REMOVE testing
            self.response_type = ResponseType[match.group(1)]

      

+1


source







All Articles