chinapnr / fishbase

自主开发、整理的一套 Python 基础函数库,涵盖 system 系统增强包、logger 日志记录增强包、file 文件处理增强包、 date 日期处理函数包、data 数据信息处理函数包、csv 处理增强函数包、crypt 加密/编码增强包等,可减少程序开发工作量、降低引用复杂度。
MIT License
94 stars 29 forks source link

fish_common,重新分类 #200

Open wingfish opened 5 years ago

wingfish commented 5 years ago

我看到现在 fish_common 中的函数有点杂,建议逐步重新分类。

itaa commented 5 years ago

好的,我们整理好后会放到该 issue 下

mindjun commented 5 years ago

fish_common.get_random_strfish_random.gen_random_str 重复,整理合并为 fish_random.gen_random_str, 输入:字符换最大、最小长度,前、后缀,结果字符串是否包含数字、字母、特殊字符 输出:满足条件的随机字符串

补充实现如下:


def gen_random_str(min_length, max_length, prefix=None, suffix=None, has_letter=True, has_digit=False, has_punctuation=False):
        """
        指定一个前后缀、字符串长度以及字符串包含字符类型,返回随机生成带有前后缀及指定长度的字符串

        :param:
            * min_length: (int) 字符串最小长度
            * max_length: (int) 字符串最小长度
            * prefix: (string) 字符串前缀
            * suffix: (string) 字符串后缀
            * has_letter: (bool) 字符串时候包含字母,默认为 True
            * has_digit: (bool) 字符串是否包含数字,默认为 False
            * has_punctuation: (bool) 字符串是否包含标点符号,默认为 False

        :return:
            * random_str: (string) 指定规则的随机字符串

        举例如下::

            print('--- gen_random_str demo ---')
            print(gen_random_str(5, 7))
            print(gen_random_str(5, 7, prefix='FISHBASE_'))
            print(gen_random_str(5, 7, prefix='FISHBASE_', suffix='.py'))
            print(gen_random_str(5, 7, has_digit=True, has_punctuation=True))
            print(gen_random_str(5, 7, prefix='FISHBASE_', has_digit=True, has_punctuation=True))
            print('---')

        执行结果::

            --- gen_string_by_range demo ---
            q4uo6E8
            FISHBASE_8uCBEUH
            FISHBASE_D4wRX2.py
            FISHBASE_65nqlNs
            FISHBASE_3"uFm$s
            ---
        """
        if not all([isinstance(min_length, int), isinstance(max_length, int)]):
            raise ValueError('min_length and max_length should be int, but we got {} and {}'.
                             format(type(min_length), type(max_length)))

        if min_length > max_length:
            raise ValueError('min_length should less than or equal to max_length')

        # 避免随机源为空
        if not any([has_letter, has_digit, has_punctuation]):
            raise ValueError('At least one value is True in has_letter, has_digit and has_punctuation')

        random_str_len = random.randint(min_length, max_length)

        random_source = ''
        random_source += string.ascii_letters if has_letter else ''
        random_source += string.digits if has_digit else ''
        random_source += string.punctuation if has_punctuation else ''

        # 避免出现 ValueError: Sample larger than population or is negative
        if random_str_len > len(random_source):
            random_source *= (random_str_len // len(random_source) + 1)

        mid_random_str = ''.join(random.sample(random_source, random_str_len))

        prefix = prefix if prefix else ''
        suffix = suffix if suffix else ''

        random_str = ''.join([prefix, mid_random_str, suffix])

        return random_str
itaa commented 5 years ago

fish_common 中的函数分类建议:

mindjun commented 5 years ago

计划将 fish_common.FishMD5, fish_common.FishSha256fish_common.Base64抽离,更新至 fish_crypt.py 模块中。

mindjun commented 5 years ago

计划整理以下 fish_common 模块中的方法: