1.验证码生成器


from string import ascii_uppercase, ascii_lowercase, digits
import random

def get_check_code(len_, mode = (True, True, True)):
    '''check code creater.
    len_ : length of check code.
    mode : tuple type, default (upper=True, lower=True, digit=True).
    example:
    >>> get_check_code(6)
        mCX90t
    >>> get_check_code(4, (False, False, True))
        4820
    >>> get_check_code(4, (True, False, False))
        XMDK
    '''

    if not isinstance(len_, int) or len(mode) != 3:
        raise ValueError
    mode = (True, True, True) if set(mode) == {False} else mode
    chars = ''
    for m, c in zip(mode, (ascii_uppercase, ascii_lowercase, digits)):
        if m:
            chars += c
    return ''.join([random.choice(chars) for i in range(len_)])

if __name__ == '__main__':

    print(get_check_code(4, (True, True, False)))
    print(get_check_code(4, (False, False, True)))
    print(get_check_code(6))



----------

输出:
scFs
2585
npRCAb

统计纯英文文本行数、单词量以及每个单词出现次数

import re 

d = {}
linescount = 0
wordscount = 0
file_text = '/home/duxu/textname.txt'

with open(file_text,'r') as r_file:
    for line in r_file:
        linescount += 1
        words = re.findall(r'[a-zA-Z0-9]+',line)
        for word in words:
            if word not in d:
                d[word] = 1 
            else:
                d[word] += 1
    wordscount = len(d)

print('words:%d\nlines:%d' % (wordscount, linescount))
Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