python functions, modules knowledge using examples

  1. Codes given length n, generates a random codes, codes consisting of numbers, letters (Reference CHR () built-in method)

    # 给定验证码长度n,生成随机验证码,验证码由数字、字母组成(参考chr()内置方法)
    # 第33~126号(共94个)是字符,其中第48~57号为0~9十个阿拉伯数字;65~90号为26个大写英文字母
    # 97~122号为26个小写英文字母,其余为一些标点符号、运算符号等。
    import random
    def identify_code(n):
        icode =''
        for i in range(n):
            # s1=chr(random.randint(65,90))
            # s2=chr(random.randint(48,57))
            s1 = chr(random.randrange(65, 90,1))
            s2 = chr(random.randrange(97, 126, 1))
            # s2 = chr(random.randrange(48, 57,1))
            # s2 = str(random.randrange(0, 9, 1))
            s3 = str(random.randint(0, 9))
            icode +=random.choice([s1,s2,s3])
        return icode
    print(identify_code(9))
  2. Print progress bar, a progress bar pattern shown below, the following code as the reference code

```python

========= ========== knowledge base

The effect of the progress bar

[# ]
[## ]
[### ]
[#### ]

Specifies the width

print('[%-15s]' %'#')
print('[%-15s]' %'##')
print('[%-15s]' %'###')
print('[%-15s]' %'####')

print%

print ( '% s %%'% (100)) #% No. canceled and the second represents the first special meaning of%

Parameter passing width can be controlled

print('[%%-%ds]' %50) #[%-50s]
print(('[%%-%ds]' %50) %'#')
print(('[%%-%ds]' %50) %'##')
print(('[%%-%ds]' %50) %'###')

import sys
import time

def progress(percent,width=50):
if percent >= 1:
percent=1
show_str=('[%%-%ds]' %width) %(int(widthpercent)'#')
print('\r%s %d%%' %(show_str,int(100*percent)),file=sys.stdout,flush=True,end='')

========= ========== Application

1025 = Data_Size
recv_size = 0
the while recv_size Data_Size <:
transmission time.sleep (0.1) # analog data delay
recv_size + = 1024 # 1024 each received

percent=recv_size/data_size #接收的比例
progress(percent,width=70) #进度条的宽度70

Guess you like

Origin www.cnblogs.com/abdm-989/p/11359918.html