撸了个轮子 2FA 一次性密码 生成 算法 Python版

Pypi: https://pypi.python.org/pypi/pytotp/3

from pytotp import c2fa

# 生成一个密钥
key = c2fa.generate_secret_key()

# 生成密码
token = c2fa.oath_hotp(c2fa.b32_decode(key),c2fa.get_timestamp())

#验证密码
result = c2fa.verify_key(key,'123456')
if result:
    print("Successfully")
else:
    print("Wrong token")

Python解析并批量下载m3u8播放列表后自动合并

import os, sys, requests


def show_progress(processed,total):
    """
    简易进度条
    """
    percent=(int(processed)/int(total))*100

    bar_length=50
    hashes = '#' * int(percent/100.0 * bar_length)
    spaces = ' ' * (bar_length - len(hashes))
    sys.stdout.write('\rPercent: [%s] %d%%[%s/%s]'%(hashes+spaces,percent,processed,total))


lines = []

# 解析并获取所有需要下载的ts分段
with open('file.m3u8','r+') as f:
    for line in f.readlines():
        line=line.strip()
        if (line.endswith('ts')):
            url=line
            lines.append(url)

i=0
total=len(lines)

# 开始下载
for line in lines:
    filepath = line
    i=i+1
    if (not os.path.isfile(filepath)):
        url = 'URLPREFIX/{}'.format(line)
        data = requests.get(url)
        with open(filepath,'wb') as code:
            code.write(data.content)

    show_progress(i,total)

# 调用ffmpeg进行合并
os.system('ffmpeg -i file.m3u8 -c copy file.mkv')

SQLAlchemy 小计2 (索引)

针对PostgreSQL

  1. 函数索引
    class test_table(db.Model):
            username = db.Column(String(20))
    
            __table_args_ = (Index('idx_username', func.lower('username'), unique=True), )
            # 生成SQL如下:
            # create unique index idx_username on test_table  lower(username)
    
  2. 指定索引类型
    __table_args__ = (Index('idx_point', 'point', postgresql_using='gist'), )
    # 生成SQL:
    # create index idx_point on table_name using gist(point)
    
  3. 条件索引
    __table_args__ = (Index('idx_where', 'column', postgresql_where = model.c.column > 10), )
    # 生成SQL:
    # create index idx_where on table_name (column) where column > 10
    

SQLAlchmey 创建复合索引

关键字:__table_args__

class User(db.Model):
    """
    user model
    """
    __tablename__ = 'users'

    id = db.Column(BigInteger, primary_key=True)
    username = db.Column(String(30), nullable=False)
    password = db.Column(db.String(60), nullable=False)
    reg_dateline = db.Column(db.String(12), nullable=False)

    # table_args
        # 创建名为 idx_test 含字段 username和reg_dateline的 索引
    __table_args__ = ( Index('idx_test','username','reg_dateline') )

电商SKU之Python

itertools是Python内置标准库之一,itertools模块包含创建有效迭代器的函数,可以用各种方式对数据进行循环操作,此模块中的所有函数返回的迭代器都可以与for循环语句以及其他包含迭代器(如生成器和生成器表达式)的函数联合使用。在BigData分析处理上也会经常使用。

简单的笛卡尔乘积问题

import itertools

# 商品属性字典
attributes = [
    # 尺码属性
    ['S','M','L','XL','XXL'],
    # 颜色属性
    ['黑色','白色','灰色','天蓝黑'],
    # 季节属性
    ['春','夏','秋','冬']
]

skus = []
for i in itertools.product(*attributes):    
    skus.append(i)
print(skus)

输出结果:

