python分页和session和计算时间差
分页
#!/usr/bin/env python
# -*- coding:utf-8 -*- class Pagenation:
def __init__(self,current_page,all_item,base_url,each):
try:
page = int(current_page)
except:
page = 1
if page < 1:
page = 1 all_pager, c = divmod(all_item,each)
if c > 0:
all_pager += 1
self.each=each
self.current_page = page
self.all_pager = all_pager
self.base_url = base_url @property
def start(self):
return (self.current_page - 1) * self.each @property
def end(self):
return self.current_page * self.each def string_pager(self):
list_page = []
if self.all_pager < 11:
s = 1
t = self.all_pager + 1
else: # 总页数大于11
if self.current_page < self.each+1:
s = 1
t = 12
else:
if (self.current_page + 5) < self.all_pager:
s = self.current_page - 5
t = self.current_page + 5 + 1
else:
s = self.all_pager - 11
t = self.all_pager + 1
# 首页
# first = '<a href="/index/1">首页</a>'
# list_page.append(first)
# 上一页
# 当前页 page
if self.current_page == 1:
prev = ''
else:
prev = '<a class="up" href="/index/%s">上一页</a>' % (self.current_page - 1,)
list_page.append(prev)
for p in range(s, t): # 1-11
if p == self.current_page:
temp = '<a class="active" href="/index/%s">%s</a>' % (p, p)
else:
temp = '<a href="/index/%s">%s</a>' % (p, p)
list_page.append(temp)
if self.current_page == self.all_pager:
nex = ''
else:
nex = '<a class="up" href="/index/%s">下一页</a>' % (self.current_page + 1,) list_page.append(nex) # # 尾页
# last = '<a href="/index/%s">尾页</a>' % (self.all_pager,)
# list_page.append(last) # 跳转
# jump = """<input type='text' /><a onclick="Jump('%s',this);">GO</a>""" % ('/index/')
script = """<script>
function Jump(baseUrl,ths){
var val = ths.previousElementSibling.value;
if(val.trim().length>0){
location.href = baseUrl + val;
}
}
</script>"""
# list_page.append(jump)
list_page.append(script)
str_page = "".join(list_page)
return str_page
Session
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import hashlib
import time CONTAINER = {
"随机字符串":{}
}
# CONTAINER【随机字符串】[is_login ] =delattr() class Session: def __init__(self, handler):
# self.handler.set_secure_cookie
# self.handler.get_secure_cookie
self.handler = handler def __genarate_random_str(self, username):
obj = hashlib.md5()
obj.update(bytes(username + str(time.time()), encoding='utf-8'))
random_str = obj.hexdigest()
return random_str def set_value(self, username, key, value):
"""
在Session中设置值
:param username:
:param key:
:param value:
:return:
"""
# 生成随机字符串,并发送到浏览器 完成
# 本地生成
# {
# 随机字符串: {'is_login' : True}
# }
random_bytes = self.handler.get_secure_cookie("__session__")
if not random_bytes:
random_str = self.__genarate_random_str(username)
self.handler.set_secure_cookie('__session__', random_str)
CONTAINER[random_str] = {}
else:
random_str = str(random_bytes, encoding='utf-8') CONTAINER[random_str][key] = value def get_value(self, key):
"""
在Session中获取值
:param key:
:return:
"""
random_bytes = self.handler.get_secure_cookie("__session__")
if not random_bytes:
return None
else:
random_str = str(random_bytes, encoding='utf-8')
user_info = CONTAINER.get(random_str, None)
if not user_info:
return None
ret = user_info.get(key, None)
return ret
计算时间差,用途,显示几小时前or几天前发布
#!/usr/bin/env python
# -*- coding:utf-8 -*- import datetime
import time
def gap(old_time):
# old_time为旧的时间戳
now=int(time.time())
gap=now-old_time
time_list=['','','']
if gap>60:
fen, miao = divmod(gap, 60)
if fen>=60:
shi,fen=divmod(fen,60) if shi>=24:
day,shi=divmod(shi,24)
time_list[0]='%d天'%day time_list[1]='%d小时' % shi time_list[2]= '%d分钟' % fen
else:
time_list.append('%d秒' % gap)
print(time_list)
return ''.join(time_list)
python分页和session和计算时间差的更多相关文章
- python计算时间差的方法
本文实例讲述了python计算时间差的方法.分享给大家供大家参考.具体分析如下: 1.问题: 给定你两个日期,如何计算这两个日期之间间隔几天,几个星期,几个月,几年? 2.解决方法: 标准模块date ...
- python计算时间差
前言 之前写代码都是看打印的初始和结束时间然后自己算间隔时间,感觉总是不方便,这不符合python的优雅简洁,于是去寻找简便之道. 方法 time模块计算时间差 import time s_time ...
- Android计算时间差
想要写个根据消耗时长来确定开始结束时间的小工具,发现Android处理时间上有点累,可能是我没找到合适的方法吧,先把我的解决办法贴出来,有好的解决方法还希望提醒一下: 1.根据时间字符串获取毫秒数 p ...
- 【转载】c/c++在windows下获取时间和计算时间差的几种方法总结
一.标准C和C++都可用 1.获取时间用time_t time( time_t * timer ),计算时间差使用double difftime( time_t timer1, time_t time ...
- C#计算时间差值
/// <summary> /// 计算时间差值 /// </summary> /// <param name="DateTime1">< ...
- 登录超时自动退出,计算时间差-b
// 此方法适用于所有被创建过的controller,且当前controller生命周期存在,如有错误的地方望大神斧正 // 说一下我们的需求和实现原理,需求:在点击home键退出但没有滑飞它,5分 ...
- js计算时间差,包括计算,天,时,分,秒
收集两个计算时间差的计算方法代码片段: var date1=new Date(); //开始时间 var date2=new Date(); //结束时间 var date3=date2.getTim ...
- Android中计算时间差的实现方法
今天为“至简天气”增加了一项功能:在启动时根据上次更新数据的时间判断是否有必要更新数据,因为 weather.com.cn 的实况数据貌似是25分钟才会刷新一次,只有在据上次更新的时间达25分钟以上才 ...
- Python入门经典. 以解决计算问题为导向的Python编程实践
Python入门经典. 以解决计算问题为导向的Python编程实践(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1juLsew8UiOErRheQPOuTaw 提取 ...
随机推荐
- Python闭包与javascript闭包比较
实例一 python def line_conf(): def line(x): return 2*x+1 print(line(5)) # within the scope line_con ...
- 分析特定类的python脚本
今天接触了下pyUSB,事先没看对象内部成员资料,直接用python的dir函数看了看pyUSB的内部构成.突然间想到自己可不可以写个简单的脚本,利用dir或其他函数遍历某个对象内部的所有成员,并打印 ...
- 如何用chrome修改js代码,跳过网站等待时间
用chrome修改js代码 By Z.H. Fu 切问录 [maplewizard.github.io](http://maplewizard.github.io ) 网页中大部分的限制都是由js编写 ...
- NodeJS学习:爬虫小探补完计划
说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. 书接上回,我们需要修改程序以达到连续抓取40个页面的内容.也就是说我们需要输出每篇文章的标题.链接.第一条评论.评论用户和论坛 ...
- World’s Smallest h.264 Encoder
转载 http://www.cardinalpeak.com/blog/worlds-smallest-h-264-encoder/ View from the Peak World’s Smalle ...
- C++问题-UniqueAppObject.cpp(147): error C3861: “GUXClientInit”: 找不到标识符
问题经过:在同事的产品上增加新功能,拿来的代码包,用VS打开后,提示某个文件不存在,从项目中移除.CPP.H文件后,提示错误,提示如下:1>UniqueAppObject.cpp(147): e ...
- POJ 2502 Subway
Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4928 Accepted: 1602 Descriptio ...
- HDU 1702 http://acm.hdu.edu.cn/showproblem.php?pid=1702
#include<stdio.h> #include<string.h> #include<queue> #include<stack> #define ...
- Codeforces Round #271 (Div. 2) D. Flowers (递推)
题目链接:http://codeforces.com/problemset/problem/474/D 用RW组成字符串,要求w的个数要k个连续出现,R任意,问字符串长度为[a, b]时,字符串的种类 ...
- MFC编辑框换行实现
MFC中换行实现 在mfc中编辑框允许输入多行时,换行符被表示为<归位><换行>即“\r\n”,用ascii码表示为13 10 如果为编辑框中想要输入换行,就请将编辑框的属性: ...