python分页和session和计算时间差

分页

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 class Pagenation:
 5     def __init__(self,current_page,all_item,base_url,each):
 6         try:
 7             page = int(current_page)
 8         except:
 9             page = 1
10         if page < 1:
11             page = 1
12 
13         all_pager, c = divmod(all_item,each)
14         if c > 0:
15             all_pager += 1
16         self.each=each
17         self.current_page = page
18         self.all_pager = all_pager
19         self.base_url = base_url
20 
21     @property
22     def start(self):
23         return (self.current_page - 1) * self.each
24 
25     @property
26     def end(self):
27         return self.current_page * self.each
28 
29     def string_pager(self):
30         list_page = []
31         if self.all_pager < 11:
32             s = 1
33             t = self.all_pager + 1
34         else:  # 总页数大于11
35             if self.current_page < self.each+1:
36                 s = 1
37                 t = 12
38             else:
39                 if (self.current_page + 5) < self.all_pager:
40                     s = self.current_page - 5
41                     t = self.current_page + 5 + 1
42                 else:
43                     s = self.all_pager - 11
44                     t = self.all_pager + 1
45         # 首页
46         # first = '<a href="/index/1">首页</a>'
47         # list_page.append(first)
48         # 上一页
49         # 当前页 page
50         if self.current_page == 1:
51             prev = ''
52         else:
53             prev = '<a class="up" href="/index/%s">上一页</a>' % (self.current_page - 1,)
54         list_page.append(prev)
55         for p in range(s, t):  # 1-11
56             if p == self.current_page:
57                 temp = '<a class="active" href="/index/%s">%s</a>' % (p, p)
58             else:
59                 temp = '<a href="/index/%s">%s</a>' % (p, p)
60             list_page.append(temp)
61         if self.current_page == self.all_pager:
62             nex = ''
63         else:
64             nex = '<a  class="up" href="/index/%s">下一页</a>' % (self.current_page + 1,)
65 
66         list_page.append(nex)
67 
68         # # 尾页
69         # last = '<a href="/index/%s">尾页</a>' % (self.all_pager,)
70         # list_page.append(last)
71 
72         # 跳转
73         # jump = """<input type='text' /><a onclick="Jump('%s',this);">GO</a>""" % ('/index/')
74         script = """<script>
75             function Jump(baseUrl,ths){
76                 var val = ths.previousElementSibling.value;
77                 if(val.trim().length>0){
78                     location.href = baseUrl + val;
79                 }
80             }
81             </script>"""
82         # list_page.append(jump)
83         list_page.append(script)
84         str_page = "".join(list_page)
85         return str_page
View Code

Session

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 import hashlib
 4 import time
 5 
 6 CONTAINER = {
 7     "随机字符串":{}
 8 }
 9 # CONTAINER【随机字符串】[is_login ] =delattr()
10 
11 class Session:
12 
13     def __init__(self, handler):
14         # self.handler.set_secure_cookie
15         # self.handler.get_secure_cookie
16         self.handler = handler
17 
18     def __genarate_random_str(self, username):
19         obj = hashlib.md5()
20         obj.update(bytes(username + str(time.time()), encoding='utf-8'))
21         random_str = obj.hexdigest()
22         return random_str
23 
24     def set_value(self, username, key, value):
25         """
26         在Session中设置值
27         :param username:
28         :param key:
29         :param value:
30         :return:
31         """
32         # 生成随机字符串,并发送到浏览器 完成
33         # 本地生成
34         # {
35         #     随机字符串: {'is_login' : True}
36         # }
37         random_bytes = self.handler.get_secure_cookie("__session__")
38         if not random_bytes:
39             random_str = self.__genarate_random_str(username)
40             self.handler.set_secure_cookie('__session__', random_str)
41             CONTAINER[random_str] = {}
42         else:
43             random_str = str(random_bytes, encoding='utf-8')
44 
45         CONTAINER[random_str][key] = value
46 
47     def get_value(self, key):
48         """
49         在Session中获取值
50         :param key:
51         :return:
52         """
53         random_bytes = self.handler.get_secure_cookie("__session__")
54         if not random_bytes:
55             return None
56         else:
57             random_str = str(random_bytes, encoding='utf-8')
58         user_info = CONTAINER.get(random_str, None)
59         if not user_info:
60             return None
61         ret = user_info.get(key, None)
62         return ret
View Code

计算时间差,用途,显示几小时前or几天前发布

 1 #!/usr/bin/env python
 2 # -*- coding:utf-8 -*-
 3 
 4 import datetime
 5 import time
 6 def gap(old_time):
 7     # old_time为旧的时间戳
 8     now=int(time.time())
 9     gap=now-old_time
10     time_list=['','','']
11     if gap>60:
12         fen, miao = divmod(gap, 60)
13         if fen>=60:
14             shi,fen=divmod(fen,60)
15 
16             if shi>=24:
17                 day,shi=divmod(shi,24)
18                 time_list[0]='%d天'%day
19 
20             time_list[1]='%d小时' % shi
21 
22         time_list[2]= '%d分钟' % fen
23     else:
24         time_list.append('%d秒' % gap)
25         print(time_list)
26     return ''.join(time_list)
View Code

 

posted @ 2016-07-25 16:52  很大的龙  阅读(472)  评论(0编辑  收藏  举报