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


Python pysolr.force_unicode函数代码示例

本文整理汇总了Python中pysolr.force_unicode函数的典型用法代码示例。如果您正苦于以下问题:Python force_unicode函数的具体用法?Python force_unicode怎么用?Python force_unicode使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: test_force_unicode

    def test_force_unicode(self):
        self.assertEqual(force_unicode(b'Hello \xe2\x98\x83'), 'Hello ☃')
        # Don't mangle, it's already Unicode.
        self.assertEqual(force_unicode('Hello ☃'), 'Hello ☃')

        self.assertEqual(force_unicode(1), '1', "force_unicode() should convert ints")
        self.assertEqual(force_unicode(1.0), '1.0', "force_unicode() should convert floats")
        self.assertEqual(force_unicode(None), 'None', 'force_unicode() should convert None')
开发者ID:alfonsoeromero,项目名称:pysolr,代码行数:8,代码来源:test_client.py

示例2: test_safe_urlencode

 def test_safe_urlencode(self):
     self.assertEqual(
         force_unicode(unquote_plus(safe_urlencode({"test": "Hello ☃! Helllo world!"}))),
         "test=Hello ☃! Helllo world!",
     )
     self.assertEqual(
         force_unicode(unquote_plus(safe_urlencode({"test": ["Hello ☃!", "Helllo world!"]}, True))),
         "test=Hello \u2603!&test=Helllo world!",
     )
     self.assertEqual(
         force_unicode(unquote_plus(safe_urlencode({"test": ("Hello ☃!", "Helllo world!")}, True))),
         "test=Hello \u2603!&test=Helllo world!",
     )
开发者ID:janurag,项目名称:pysolr,代码行数:13,代码来源:client.py

示例3: _to_python

    def _to_python(self, value):
        """
        Converts values from Solr to native Python values.
        """
        if isinstance(value, (int, list, tuple, float, long, complex)):
            return value

        if value == 'true':
            return True
        elif value == 'false':
            return False

        is_string = False

        if isinstance(value, str):
            value = force_unicode(value)

        if isinstance(value, basestring):
            is_string = True

        if is_string == True:
            possible_datetime = DATETIME_REGEX.search(value)

            if possible_datetime:
                date_values = possible_datetime.groupdict()

                for dk, dv in date_values.items():
                    date_values[dk] = int(dv)

                return datetime.datetime(date_values['year'], date_values['month'], date_values['day'], date_values['hour'], date_values['minute'], date_values['second'])

        return value
开发者ID:mohitmehta93,项目名称:eventify,代码行数:32,代码来源:pysolr_override.py

示例4: put

 def put(self, data):
     '''
     Supports partial update of solr.
     '''
     if data is None or len(data) == 0:
         return
     # Update Solr: (Mostly from pysolr.Solr code.)
     # Generate the exact update command in xml -
     #  <add>
     #   <doc>
     #    <field name="id">1</field>
     #    <field name="memory_used" update="set">832</field>
     #   </doc>
     #  </add>
     data_xml = ET.Element('add')
     for doc_update in data: 
         doc_element = ET.Element('doc')
         id_field = ET.Element('field', **{'name':'id'})
         id_field.text = str(doc_update['id'])
         doc_element.append(id_field)
         for field in doc_update['fields']:
             field_xml = ET.Element('field', **{'name':field['name'], 'update':field['command']})
             field_xml.text = str(field['value'])
             doc_element.append(field_xml)
         data_xml.append(doc_element)
     # This returns a bytestring.
     data_xml_str = ET.tostring(data_xml, encoding='utf-8')
     # Convert back to Unicode.
     data_xml_str = pysolr.force_unicode(data_xml_str)
     try:
         solr = session.get_solr_interface(self.solr_url)
         solr._update(data_xml_str)
     except:
         LOG.exception('Failed to add to solr.')
         raise
开发者ID:StackStorm,项目名称:search,代码行数:35,代码来源:inventory.py

示例5: test__build_doc

 def test__build_doc(self):
     doc = {
         'id': 'doc_1',
         'title': 'Example doc ☃ 1',
         'price': 12.59,
         'popularity': 10,
     }
     doc_xml = force_unicode(ElementTree.tostring(self.solr._build_doc(doc), encoding='utf-8'))
     self.assertTrue('<field name="title">Example doc ☃ 1</field>' in doc_xml)
     self.assertTrue('<field name="id">doc_1</field>' in doc_xml)
     self.assertEqual(len(doc_xml), 152)
开发者ID:alfonsoeromero,项目名称:pysolr,代码行数:11,代码来源:test_client.py

示例6: test__build_doc_with_sets

 def test__build_doc_with_sets(self):
     doc = {
         'id': 'doc_1',
         'title': 'Set test doc',
         'tags': set(['alpha', 'beta']),
     }
     doc_xml = force_unicode(ElementTree.tostring(self.solr._build_doc(doc), encoding='utf-8'))
     self.assertTrue('<field name="id">doc_1</field>' in doc_xml)
     self.assertTrue('<field name="title">Set test doc</field>' in doc_xml)
     self.assertTrue('<field name="tags">alpha</field>' in doc_xml)
     self.assertTrue('<field name="tags">beta</field>' in doc_xml)
     self.assertEqual(len(doc_xml), 144)
