Python實現日周月排行榜

2021-08-29 18:13:27 字數 4148 閱讀 9153

根據各種時間模組和python內建函式實現,換算的步驟比較簡單,邏輯比較多,僅供參考。

寫的比較嘍,參考下思路就好,測試過,功能都能實現,自己可以去優化,用redis做下快取處理就好。這是用django框架實現的。直接懟**!

import calendar

import time

from datetime import datetime, timedelta, date

import datetime

from django.db.models import q, sum

from rest_framework.response import response

from rest_framework.views import apiview

from trade.models import usersettlementfee

from users.models import users

class rankinglistview(apiview):

"""排行榜"""

def get(self, request):

cycle_way = request.query_params.get('cycle_way')

if cycle_way is none:

return response()

rank_list =

if cycle_way == "day":

# 日排行榜

# time.strftime("%y-%m-%d", time.localtime(time.time())) -- 獲取當天的所有時間段(如:2018-10-23)

query_set = usersettlementfee.objects.filter(date=time.strftime("%y-%m-%d", time.localtime(time.time())))

for qs in query_set:

total_fee = qs.total_fee

self_fee = qs.self_fee

user_id = qs.user_id

ztotal_fee = (total_fee + self_fee) * 1000

user = users.objects.get(id=user_id)

bossuid = user.bossuid

invit = user.invit

moble = user.moble

data =

elif cycle_way == "week":

# 周排行榜

# time.strftime("%y-%m-%d", time.localtime((time.mktime(datetime.strptime(str(datetime.now() - timedelta(days=datetime.now().weekday())).split()[0],"%y-%m-%d").timetuple())) -- 獲取的是這週的周一的時間

timestamp1 = time.strftime("%y-%m-%d", time.localtime((time.mktime(datetime.strptime(str(datetime.now() - timedelta(days=datetime.now().weekday())).split()[0], "%y-%m-%d").timetuple())) - 24*2*60*60)).split("-")

timestamp2 = time.strftime("%y-%m-%d", time.localtime((time.mktime(datetime.strptime(str(datetime.now() - timedelta(days=datetime.now().weekday())).split()[0], "%y-%m-%d").timetuple())) + 24*5*60*60)).split("-")

query_set = usersettlementfee.objects.filter(q(date__gt=date(int(timestamp1[0]), int(timestamp1[1]), int(timestamp1[2]))) &q(date__lt=date(int(timestamp2[0]), int(timestamp2[1]), int(timestamp2[2]))))

for qs in query_set:

user_id = qs.user_id

total_fee = usersettlementfee.objects.filter(user_id=user_id).aggregate(sum('total_fee'))

self_fee = usersettlementfee.objects.filter(user_id=user_id).aggregate(sum('self_fee'))

user = users.objects.get(id=user_id)

bossuid = user.bossuid

invit = user.invit

moble = user.moble

data =

elif cycle_way == 'month':

# 月排行榜

t = time.localtime()

week, month_day = calendar.monthrange(t.tm_year, t.tm_mon - 1) # 獲取當前對應的星期幾和月份天數

today = datetime.datetime.now() # 獲取當前時間

offset = datetime.timedelta(days=-month_day) # 計算偏移量

re_date = ((today + offset).strftime('%y-%m-%d')).split("-")

query_set = usersettlementfee.objects.filter(q(date__gt=date(int(re_date[0]), int(re_date[1]), int(re_date[2]))) & q(date__lt=date(t.tm_year, t.tm_mon, t.tm_mday)))

for qs in query_set:

user_id = qs.user_id

total_fee = usersettlementfee.objects.filter(user_id=user_id).aggregate(sum('total_fee'))

self_fee = usersettlementfee.objects.filter(user_id=user_id).aggregate(sum('self_fee'))

user = users.objects.get(id=user_id)

bossuid = user.bossuid

invit = user.invit

moble = user.moble

data =

else:

return response()

rank_list = sorted(rank_list, key=lambda x: x['ztotal_fee'], reverse=true)

return response()

class usersettlementfee(models.model):

"""交易費率"""

create_at = models.datetimefield(auto_now=true, verbose_name='建立時間')

total_fee = models.decimalfield(max_digits=20, decimal_places=4, verbose_name='交易費率')

date = models.datefield(max_length=25, verbose_name='日期')

user_id = models.integerfield(verbose_name='使用者id')

data = models.charfield(max_length=255, verbose_name='買/賣費率當日總額')

self_fee = models.decimalfield(max_digits=20, decimal_places=4, verbose_name='自買自賣費率')

mysql製作排行榜 mysql實現排行榜

博主新人一枚,大家可以提出自己的寶貴意見。下來我們進入正題。大家首先要了解介面的場景,再就是排行榜的規則,我們這裡說的中國式排行榜。排行榜總結了一下分為3種 中國式排行 非中國式排行1 非中國式排行2 1 1 1 2 2 2 2 2 3 3 4 4 3 5 5 4 5 6 5 7 7 select ...

redis實現排行榜

排行榜功能是乙個很普遍的需求。設想在乙個遊戲中,有上百萬的玩家資料,如果現在需要你根據玩家的經驗值整理乙個前20名的排行榜,你會怎麼做呢?你不可能 order by limit 去實現 select from game socre order by score desc limit 0,20 使用 ...

Redis 實現排行榜

不再介紹資料庫做實時排行榜的弊端,直接介紹redis的有序集合的強大作用。有序集合的資料和集合一樣,不能重複,但每個元素又可以關聯乙個分數,這個分數可以重複。需要注意的是,redis版本和命令變化較大,注意執行環境。執行環境 redis 庫版本 3.3.11 redis版本 3.2.1 生成資料 i...