Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to optimize the list interface for paging by Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Editor to share with you Python how to optimize the list interface for paging, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!

The purpose of paging

Let's assume that there are 1W pieces of data added to the use case library, and if you want to display them in a list, you will return 1W pieces of data at once.

This has two drawbacks:

1. The interface written in this way will be slow. Think about what to do if this number becomes 10W or 100W over time.

two。 Not friendly to the front end. Rendering 1W pieces of data with a front end would not be done by a rational developer.

Therefore, the purpose of paging is mainly to improve the performance and improve the user experience.

My side of the development language is Python,Web framework using Tornado, background database using Mongodb.

Interface design

First of all, let's design the interface as follows:

HTTP URL: / api/admin/v1/case/listHTTP Method: GETRequest ParamsReponse Data {"code": 0, "message": null, "data": {"cases": [{"method": str, "url": str, "request_data"?: str, "request_params"?: str "header": dict, "reponse_data"?: str},...], "count": int}}

Return data structure parsing:

The value of cases is a list of multiple case objects

Count refers to the total number of case, so that the front end can determine how many pages there are through count, and then directly request the data of the corresponding page for display through the number of pages clicked. This can effectively control the amount of data returned and the return time, and improve the user's sense of experience.

According to the above interface convention, the implementation of the interface is as follows (only the key code is shown):

Class CaseList (APIHandler): async def get_handler (self): page = self.input.page or 1 limit= self.input.limit or 12 page, limit= int (page), int (limit) skip= limit * (page-1) return {'customers': await CaseService.get_case_list (filter_= {}, skip=skip, limit=limit) Interpretation of 'count': await CaseService.get_count (filter_= {})} Interface

Let's interpret this interface line by line.

1. According to the API convention, page and limit do not need to be passed and have default values, so you can directly write it as self.input.page or 1 when you get it, where self.input.page is None when page is not passed.

two。 The request parameter of the GET request obtained by the backend is String, so you need to convert int () to an integer. Here, you can directly int (page), because after the previous processing, page must have a value.

3. The number of skips is calculated through limit * (page-1). The function of describing skip in vernacular is to start with the skip object, fetch limit later, and return.

The values of the filter_ parameters of the 4.get_case_list method and the get_count method should be the same, because they both operate on the object case, but the count takes the total.

CaseService.get_case_list method async def get_case_list (filter_: dict, skip: int = None, limit: int = None)-> case [list]]: "" get case list "" sort= [('create_time',-1)] if skip is not None and limit is not None: ret = await cls.find (' case', filter=filter_, sort=sort, limit=limit " Skip=skip) else: ret = await cls.find ('case', filter=filter_, sort=sort) return ret

1. Is not None is used to judge the null value of skip and limit, because skip may be, if you use if skip to judge, skip=0 will also enter the branch.

The introduction of 2.sort = [('create_time',-1)] causes the returned list to be arranged backwards according to the creation time. If you don't do this, I believe the test classmate in charge will ask you an "ease of use question".

3.sort + skip+limit has execution priority, and their priority is sort → skip → limit,skip+limit, and the priority is skip → limit.

Here, we should not consider whether sort, skip and limit will be automatically executed according to priority when pymogo is executed. Just pass parameters according to priority when passing parameters, and form good habits without worrying about other things.

CaseService.get_count method async def get_count (cls, filter_: dict)-> int: "" get the total number of case "return await ModelHandler.count ('case', filter=filter_)"

Here, using count to get the data, you can get the total directly, instead of taking out the list and then asking for length, avoiding the waste of memory space.

The above is all the contents of the article "how to optimize the list interface for paging by Python". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report