python练习5

import re
from datetime import datetime, timezone, timedelta


def to_timestamp(dt_str, tz_str):
    # 时间
    dt_now = datetime.strptime(dt_str, '%Y-%m-%d %H:%M:%S')
    #时区
    tz_num = int(re.match('w{3}([+|-]d+):00',tz_str).groups()[0])
    tz_utc = timezone(timedelta(hours = tz_num))
    #utc时间,强制设置时区
    dt_now = dt_now.replace(tzinfo=tz_utc)
    return dt_now.timestamp()


# 测试:
t1 = to_timestamp('2015-6-1 08:10:30', 'UTC+7:00')
assert t1 == 1433121030.0, t1

t2 = to_timestamp('2015-5-31 16:10:30', 'UTC-09:00')
assert t2 == 1433121030.0, t2

print('ok')



#请写一个能处理去掉=的base64解码函数:

#在python中字符串和字节编码序列之间的转换一定要用utf-8编码和解码
import base64


def safe_base64_decode(s):
    if len(s) % 4 == 0:
        return base64.b64decode(s)
    else:
        remain = 4 - (len(s) % 4)
        a = '='
        a *= remain
        s = s.decode('utf-8')
        s += a
    return base64.b64decode(s.encode('utf-8'))


# 测试:
assert b'abcd' == safe_base64_decode(b'YWJjZA=='), safe_base64_decode('YWJjZA==')
assert b'abcd' == safe_base64_decode(b'YWJjZA'), safe_base64_decode('YWJjZA')
print('ok')

# -*- coding: utf-8 -*-
import base64, struct


def bmp_info(data):
    info = struct.unpack('<ccIIIIIIHH',data[:30])
    if (info[0] , info[1]) == (b'B',b'M'):
        return {
            'width': info[6],
            'height': info[7],
            'color': info[9]
        }
    else:
        return 'NOT BMP FILE'


def bmp_check(file):
    with open(file,'rb') as f:
        info = struct.unpack('<ccIIIIIIHH',f.read(30))
        if (info[0], info[1]) == (b'B', b'M'):
            return {
                'width': info[6],
                'height': info[7],
                'color': info[9]
            }
        else:
            return 'NOT BMP FILE'


#print(struct.unpack('<ccIIIIIIHH',bmp_data[:30]))
# 测试
bi = bmp_check(r'')#文件路径
print(bi)
assert bi['width'] == 28
assert bi['height'] == 10
assert bi['color'] == 16
print('ok')

# -*- coding: utf-8 -*-
import hashlib
db = {
    'michael': 'e10adc3949ba59abbe56e057f20f883e',
    'bob': '878ef96e86145580c38c87f0410ad153',
    'alice': '99b1c2188db85afee403b1536010c2c9'
}


def login(user, password):
    md5 = hashlib.md5()
    md5.update(password.encode('utf-8'))
    return md5.hexdigest() == db[user]


# 测试:
assert login('michael', '123456')
assert login('bob', 'abc999')
assert login('alice', 'alice2008')
assert not login('michael', '1234567')
assert not login('bob', '123456')
assert not login('alice', 'Alice2008')
print('ok')

# -*- coding: utf-8 -*-
import hashlib, random


def get_md5(s):
    return hashlib.md5(s.encode('utf-8')).hexdigest()


class User(object):
    def __init__(self, username, password):
        self.username = username
        self.salt = ''.join([chr(random.randint(48, 122)) for i in range(20)])
        self.password = get_md5(password + self.salt)
db = {
    'michael': User('michael', '123456'),
    'bob': User('bob', 'abc999'),
    'alice': User('alice', 'alice2008')
}


def login(username, password):
    user = db[username]
    return user.password == get_md5(password+user.salt)


# 测试:
assert login('michael', '123456')
assert login('bob', 'abc999')
assert login('alice', 'alice2008')
assert not login('michael', '1234567')
assert not login('bob', '123456')
assert not login('alice', 'Alice2008')
print('ok')
原文地址:https://www.cnblogs.com/MalcolmMeng/p/8442958.html