五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

Python 常用工具類

2023-04-09 20:07 作者:夕林泉石  | 我要投稿

'''

? ? 文? ? 件 : util.py

? ? 說? ? 明 : 常用工具

? ? 作? ? 者 : 李光強

? ? 時? ? 間 : 2022/4/2/

'''



import os

import re

import sys

import json

import uuid

import random

import hashlib


from datetime import datetime


class Util:

? ? '''常用工具'''

? ??

? ? def root_path(self):

? ? ? ? '''

? ? ? ? Get the physical path of the web root.

? ? ? ? '''

? ? ? ? # Infer the root path from the run file in the project root (e.g. manage.py)

? ? ? ? fn = getattr(sys.modules['__main__'], '__file__')

? ? ? ? root_path = os.path.abspath(os.path.dirname(fn))

? ? ? ? return root_path


? ? def to_float(self,s):

? ? ? ? '''Convert the input string to a float'''

? ? ? ? try:

? ? ? ? ? ? return float(s);

? ? ? ? except ValueError:

? ? ? ? ? ? return False;

? ? ? ??

? ? def to_int(self,s):

? ? ? ? '''Convert the input string to a int'''

? ? ? ? try:

? ? ? ? ? ? return int(s);

? ? ? ? except ValueError:

? ? ? ? ? ? return False;

? ? ? ??

? ? def to_date(self,s,fmt=None):

? ? ? ? '''Convert the input string to a date

? ? ? ? ? ? @參數(shù) s : 輸入日期字符串

? ? ? ? ? ? @參數(shù) fmt: 輸入的日期字符串格式,如%Y-%m-%d

? ? ? ? '''

? ? ? ? if(not fmt):

? ? ? ? ? ? fmt='%Y-%m-%d';

? ? ? ? print("s=",s,",fmt=",fmt);

? ? ? ? try:? ? ? ? ? ??

? ? ? ? ? ? return datetime.strptime(s,fmt);

? ? ? ? except ValueError:

? ? ? ? ? ? return False;


? ? def list_json(self,list):

? ? ? ? '''

? ? ? ? Convert the list of objects to json string.

? ? ? ? '''

? ? ? ? s = '[';

? ? ? ? for x in list:

? ? ? ? ? ? if s=='[':

? ? ? ? ? ? ? ? s = s+json.dumps(x.__dict__);

? ? ? ? ? ? else:

? ? ? ? ? ? ? ? s = s+','+json.dumps(x.__dict__);

? ? ? ? ? ??

? ? ? ? s = s+"]";

? ? ? ??

? ? ? ? return s;

? ??

? ? def random(self,start,end):

? ? ? ? '''生成在start~end之間的正整數(shù)隨機數(shù)'''? ? ? ??

? ? ? ? r = random.random();

? ? ? ? return int(start+(end-start)*r);

? ??

? ? def dict_obj(self,d):

? ? ? ? '''

? ? ? ? Convert the dictionary to object.

? ? ? ? '''??

? ? ? ? # checking whether object d is a

? ? ? ? # instance of class list

? ? ? ? if isinstance(d, list):

? ? ? ? ? ? d = [self.dict_obj(x) for x in d]?

? ??

? ? ? ? # if d is not a instance of dict then

? ? ? ? # directly object is returned

? ? ? ? if not isinstance(d, dict):

? ? ? ? ? ? return d

? ??

? ? ? ? # declaring a class

? ? ? ? class C:

? ? ? ? ? ? pass

? ??

? ? ? ? # constructor of the class passed to obj

? ? ? ? obj = C()

? ??

? ? ? ? for k in d:

? ? ? ? ? ? obj.__dict__[k] = self.dict_obj(d[k])

? ??

? ? ? ? return obj

? ??

? ? def gbk_utf8(s):

? ? ? ? '''GBK轉(zhuǎn)讓UTF8'''

? ? ? ? n = s.decode('gbk');

? ? ? ? return n.encode('utf8');



? ? # Function to print sum

? ? def dict_haskey(self, dict, key):

? ? ? ? '''

? ? ? ? 檢查dict里是否包括key

? ? ? ? 參數(shù):

? ? ? ? ? ? dict - 要查找的字典

? ? ? ? ? ? key? - 關(guān)鍵字

? ? ? ? '''? ? ??

? ? ? ? if key in dict.keys():

? ? ? ? ? ? return True;

? ? ? ? else:

? ? ? ? ? ? return False;


? ? def var_type(self,var):

? ? ? ? '''

? ? ? ? 獲取變量類型

? ? ? ??

? ? ? ? Arg:

? ? ? ? ? ? var 變量

? ? ? ? return:

? ? ? ? ? ? string

? ? ? ? ? ? ? ? str - 字符串

? ? ? ? ? ? ? ? int - 整型

? ? ? ? ? ? ? ? float - 浮點型

? ? ? ? ? ? ? ? list

? ? ? ? ? ? ? ? complex

? ? ? ? ? ? ? ? tuple

? ? ? ? ? ? ? ? dict

? ? ? ? ? ? ? ? bool

? ? ? ? ? ? ? ? bytes

? ? ? ? ? ? ? ? bytearray

? ? ? ? ? ? ? ? time.struct_time

? ? ? ? ? ? ? ? datetime.datetime

? ? ? ? ? ? ? ??

? ? ? ? '''

? ? ? ? s=str(type(var));

? ? ? ? # 取單引號里的字符串

? ? ? ? pattern = re.compile("'(.*)'", re.S)

? ? ? ? m = re.search(pattern,s);? ? ? ??

? ? ? ? return m.group(1).strip();


? ? def guid(self):

? ? ? ? '''create a guid'''

? ? ? ? return uuid.uuid1();


? ? def guid_withoutdash(self):

? ? ? ? '''

? ? ? ? create a guid without dash

? ? ? ? '''

? ? ? ? return self.myuuid_nodash();


? ? def myuuid(self):

? ? ? ? '''

? ? ? ? create a uuid

? ? ? ? '''

? ? ? ? return uuid.uuid1();


? ? def myuuid_nodash(self):

? ? ? ? '''

? ? ? ? create a uuid without dashes

? ? ? ? '''

? ? ? ? return str(uuid.uuid1()).replace('-','');

? ??

? ? def md5(self,plain_text):

? ? ? ? '''MD5加密'''? ? ? ??

? ? ? ? # 創(chuàng)建md5對象

? ? ? ? hl = hashlib.md5()


? ? ? ? # Tips

? ? ? ? # 此處必須聲明encode

? ? ? ? # 若寫法為hl.update(str)? 報錯為: Unicode-objects must be encoded before hashing

? ? ? ? hl.update(plain_text.encode(encoding='utf-8'))

? ? ? ? return hl.hexdigest();


Python 常用工具類的評論 (共 條)

分享到微博請遵守國家法律
志丹县| 广汉市| 碌曲县| 文成县| 含山县| 桐梓县| 富宁县| 达州市| 板桥市| 两当县| 赫章县| 通榆县| 鹰潭市| 桃江县| 宁陕县| 镶黄旗| 拉萨市| 浠水县| 克什克腾旗| 长岛县| 子洲县| 尚义县| 紫金县| 承德市| 成都市| 会昌县| 五莲县| 囊谦县| 广丰县| 乌兰浩特市| 息烽县| 河南省| 罗平县| 新巴尔虎左旗| 桐梓县| 福泉市| 呼伦贝尔市| 惠州市| 白银市| 延吉市| 班玛县|