misc.py 文件源码

python
阅读 25 收藏 0 点赞 0 评论 0

项目:happinesspackets 作者: mxsasha 项目源码 文件源码
def readable_random_token(alphanumeric=False, add_spaces=False, short_token=False, long_token=False):
    """
    Generate a random token that is also reasonably readable.

    Generates 4 segments of 4 characters, seperated by dashes. Can either use digits only (default),
    or non-confusing letters and digits (alphanumeric=True). If add_spaces is set, spaces are added
    around the groups. This is intended to prevent mobile phones that see e.g. "-3" as an emoticon.
    If short_token is set, the token is two segments of four characters.
    """
    segments = 4
    if short_token:
        segments = 2
    if long_token:
        segments = 8
    if alphanumeric:
        allowed_chars = "BCDFGHJLKMNPQRSTVWXYZ23456789"
    else:
        allowed_chars = "1234567890"
    elements = [get_random_string(length=4, allowed_chars=allowed_chars) for i in range(segments)]
    join_str = "-"
    if add_spaces:
        join_str = " - "
    return join_str.join(elements)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号