开发者ID:django-haystack,项目名称:pysolr,代码行数:12,代码来源:test_client.py

示例7: add

def add(solr, docs, dsId, commit=True, boost=None, commitWithin="1000", waitFlush=None, waitSearcher=None):
    """
    Adds or updates documents.
    Requires ``docs``, which is a list of dictionaries. Each key is the
    field name and each value is the value to index.
    Optionally accepts ``commit``. Default is ``True``.
    Optionally accepts ``boost``. Default is ``None``.
    Optionally accepts ``commitWithin``. Default is ``None``.
    Optionally accepts ``waitFlush``. Default is ``None``.
    Optionally accepts ``waitSearcher``. Default is ``None``.
    Usage::
        solr.add([
                            {
                                "id": "doc_1",
                                "title": "A test document",
                            },
                            {
                                "id": "doc_2",
                                "title": "The Banana: Tasty or Dangerous?",
                            },
                        ])
    """
    start_time = time.time()
    #self.log.debug("Starting to build add request...")
    message = ET.Element('add')

    if commitWithin:
        message.set('commitWithin', commitWithin)

    for doc in docs:
        message.append(solr._build_doc(doc, boost=boost))

    # This returns a bytestring. Ugh.
    m = ET.tostring(message, encoding='utf-8')
    # Convert back to Unicode please.
    m = pysolr.force_unicode(m)
    #print "Indexing to: " + dsId
    end_time = time.time()
    #self.log.debug("Built add request of %s docs in %0.2f seconds.", len(message), end_time - start_time)
    return update(solr, m, dsId, commit=commit, waitFlush=waitFlush, waitSearcher=waitSearcher)
开发者ID:umitunal,项目名称:solr-for-datascience,代码行数:40,代码来源:setup.py

示例8: test_safe_urlencode

 def test_safe_urlencode(self):
     self.assertEqual(force_unicode(unquote_plus(safe_urlencode({'test': 'Hello ☃! Helllo world!'}))), 'test=Hello ☃! Helllo world!')
     self.assertEqual(force_unicode(unquote_plus(safe_urlencode({'test': ['Hello ☃!', 'Helllo world!']}, True))), "test=Hello \u2603!&test=Helllo world!")
     self.assertEqual(force_unicode(unquote_plus(safe_urlencode({'test': ('Hello ☃!', 'Helllo world!')}, True))), "test=Hello \u2603!&test=Helllo world!")
开发者ID:alfonsoeromero,项目名称:pysolr,代码行数:4,代码来源:test_client.py

示例9: _send_request

    def _send_request(self, method, path='', body=None, headers=None,
                      files=None):
        """
        Copy and paste of the base (pysolr version 3.2.0) _send_request()
        method except for the resp = requests_method() line, which
        passes along the auth information.

        """
        url = self._create_full_url(path)
        method = method.lower()
        log_body = body

        if headers is None:
            headers = {}

        if log_body is None:
            log_body = ''
        elif not isinstance(log_body, str):
            log_body = repr(body)

        self.log.debug("Starting request to '%s' (%s) with body '%s'...", url,
                       method, log_body[:10])
        start_time = time.time()

        try:
            requests_method = getattr(self.session, method, 'get')
        except AttributeError:
            err = "Unable to send HTTP method '{0}.".format(method)
            raise pysolr.SolrError(err)

        try:
            bytes_body = body

            if bytes_body is not None:
                bytes_body = pysolr.force_bytes(body)

            resp = requests_method(url, data=bytes_body, headers=headers,
                                   files=files, timeout=self.timeout,
                                   auth=self.auth)
        except requests.exceptions.Timeout as err:
            error_message = "Connection to server '%s' timed out: %s"
            self.log.error(error_message, url, err, exc_info=True)
            raise pysolr.SolrError(error_message % (url, err))
        except requests.exceptions.ConnectionError as err:
            error_message = "Failed to connect to server at '%s', are you " \
                            "sure that URL is correct? Checking it in a " \
                            "browser might help: %s"
            params = (url, err)
            self.log.error(error_message, *params, exc_info=True)
            raise pysolr.SolrError(error_message % params)

        end_time = time.time()
        self.log.info("Finished '%s' (%s) with body '%s' in %0.3f seconds.",
                      url, method, log_body[:10], end_time - start_time)

        if int(resp.status_code) != 200:
            error_message = self._extract_error(resp)
            data = {'data': {'headers': resp.headers, 'response': resp.content}}
            self.log.error(error_message, extra=data)
            raise pysolr.SolrError(error_message)

        return pysolr.force_unicode(resp.content)
开发者ID:AstroMatchDynamics,项目名称:MyJobs,代码行数:62,代码来源:seo_pysolr.py

示例10: test__build_doc

 def test__build_doc(self):
     doc = {"id": "doc_1", "title": "Example doc ☃ 1", "price": 12.59, "popularity": 10}
     doc_xml = force_unicode(ET.tostring(self.solr._build_doc(doc), encoding="utf-8"))
     self.assertTrue('<field name="title">Example doc ☃ 1</field>' in doc_xml)
     self.assertTrue('<field name="id">doc_1</field>' in doc_xml)
     self.assertEqual(len(doc_xml), 152)
开发者ID:janurag,项目名称:pysolr,代码行数:6,代码来源:client.py


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