The relationship between Maxint in Python and the number of system bits (32/64) __python

Source: Internet
Author: User
Tags in python
The recent study of Python's regular expressions requires batch generation of random data. This involves to the module SYS, time in the Maxint, CTime, the code in the run CTime format times has been the error can not run.
After studying the relevant data, it is found that the Maxint value is different in the operating system, the MAXINT>2^32 is under the 64-bit operating system, and the number of seconds in the 32-bit operating system Maxint=2^32,ctime function is 0~2^32.
old version Code
#!/usr/bin/env python

from random import randrange, choice from
string import ascii_lowercase as LC from
SYS i Mport maxint
Bit has a longer maxint from time
import ctime

TLDs = (' com ', ' edu ', ' net ', ' org ', ' gov ') 
  for i in Xrange (Randrange (5, one)):
    dtint = Randrange (maxint)     # pick Date
    dtstr = CTime (dtint)          # da Te string
    llen = Randrange (4, 8)        # login is shorter 
    login = '. Join (choice (LC) to J in range (Llen))
    DL En =  randrange (llen,13)    # domain is longer
    dom = '. Join (choice (LC) to J in Xrange (Dlen))
    print '%s: :%s@%s.%s::%d-%d-%d '% (dtstr, login,
        DOM, Choice (TLDs), Dtint, Llen, Dlen)
Error Alert
[Hbu@localhost python]$ Python gendata.py 
traceback (most recent call last):
  File ' gendata.py ', line, in <mo dule>
    dtstr = CTime (dtint)          # date string
valueerror:unconvertible time

Case code is provided by reference books, theoretically maxint get the value of no problem, in the CTime format will get to the corresponding time value, the error here should not occur. Later, a careful view of the CTime format time range is limited to 0~2^32. The test script is located in the CentOS7 64-bit system, so the Maxint value is 9223372036854775807, much larger than the 2^32 value. Dtint is obtained by the expression Dtint = Randrange (maxint), so the dtint will get a greater probability than the 2^32 value, beyond the scope of the CTime processing, so the script has been the error. maxint Value Validation

>>> m = maxint
>>> m
9223372036854775807
>>> 2**32
4294967296
>>> 2**64
18446744073709551616L
Modified new Code
#!/usr/bin/env python

from random import randrange, choice from
string import ascii_lowercase as LC
# from SYS Import Maxint
# bit has a longer maxint from time
import ctime

= (' com ', ' edu ', ' net ', ' org ', ' gov ') C6/>for i in Xrange (Randrange (5, one)):
    dtint = Randrange (2**32)     # pick Date
    dtstr = CTime (dtint)          # date S Tring
    Llen = Randrange (4, 8)        # login is shorter 
    login = '. Join (choice (LC) to J in range (Llen))
    Dlen =  Randrange (llen,13)    # domain is longer
    dom = '. Join (choice (LC) to J in Xrange (Dlen))
    print '%s::% s@%s.%s::%d-%d-%d '% (dtstr, login,
        DOM, Choice (TLDs), Dtint, Llen, Dlen)

The modified code takes care of the upper limit of the number of ctime that can be processed, and the following results are obtained:

[hbu@localhost python]$ Python gendata.py Thu Nov 4 02:38:42 1982::ymwvvf@peyutywmtfy.org: : 405254322-6-11 Sun may 03:55:09 2008::wegvig@wttsvo.org::1211712909-6-6 Thu Oct 19 14:00:00 2073:: Zgqgcbs@bqfglyza.net::3275672400-7-8 Wed May 13:17:13 2003::ykpksq@raisjbw.edu::1052943433-6-7 Thu 1 00:37:41 201 5::eifgiu@geypcbbwn.gov::1420101461-6-9 Tue 04:59:18 2043::tkjmit@eaqnbmrxzcd.org::2321697558-6-11 Thu Dec 21
17:40:36 2034::stisf@wqwakqaho.net::2050364436-5-9 Sun May 11:07:24 1983::ubydsn@gxbkjhftp.gov::421870044-6-9 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.