MCPcopy
hub / github.com/wechaty/python-wechaty / find_all

Method find_all

src/wechaty/user/contact.py:222–267  ·  view source on GitHub ↗

Find all contacts based on query, which can be string, ContactQueryFilter, or callable function. Args: query: the query body to build filter Examples: >>> # 1. find contacts based query string, will match one of: contact_id, weixin, name and

(cls: Type[Contact],
                       query: Optional[Union[str, ContactQueryFilter, Callable[[Contact], bool]]] = None
                       )

Source from the content-addressed store, hash-verified

220
221 @classmethod
222 async def find_all(cls: Type[Contact],
223 query: Optional[Union[str, ContactQueryFilter, Callable[[Contact], bool]]] = None
224 ) -> List[Contact]:
225 """
226 Find all contacts based on query, which can be string, ContactQueryFilter, or callable<filter> function.
227
228 Args:
229 query: the query body to build filter
230
231 Examples:
232 >>> # 1. find contacts based query string, will match one of: contact_id, weixin, name and alias
233 >>> # what's more, contact_id and weixin will follow extract match, name and alias will follow fuzzy match
234 >>> Contact.find_all('your-contact-id/weixin') # find: <your-contact-id> == contact.contact_id
235 >>> Contact.find_all('name/alias') # find: <name> in contact.name
236
237 >>> # 2. find contacts based ContactQueryFilter object, will match all fields
238 >>> query = ContactQueryFilter(id='your-contact-id', weixin='weixin') # find: <your-contact-id> == contact.contact_id
239 >>> query = ContactQueryFilter(name='your-contact-name') # find: <your-contact-name> in contact.name
240 >>> Contact.find_all(query)
241
242 >>> # 3. find contacts based on callable query function
243 >>> def filter_contacts(contact: Contact) -> bool:
244 >>> if contact.contact_id == "your-contact-id":
245 >>> return True
246 >>> return False
247 >>> Contact.find_all(filter_contacts)
248
249 Returns:
250 Contact: the contact object filtered by query
251 """
252 log.info('find_all() <%s, %s>', cls, query)
253
254 # 1. load contacts with concurrent tasks
255 contact_ids: List[str] = await cls.get_puppet().contact_list()
256
257 contacts: List[Contact] = [cls.load(contact_id) for contact_id in contact_ids]
258 tasks: List[Task] = [asyncio.create_task(contact.ready()) for contact in contacts]
259 await gather_with_concurrency(PARALLEL_TASK_NUM, tasks)
260
261 # 2. filter contacts
262 if not query:
263 return contacts
264
265 contacts = cls._filter_contacts(contacts, query)
266
267 return contacts
268
269 async def ready(self, force_sync: bool = False) -> None:
270 """

Callers 1

findMethod · 0.45

Calls 6

gather_with_concurrencyFunction · 0.90
contact_listMethod · 0.80
get_puppetMethod · 0.80
_filter_contactsMethod · 0.80
loadMethod · 0.45
readyMethod · 0.45

Tested by

no test coverage detected