[(‘S’, ‘黑色’, ‘春’), (‘S’, ‘黑色’, ‘夏’), (‘S’, ‘黑色’, ‘秋’), (‘S’, ‘黑色’, ‘冬’), (‘S’, ‘白色’, ‘春’), (‘S’, ‘白色’, ‘夏’), (‘S’, ‘白色’, ‘秋’), (‘S’, ‘白色’, ‘冬’), (‘S’, ‘灰色’, ‘春’), (‘S’, ‘灰色’, ‘夏’), (‘S’, ‘灰色’, ‘秋’), (‘S’, ‘ 灰色’, ‘冬’), (‘S’, ‘天蓝黑’, ‘春’), (‘S’, ‘天蓝黑’, ‘夏’), (‘S’, ‘天蓝黑’, ‘秋’), (‘S’, ‘天蓝黑’, ‘冬’), (‘M’, ‘黑色’, ‘春’), (‘M’, ‘黑色’, ‘夏’), (‘M’, ‘黑色’, ‘秋’), (‘M’, ‘黑色’, ‘冬’), (‘M’, ‘白色’, ‘春’), (‘M’, ‘白色’, ‘夏’), (‘M’, ‘ 白色’, ‘秋’), (‘M’, ‘白色’, ‘冬’), (‘M’, ‘灰色’, ‘春’), (‘M’, ‘灰色’, ‘夏’), (‘M’, ‘灰色’, ‘秋’), (‘M’, ‘灰色’, ‘冬’), (‘M’, ‘天蓝黑’, ‘春’), (‘M’, ‘天蓝黑’, ‘夏’), (‘M’, ‘天蓝黑’, ‘秋’), (‘M’, ‘天蓝黑’, ‘冬’), (‘L’, ‘黑色’, ‘春’), (‘L’, ‘ 黑色’, ‘夏’), (‘L’, ‘黑色’, ‘秋’), (‘L’, ‘黑色’, ‘冬’), (‘L’, ‘白色’, ‘春’), (‘L’, ‘白色’, ‘夏’), (‘L’, ‘白色’, ‘秋’), (‘L’, ‘白色’, ‘冬’), (‘L’, ‘灰色’, ‘春’), (‘L’, ‘灰色’, ‘夏’), (‘L’, ‘灰色’, ‘秋’), (‘L’, ‘灰色’, ‘冬’), (‘L’, ‘天蓝黑’, ‘春’), (‘L’, ‘天蓝黑’, ‘夏’), (‘L’, ‘天蓝黑’, ‘秋’), (‘L’, ‘天蓝黑’, ‘冬’), (‘XL’, ‘黑色’, ‘春’), (‘XL’, ‘黑色’, ‘夏’), (‘XL’, ‘黑色’, ‘秋’), (‘XL’, ‘黑色’, ‘冬’), (‘XL’, ‘白色’, ‘春’), (‘XL’, ‘白色’, ‘夏’), (‘XL’, ‘白色’, ‘秋’), (‘XL’, ‘白色’, ‘冬’), (‘XL’, ‘灰色’, ‘春’), (‘XL’, ‘灰色’, ‘夏’), (‘XL’, ‘灰色’, ‘秋’), (‘XL’, ‘灰色’, ‘冬’), (‘XL’, ‘天蓝黑’, ‘春’), (‘XL’, ‘天蓝黑’, ‘夏’), (‘XL’, ‘天蓝黑’, ‘秋’), (‘XL’, ‘天蓝黑’, ‘冬’), (‘XXL’, ‘黑色’, ‘春’), (‘XXL’, ‘黑色’, ‘夏’), (‘XXL’, ‘黑色’, ‘秋’), (‘XXL’, ‘黑色’, ‘冬’), (‘XXL’, ‘白色’, ‘春’), (‘XXL’, ‘白色’, ‘夏’), (‘XXL’, ‘白色’, ‘秋’), (‘XXL’, ‘白色’, ‘冬’), (‘XXL’, ‘灰色’, ‘春’), (‘XXL’, ‘灰色’, ‘夏’), (‘XXL’, ‘灰色’, ‘秋’), (‘XXL’, ‘灰色’, ‘冬’), (‘XXL’, ‘天蓝黑’, ‘春’), (‘XXL’, ‘天蓝黑’, ‘夏’), (‘XXL’, ‘天蓝黑’, ‘秋’), (‘XXL’, ‘天蓝黑’, ‘冬’)]

Gunicorn 部署 Flask

/application
├── app
│   ├── admin
│   │   ├── __init__.py
│   ├── api
│   │   ├── __init__.py
│   ├── exceptions.py
│   ├── home
│   │   ├── __init__.py
│   ├── __init__.py
│   ├── main
│   │   ├── errors.py
│   │   ├── __init__.py
│   │   └── views.py
│   ├── models.py
│   ├── shop
│   │   ├── __init__.py
│   └── views
│       ├── base_layout.html
│       ├── index.html
│       ├── password.html
│       └── pc.html
├── config.py
├── wsgi.py
# file: wsgi.py

from .app import create_app

# 此处的名字(app)后面会用到
app = create_app('development')

启动

./venv/bin/gunicorn -b :8080 -w8 --worker-class "gevent" wsgi:app <- 文件名称:变量名 不支持 gunicorn -b :8080 /var/www/dir/wsgi: 这种形式 必须是当前目录