推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
xmbaozi
V2EX  ›  Python

写了个 python 批量生成序列号,请批评,吐槽

  •  2
     
  •   xmbaozi · May 17, 2014 · 9715 views
    This topic created in 4405 days ago, the information mentioned may be changed or developed.


    如题。
    嗯,我是php码农
    Supplement 1  ·  May 17, 2014
    因为工作需要,经常要生成几万到几百万的不重复序列号。组成字符,长度都要求比较灵活
    18 replies    2014-05-19 09:40:11 +08:00
    binux
        1
    binux  
       May 17, 2014   ❤️ 1
    import uuid

    uuid.uuid1()
    uuid.uuid3(uuid.NAMESPACE_DNS, 'python.org')
    uuid.uuid4()
    uuid.uuid5(uuid.NAMESPACE_DNS, 'python.org')
    guoqiao
        2
    guoqiao  
       May 17, 2014
    @binux 不要这样打击别人嘛...
    imlonghao
        3
    imlonghao  
       May 17, 2014
    @binux 求不打击、
    byron
        4
    byron  
       May 17, 2014
    我错过了什么?
    leyle
        5
    leyle  
       May 17, 2014 via Android
    打击的体无完肤啊
    mengzhuo
        6
    mengzhuo  
       May 17, 2014   ❤️ 1
    写任何Python函数前都应该想想标准库哈哈哈哈
    xmbaozi
        7
    xmbaozi  
    OP
       May 17, 2014 via Android
    额,才知道有这个uuid
    不过用这个位数可控制吗
    写下生成多个的代码

    @binux
    qdcanyun
        8
    qdcanyun  
       May 17, 2014   ❤️ 1
    @xmbaozi
    uuid.uuid1().hex[:16]

    这是你源代码生成16位的 16可以替换成其他长度
    cbsw
        9
    cbsw  
       May 17, 2014   ❤️ 4
    Python 自带很多常用的电池。码代码之前先看看有没有用得着的电池,没有的话再看看 pypi 中有没有第三方的库,如果都没有或不能满足需要的话再考虑自己制造。

    当然上述原则适用于实际生产运用,如果是学习的话,完全可以上来不参考任何库的实现,自己造轮子,然后与标准轮子对比,学习别人的造轮技术
    tonic
        10
    tonic  
       May 17, 2014
    @binux 哈哈哈哈哈哈哈哈哈哈
    ericls
        11
    ericls  
       May 17, 2014
    就像又一次我不知道operator.countOf(a,b)
    ChanneW
        12
    ChanneW  
       May 17, 2014
    我看的很顺利,我也不知道 uuid ,看来我也很菜。
    ChanneW
        13
    ChanneW  
       May 17, 2014
    这种问题怎么避免?只能遇到问题再去搜,个人经验积累?
    cbsw
        14
    cbsw  
       May 18, 2014
    @ChanneW 在学习 python (或其它语言)过程中,在掌握了程序的基本写法之后,还有一个必须的过程就是将这种语言的标准库浏览一遍,对其有一个大致了解,具体详细的使用则不必看得太过仔细,等到需要用的时候就可以根据对标准库的印象去文档中相应章节查看。

    其实 Python 的官方教程在这方面做得很好,基本的东西告诉你之后,接下来就是 brief tour of standard library ,非常合理(PS:这也是 K&R 的经典 C 语言书的结构)

    不过我只是一个 Python 入门者,其它语言都没有深入学习过,所以这种学习经验没有在其它语言中实践过,感兴趣的话你可以在其它语言学习中试试,效果好的话可以告诉我哦 ^_^
    lm902
        15
    lm902  
       May 18, 2014
    (Math.random() + " ").substring(2)
    xmbaozi
        16
    xmbaozi  
    OP
       May 18, 2014 via Android
    @qdcanyun uuid生成,再截取长度就没法保证唯一性吧
    jamiesun
        17
    jamiesun  
       May 18, 2014   ❤️ 1
    提供一个明确时间规则的函数,用于订单号生成

    https://gist.github.com/jamiesun/d25d4680cdd79482fe60
    tongle
        18
    tongle  
       May 19, 2014   ❤️ 1
    @xmbaozi 应该是,截取之后碰撞的几率变大了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1042 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 69ms · UTC 18:53 · PVG 02:53 · LAX 11:53 · JFK 14:53
    ♥ Do have faith in what you're doing.