個人常用集

Jason990420發表於2020-03-31

建立日期: 2020/03/31
更新日期: None
相關軟體版本

Win 10 Python 3.7.6 json 2.0.9 zlib 1.0 brotli 1.0.7
pathlib 3.4 PIL pillow 7.0.0 urllib (Python) http (Python)

說明:本文請隨意引用或更改,只須標示出處及作者,作者不保證內容絶對正確無誤,如造成任何後果,請自行負責.

主題: 個人常用集

有一些函式或方法常常會用到, 有很多引數啊, 情況要檢查的, 不是一行程式碼就能搞定的, 每次都要重複想一次, 建立一次, 所以建立了這個人常用集, 方便自己更簡單地呼叫. 使用方式如下, 以後會一直更新. 如果各位想到什麼常用的, 也請建議加入.

使用方式

from Tool import Read_File, Save_File, Read_URL

# txt file read and save
text = Read_File('file1.txt')
if text != None:
    Save_File('file2.txt', text)

# json file read and save
data = Read_File('file1.json')
if data != None:
    Save_File('file2.json', data)

# Picture file read and asve
image = Read_File('file1.png')
if data != None:
    Save_File('file2.png', image)

# URL read
html = Read_HTML(url)

匯入說明

from Tool import Read_File, Save_File, Read_URL
  1. Tool.py可以放在程式碼同層目錄中
  2. 也可以在sys.path中的路徑之一中, 放入Tool.py. (建議方式)
  3. 也可以在sys.path中的路徑之一, 建立一個Tool子目錄, 裡面再放一個__init__.py的檔案, 內容就是Tool.py的內容.
from Tool.Tool import Read_File, Save_File, Read_URL
  1. 在sys.path中的路徑之一, 建立一個Tool子目錄, 裡面再放一個__init__.py的空檔案, 再子目錄中再放上Tool.py

Tool.py 原始碼 (內容就不另外說明)

"""
Personal uility for common functions
Author: Jason Yang
Version: 0.0.1
Created Date: 2020/03/31
Revised Date: None
"""
import json
import zlib
import brotli
from pathlib import Path
from PIL import Image
from urllib import request, error
from urllib.parse import urlsplit
from http import client

def Signal(message):
    """
    Function as an failure interface for methods defined here. If you have
    different GUI, you can redefine it.
    : Parameters
      message: object, error message generally in string
    : Return - None
    """
    print(message+'\n')

def Read_File(filename, encoding='utf-8'):
    """
    Read any file and return the result. Method to open the file depend on
    the file extension.
    Legal file types (Continually updated)
        '.txt', '.json', '.jpg', '.bmp', '.png'
    :Parameter
      filename: path-like object.
      encoding: name of encoding, only be used in text mode.
    :Return
      None if failed, else depend on file extension
      '.txt'  - string
      '.json' - Python object
      image   - PIL Image object
    """
    path = Path(filename)
    if not path.is_file():
        Signal('FileNotFoundError')
        return None
    suffix = path.suffix
    if suffix in ['.txt', '.json']:
        try:
            with open(path, 'rt', encoding=encoding) as f:
                if suffix == '.txt':
                    data = f.read()
                elif suffix == '.json':
                    try:
                        data = json.load(f)
                    except:
                        Signal('JSONDecodeError')
        except:
            Signal('OpenError')
            return None
    elif suffix in ['.png', '.jpg', '.bmp']:
        try:
            data = Image.open(path)
        except:
            Signal('OpenError')
            return None
    else:
        Signal('FileTypeError')
        return None
    return data

def Save_File(filename, data, encoding='utf-8'):
    """
    Save any file. Method to read the file depend on the file extension.
    Legal file types (Continually updated)
        '.txt', '.json', '.jpg', '.bmp', '.png'
    :Parameter
      filename: path-like object.
      data    : data to write into file.
      encoding: name of encoding, only be used in text mode.
    :Return
      False if failed, else True
    """
    path = Path(filename)
    suffix = path.suffix
    if suffix in ['.txt', '.json']:
        try:
            with open(path, 'wt', encoding=encoding) as f:
                if suffix == '.txt':
                    f.write(data)
                elif suffix == '.json':
                    json.dump(data, f)
        except:
            Signal('OpenError')
            return False
    elif suffix in ['.png', '.jpg', '.bmp']:
        try:
            data.save(path)
        except:
            Signal('OpenError')
            return False
    else:
        Signal('FileTypeError')
        return False
    return True

def Read_URL(url, data=None, headers=None, encoding='utf-8'):
    """
    Read text from URL
    Compress method for gzip, deflate, br dealed internally.
    :Parameter
      url     : string or a Request object.
      data    : an object specifying additional data to be sent, or None.
      headers : dictionary, header of Http request entity.
      encoding: name of encoding to convert bytes into string.
    :Return
      None if failed, else string of html content
    """
    if not headers:
        url_base = urlsplit(url).netloc
        headers = {
            'Accept': ('text/html,application/xhtml+xml,application/xml;q=0.9,'
                       'image/webp,image/apng,*/*;q=0.8,application/signed-exc'
                       'hange;v=b3;q=0.9'),
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh-CN,zh-TW;q=0.9,zh;q=0.8,en;q=0.7',
            'Connection': 'keep-alive',
            'Host': f'{url_base}',
            'Sec-Fetch-Dest': 'document',
            'Sec-Fetch-Mode': 'navigate',
            'Sec-Fetch-Site': 'none',
            'Upgrade-Insecure-Requests': '1',
            'User-Agent': ('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWeb'
                           'Kit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.14'
                           '9 Safari/537.36')}

    req = request.Request(url, data=data, headers=headers)
    try:
        response = request.urlopen(req)
    except urllib.error.HTTPError as e:
        Signal('HTTPError')
        return None
    except urllib.error.URLError as e:
        Signal('URLError')
        return None
    if response.status != 200:
        Signal(client.responses[response.status])
        return None

    data = response.read()

    accept_encoding = response.headers['Content-Encoding']
    if accept_encoding == 'gzip':
        data = zlib.decompress(data, zlib.MAX_WBITS|16)
    elif accept_encoding == 'deflate':
        data = zlib.decompress(data, -zlib.MAX_WBITS)
    elif accept_encoding == 'br':
        data = brotli.decompress(data)
    html = data.decode(encoding)

    return html
本作品採用《CC 協議》,轉載必須註明作者和本文連結

Jason Yang

相關文章