Python中maxint与系统位数(32/64)的关系
来源:互联网 发布:higo出租软件 编辑:程序博客网 时间:2024/05/01 08:09
最近在学习Python的正则表达式时,需要批量生成一批随机数据。其中涉及到了模块sys、time中的maxint、ctime,代码在运行ctime格式化时间时候一直报错无法运行。经过对相关的资料的研究,发现maxint值在不同操作系统是不一样的,在64位操作系统下的maxint>2^32,而在32位操作系统maxint=2^32,ctime函数处理的秒数范围为0~2^32。
旧版本代码
#!/usr/bin/env pythonfrom random import randrange, choicefrom string import ascii_lowercase as lcfrom sys import maxint# 64 bit has a longer maxintfrom time import ctimetlds = ('com', 'edu', 'net', 'org', 'gov')for i in xrange(randrange(5, 11)): dtint = randrange(maxint) # pick date dtstr = ctime(dtint) # date string llen = randrange(4, 8) # login is shorter login =''.join(choice(lc) for j in range(llen)) dlen = randrange(llen,13) # domain is longer dom =''.join(choice(lc) for j in xrange(dlen)) print '%s::%s@%s.%s::%d-%d-%d' %(dtstr, login, dom, choice(tlds), dtint, llen, dlen)
报错提示
[hbu@localhost Python]$ python gendata.py Traceback (most recent call last): File "gendata.py", line 13, in <module> dtstr = ctime(dtint) # date stringValueError: unconvertible time
事例代码是由参考书提供的,理论上maxint获取的值没有问题,在ctime格式化会获得到相应的时间值,此处的报错本不应该发生。后来仔细查看ctime格式时间的范围限定在0~2^32。测试脚本位于CentOS7 64位系统,这样得到的maxint值为9223372036854775807,远大于2^32的值。dtint是由表达式 dtint = randrange(maxint)获取的,这样的dtint在很大概率上会得到大于2^32的值,超出了ctime的处理范围,所以就脚本一直在报错。
maxint值验证
>>> m = maxint>>> m9223372036854775807>>> 2**324294967296>>> 2**6418446744073709551616L
修改后新版代码
#!/usr/bin/env pythonfrom random import randrange, choicefrom string import ascii_lowercase as lc# from sys import maxint# 64 bit has a longer maxintfrom time import ctimetlds = ('com', 'edu', 'net', 'org', 'gov')for i in xrange(randrange(5, 11)): dtint = randrange(2**32) # pick date dtstr = ctime(dtint) # date string llen = randrange(4, 8) # login is shorter login =''.join(choice(lc) for j in range(llen)) dlen = randrange(llen,13) # domain is longer dom =''.join(choice(lc) for j in xrange(dlen)) print '%s::%s@%s.%s::%d-%d-%d' %(dtstr, login, dom, choice(tlds), dtint, llen, dlen)
修改后的代码很好的照顾了ctime可处理数值的上限,可得到如下结果:
[hbu@localhost Python]$ python gendata.py Thu Nov 4 02:38:42 1982::ymwvvf@peyutywmtfy.org::405254322-6-11Sun May 25 03:55:09 2008::wegvig@wttsvo.org::1211712909-6-6Thu Oct 19 14:00:00 2073::zgqgcbs@bqfglyza.net::3275672400-7-8Wed May 14 13:17:13 2003::ykpksq@raisjbw.edu::1052943433-6-7Thu Jan 1 00:37:41 2015::eifgiu@geypcbbwn.gov::1420101461-6-9Tue Jul 28 04:59:18 2043::tkjmit@eaqnbmrxzcd.org::2321697558-6-11Thu Dec 21 17:40:36 2034::stisf@wqwakqaho.net::2050364436-5-9Sun May 15 11:07:24 1983::ubydsn@gxbkjhftp.gov::421870044-6-9
0 0
- Python中maxint与系统位数(32/64)的关系
- Python中maxint与系统位数(32/64)的关系
- MaxInt
- 电脑内存与地址位数的关系
- 指针(内存大小和数据类型无关,和系统操作位数有关),对象,地址,和系统操作位数的关系
- 查看Linux系统的位数(64/32bit)
- python中类与函数的关系
- 系统位数和内存关系
- 操作系统位数与数据字节数的关系
- eclipse - 解决虚拟机与IDE位数不同时启动不了的问题(位数:32位/64位)
- 获得系统的位数(64 or 32)-几种方法
- linux下查看系统位数(32/64位)的方法
- python中numpy与matlab的对应关系
- Python中变量名与变量值的关系
- python中numpy与matlab的对应关系
- Oracle中系统process与session的关系
- uboot 与系统内核中 MTD分区的关系
- uboot 与系统内核中 MTD分区的关系
- 【项目管理和构建】十分钟教程,eclipse配置maven + 创建maven项目(二)
- 第十一天:制定进度计划、错题整理、进度压缩、资源平衡和资源平滑、关键路径和关键链对比
- 205. Isomorphic Strings
- 275. H-Index II
- 人的潜力和极限
- Python中maxint与系统位数(32/64)的关系
- nginx ssl配置
- 使用ImageNet在faster-rcnn上训练自己的分类网络
- 条件编译
- Codeforces 358 . B Alyona and Mex
- C#基础(2)
- Linux内核驱动之DDR3(一)寻址
- Python 资源大全中文版
- C/C++框架和库