Exemplo n.º 1
0
    def testProxyingToResponse(self):
        class FakeResponse(object):
            def __init__(self):
                self.geturl_called = False
                self.info_called = False
                self.iter_called = False
                self.next_called = False

            def geturl(self):
                self.geturl_called = True

            def info(self):
                self.info_called = True
                return {"key": "value"}

            def __iter__(self):
                self.iter_called = True

            def next(self):
                self.next_called = True

        result = FakeResponse()

        qr = QueryResult(result)
        qr.geturl()
        qr.__iter__()
        qr.next()

        self.assertTrue(result.geturl_called)
        self.assertTrue(result.iter_called)
        self.assertTrue(result.next_called)

        info = qr.info()
        self.assertTrue(result.info_called)
        self.assertEqual('value', info.__getitem__('KEY'), 'keys should be case-insensitive')
Exemplo n.º 2
0
    def testConstructor(self):
        qr = QueryResult('result')
        self.assertEqual('result', qr.response)
        try:
            format = qr.requestedFormat
            self.fail('format is not supposed to be set')
        except:
            pass

        qr = QueryResult(('result', 'format'))
        self.assertEqual('result', qr.response)
        self.assertEqual('format', qr.requestedFormat)
Exemplo n.º 3
0
    def testConstructor(self):
        qr = QueryResult("result")
        self.assertEqual("result", qr.response)
        try:
            format = qr.requestedFormat
            self.fail("format is not supposed to be set")
        except:
            pass

        qr = QueryResult(("result", "format"))
        self.assertEqual("result", qr.response)
        self.assertEqual("format", qr.requestedFormat)
Exemplo n.º 4
0
    def testProxyingToResponse(self):
        class FakeResponse(object):
            def __init__(self):
                self.geturl_called = False
                self.info_called = False
                self.iter_called = False
                self.next_called = False

            def geturl(self):
                self.geturl_called = True

            def info(self):
                self.info_called = True
                return {"key": "value"}

            def __iter__(self):
                self.iter_called = True

            def next(self):
                self.next_called = True

        result = FakeResponse()

        qr = QueryResult(result)
        qr.geturl()
        qr.__iter__()
        qr.next()

        self.assertTrue(result.geturl_called)
        self.assertTrue(result.iter_called)
        self.assertTrue(result.next_called)

        info = qr.info()
        self.assertTrue(result.info_called)
        self.assertEqual('value', info.__getitem__('KEY'), 'keys should be case-insensitive')
Exemplo n.º 5
0
        def _print_results(mime):
            """
            :param mime: mimetype/Content-Type of the response
            :return: number of warnings produced by combo
            """
            with warnings.catch_warnings(record=True) as w:
                qr = QueryResult(FakeResponse(mime))

                try:
                    qr.print_results()
                except:
                    pass

                return len(w)
Exemplo n.º 6
0
        def _print_results(mime):
            """
            @param mime: mimetype/Content-Type of the response
            @return: number of warnings produced by combo
            """
            with warnings.catch_warnings(record=True) as w:
                qr = QueryResult(FakeResponse(mime))

                try:
                    qr.print_results()
                except:
                    pass

                return len(w)
Exemplo n.º 7
0
        def _mime_vs_type(mime, requested_type):
            """
            @param mime:
            @param requested_type:
            @return: number of warnings produced by combo
            """
            with warnings.catch_warnings(record=True) as w:
                qr = QueryResult((FakeResponse(mime), requested_type))

                try:
                    qr.convert()
                except:
                    pass

                return len(w)
Exemplo n.º 8
0
        def _mime_vs_type(mime, requested_type):
            """
            @param mime:
            @param requested_type:
            @return: number of warnings produced by combo
            """
            with warnings.catch_warnings(record=True) as w:
                qr = QueryResult((FakeResponse(mime), requested_type))

                try:
                    qr.convert()
                except:
                    pass

                return len(w)
Exemplo n.º 9
0
        def _mime_vs_type(mime, requested_type):
            """
            :param mime: mimetype/Content-Type of the response
            :param requested_type: requested mimetype (alias)
            :return: number of warnings produced by combo
            """
            with warnings.catch_warnings(record=True) as w:
                qr = QueryResult((FakeResponse(mime), requested_type))

                try:
                    qr.convert()
                except:
                    pass

                return len(w)
Exemplo n.º 10
0
 def _convertXML(self):
     res = QueryResult._convertXML(self)
     boolean_dom = res.getElementsByTagName('boolean')
     b = boolean_dom[0].firstChild.nodeValue if boolean_dom else None
     results = res.getElementsByTagName('results')
     if results:
         results = results.pop()
         for r in results.getElementsByTagName("result"):
             elem = {}
             for binding in r.getElementsByTagName("binding"):
                 elem.update({binding.attributes["name"].value : getText(binding.childNodes[0].childNodes)})
             self.queue.put(elem)
     if b is None: self.queue.put('EOF')
     return b
Exemplo n.º 11
0
    def _convertJSON(self):
        res = QueryResult._convertJSON(self)
        ret = res.get('boolean', None)
        if 'results' in res:
            for x in res['results']['bindings']:
                for key, props in x.iteritems():
                    x[key] = props['value']

            reslist = res['results']['bindings']

            # Every tuple is added to the queue.
            for elem in reslist:
                self.queue.put(elem)
        if ret is None: self.queue.put('EOF')
        return ret
Exemplo n.º 12
0
def _to_df(result: Wrapper.QueryResult) -> pd.DataFrame:
    """

    :param result:
    :return: Dataframe
    """
    pd.set_option('display.max_columns', None)
    pd.set_option('display.max_colwidth', None)

    def format_value(res_value):
        value = res_value["value"]
        lang = res_value.get("xml:lang", None)
        datatype = res_value.get("datatype", None)
        if lang is not None:
            value += "@" + lang
        if datatype is not None:
            value += " [" + datatype + "]"
        return value

    results = result.convert()

    column_names = []
    for var in results["head"]["vars"]:
        column_names.append(var)
    df = pd.DataFrame(columns=column_names)

    values = []
    for r in results["results"]["bindings"]:
        row = []
        for col in results["head"]["vars"]:
            if col in r:
                result_value = format_value(r[col])
            else:
                result_value = None
            row.append(result_value)
        values.append(row)
    df = df.append(pd.DataFrame(values, columns=df.columns))

    return df
Exemplo n.º 13
0
 def __init__(self, *args, **kwargs):
     self.queue = kwargs.pop('queue', None)
     QueryResult.__init__(self, *args, **kwargs)