个人常用集

相关软件版本

Win 10 Python 3.7.6

说明:本文请随意引用或更改,只须标示出处及作者,作者不保证内容絶对正确无误,如造成任何后果,请自行负责.

版本及更新

Personal uility for common functions
Author : Jason Yang
Create : 2020/03/31
Update : 2020/11/20
Version: 0.0.10

Revised:

  • 2020/05/20 - version 0.0.9
    • Add load_color_table for tkinter color name.
    • Add load_family_list for font families.
  • 2020/11/20 - version 0.0.10
    • Add types for nested variable types

主题: 个人常用集 (版本 0.0.10)

有一些函数或方法常常会用到, 有很多参数啊, 情况要检查的, 不是一行代码就能搞定的, 每次都要重复想一次, 建立一次, 所以建立了这个人常用集, 方便自己更简单地调用. 使用方式如下, 以后會一直更新. 如果各位想到什么常用的, 也请建议加入, 如有任何bug也请提示, 謝謝.

使用方式

from Tool import *

# color_convert
>>> color_convert("#80A0FF", to_hex=True)
'#80A0FF'
>>> color_convert("#80A0FF")
(128, 160, 255)
>>> color_convert((128, 160, 255), to_hex=True)
'#80A0FF'
>>> color_convert((128, 160, 255))
(128, 160, 255)
>>> color_convert("blue", to_hex=True)
'#0000FF'
>>> color_convert("blue")
(0, 0, 255)

# f-string
>>> books = 10
>>> fmt = 'I have {books} books'
>>> f(fmt)
'I have 10 books'

# filter_
>>> def func(a, b, c):
        return True if a<b or a>c else False
>>> filter_(func, [1,2,3,4,5,6,7,8,9,10], 5, 8)
[1, 2, 3, 4, 9, 10]

# flat
>>> flat([1,[2,3,4], [5,6,[7,8,9],10]])
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# get_substring
>>> text = '<link rel="dns-prefetch" href="https://github.githubassets.com">'
>>> get_substring(text, 'href="', '">')
'https://github.githubassets.com'

# len_
>>> a, b, c, d = 1, '123', [1,2,3], (1,2,3)
>>> len_(a), len_(b), len_(c), len_(d)
(1, 3, 3, 3,)

# load_color_table
>>> load_color_table()
{'AliceBlue': '#f0f8ff', 'AntiqueWhite': '#faebd7', ...,  'yellow2': '#eeee00', 'yellow3': '#cdcd00', 'yellow4': '#8b8b00'}

# load_family_list
>>> load_family_list()
('System', '@System', 'Terminal', ...,  'Open Sans Semibold', 'SimHei', '@SimHei')

# mapping
>>> def func(a, b, c):
...     return a*b+c
... 
>>> mapping(func, [1,2,3,4], 10, 5)
[15, 25, 35, 45]

# 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_URL(url, data=None, headers=None, encoding='utf-8', user='xxxx', password='xxxx', byte=False)

# transpose
>>> transpose([[1,2,3], [4,5,6], [7,8,9]])
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]

# class Thread
T = Thread(func, sequence, fail=None, size=40)
Manage jobs for threading by list.
: Parameters
  func - callable method to thread.
  sequence - list of tuple, each arguments for func.
  fail - callable method if func return Fail.
  size - max size of thread queue.
: Return
  Obejct of Thread manager.

# types
>>> types([123, '123', [1, '1'], (1, '1'), {1:10}, {5}, 1.3])
[<class 'int'>, <class 'str'>, [<class 'int'>, <class 'str'>], (<class 'int'>, <class 'str'>), <class 'dict'>, <class 'set'>, <class 'float'>]
>>> types([123, '123', [1, '1'], (1, '1'), {1:10}, {5}, 1.3]) == [int, str, [int, str], (int, str), dict, set, float]
True

导入说明

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 源代码

Tool.py on GitHub

本作品采用《CC 协议》,转载必须注明作者和本文链接
Jason Yang
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!