Python notes, python
Recently, when I was using MongoDB, I encountered a scenario where I used multiple keywords for fuzzy search. Zhu Feng uses the producer engine library.
I checked various materials and finally summarized the useful methods. First, go to the code, and then describe it in detail. As follows:
1 #! /Usr/bin/env python 2 # coding: UTF-8 3 4 import re 5 import Runtime Engine 6 from Runtime Engine import * 7 8 runtime engine. register_connection ('default', 'test', host = '2017. 0.0.1 ') 9 10 class TestData (Document): 11 name = StringField () 12 content = StringField () 13 14 TestData. objects. get_or_create (name = 'Heaven and earth Xuan Huang ', ults = {'content': 'abc123'}) 15 TestData. objects. get_or_create (name = 'cosmic preferences ', ults = {'content': 'abc123'}) 16 TestData. objects. get_or_create (name = 'daily upstream ', ults = {'content': 'abc123'}) 17 18 def print_arr (obj): 19 print obj. name, obj. content20 21 def fuzzy_query_by_contains (): 22 print "\ n ### use container engine contains to query" 23 print "# contains case sensitive:" 24 test_data_list = TestData. objects (content _ contains = 'abc123') 25 map (print_arr, test_data_list) 26 27 print "# icontains are case insensitive:" 28 test_data_list = TestData. objects (content _ icontains = 'abc123') 29 map (print_arr, test_data_list) 30 31 def fuzzy_query_by_Q (): 32 print "\ n ### Use Q to query" 33 test_data_list = TestData. objects (34 Q (name _ icontains = u'heaven and Earth ') | Q (name _ icontains = u'cosmic') 35 map (print_arr, test_data_list) 36 37 def fuzzy_query_by_pymongo (): 38 print "\ n ### use raw queries, new in version 0.4 "39 print" # single query condition "40 search = {41 '_ raw _': {42 'content': {'$ regex ': 'A \ S + \ d + '}, 43}, 44} 45 test_data_list = TestData. objects (** search) 46 map (print_arr, test_data_list) 47 48 print "# multiple query conditions" 49 search = {50' _ raw __': {51 'name': {'$ in': [re. compile (u'day'), re. compile (u'cosmic ')]}, 52}, 53} 54 test_data_list = TestData. objects (** search) 55 map (print_arr, test_data_list) 56 57 if _ name _ = '_ main _': 58 fuzzy_query_by_contains () 59 fuzzy_query_by_Q () 60 fuzzy_query_by_pymongo ()
First, we will discuss the fuzzy_query_by_contains method. Here we use the contains operation provided by the runtime engine. It is worth noting that the contains are case-sensitive, while the icontains are case-insensitive. This method is especially convenient for fuzzy search of a keyword.
Then there is the fuzzy_query_by_Q method, which combines contains and Q for combined query. When Q () is used for combined queries, bitwise operators (| and &) must be used instead of or and for logical operations. This method is suitable for determining the number of keywords. If the number of keywords is variable, this method will be slightly tangled.
Zhu Feng's problem in fuzzy search of dynamic keywords is also long-winded. He almost had to query each keyword separately and then get the result of intersection. Later, we found in the document that the runtime engine has the _ raw _ parameter, which can be used to query PyMongo (new function provided by version 0.4 ). After several tests, the fuzzy_query_by_pymongo method was released.
PyMongo supports regular expressions and provides two methods: $ regex and re. compile ().
In the example, perform A fuzzy query on A single keyword. The code is: {'$ regex': 'A \ S + \ d + '}
The following code queries multiple keywords: {'$ in': [re. compile (u'day'), re. compile (u'cosmic ')]}
Modify the code to accept multiple keywords. The Code is as follows:
1 def fuzzy_query_by_pymongo (): 2 print "# multiple query conditions" 3 keyword = u'everyday cosmic '4 search = {' _ raw _ ': {'name ': {'$ in': map (re. compile, keyword. split () }}5 test_data_list = TestData. objects (** search) 6 map (print_arr, test_data_list)
In this example, get_or_create is used to create data. A tuples are returned. The first element is the object for creating or querying, and the second element is whether the data is created successfully. The recommended usage in this document is as follows:
1 >>> a, created = User.objects.get_or_create(name='User A', defaults={'age': 30})2 >>> b, created = User.objects.get_or_create(name='User A', defaults={'age': 40})3 >>> a.name == b.name and a.age == b.age4 True
The final result is the running result of the example. The returned results may be in a slightly different order, so you don't have to worry about it.
1 $ python mongodb_test.py 2 3 ### use mongoins of engine for query 4 # contains case sensitive: 5 heaven and earth Xuan Huang abc123 6 # icontains are case insensitive: 7 heaven and earth Xuan Huang abc123 8 universe flood ABC123 9 every day up Abc12310 11 ### Use Q to query 12 heaven and earth Xuan Huang abc12313 universe flood ABC12314 15 ### use raw queries, new in version 0.416 # single query condition 17 cosmic flood ABC12318 daily up Abc12319 # multiple query conditions 20 cosmic flood ABC12321 daily up Abc123
Error Message
Can Python run perfectly in a notebook?
As long as any electronic device has the following features:
1. CPU (no matter what, including the mobile phone's ARM chip)
2. Run this intelligent operating system (no matter what it is, as long as it is an intelligent system)
Then this electronic device can run Python
Don't worry about the laptop. You can use it on your mobile phone ~
I want to learn Python ~ Convenient, simple, practical, and powerful ~~