|
|
| import os,time |
| import numpy as np |
| from pathlib import Path |
| class ch_cwd_to_this_file: |
| def __init__(self, _code_file_path): |
| self._code_file_path = _code_file_path |
| def __enter__(self): |
| self._old_dir = os.getcwd() |
| cwd=os.path.dirname(os.path.abspath(self._code_file_path)) |
| os.chdir(cwd) |
| def __exit__(self, exc_type, exc_val, exc_tb): |
| os.chdir(self._old_dir) |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import datetime |
| import pytz |
| def beijing_datetime()->datetime.datetime: |
| """ |
| Example: print(f'Current Beijing time = {beijing_time:%Y.%m.%d %H:%M:%S}') |
| """ |
| |
| local_tz = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo |
| |
| beijing_tz = pytz.timezone('Asia/Shanghai') |
| |
| now = datetime.datetime.now() |
| |
| local_time = now.astimezone(local_tz) |
| |
| beijing_time:datetime.datetime = local_time.astimezone(beijing_tz) |
| return beijing_time |
| def beijing_str_A( os_is_windows=False)->str: |
| """ |
| print( beijing_str_A() ) |
| """ |
| ret= f"{beijing_datetime():%m.%d-%H:%M:%S}" |
| if os_is_windows: |
| ret=ret.replace(':',':') |
| return ret |
|
|
|
|
|
|
|
|
|
|
| |
| import json |
| import numpy |
| import PIL |
| import torch |
| from torch import Tensor |
|
|
|
|
| def to_list_to_primitive(obj): |
| if isinstance(obj, numpy.ndarray): |
| return obj.tolist() |
| if isinstance(obj, torch.Tensor): |
| return obj.cpu().data.numpy().tolist() |
| if isinstance(obj, list): |
| return [to_list_to_primitive(i) for i in obj] |
| |
| |
| elif (isinstance(obj, numpy.int32) or |
| isinstance(obj, numpy.int64) or |
| isinstance(obj, numpy.float32) or |
| isinstance(obj, numpy.float64)): |
| return obj.item() |
| elif (isinstance(obj, int) or |
| isinstance(obj, float) |
| ): |
| return obj |
| else: |
| raise TypeError("got {}".format(type(obj))) |
| def to_ndarray(x): |
| if isinstance(x, numpy.ndarray): |
| return x |
| if isinstance(x, torch.Tensor): |
| return x.cpu().data.numpy() |
| if isinstance(x, list): |
| return numpy.array(x) |
| if isinstance(x, PIL.Image.Image): |
| return numpy.array(x) |
| |
| |
| raise TypeError("got {}".format(type(x))) |
|
|
| def to_tensor(x): |
| if isinstance(x, numpy.ndarray): |
| return torch.from_numpy(x) |
| if isinstance(x, torch.Tensor): |
| return x |
| if isinstance(x, PIL.Image.Image): |
| return torch.from_numpy(numpy.array(x)) |
| if isinstance(x, list): |
| return torch.tensor(x) |
| |
| |
| raise TypeError("got {}".format(type(x))) |
| def to_pil(x): |
| import torch |
| if isinstance(x, PIL.Image.Image): |
| return x |
| if isinstance(x, numpy.ndarray): |
| return PIL.Image.fromarray(x) |
| if isinstance(x, torch.Tensor): |
| return PIL.Image.fromarray(x.cpu().data.numpy()) |
| raise TypeError("got {}".format(type(x))) |
|
|
|
|
| class myJSONEncoder(json.JSONEncoder): |
| def default(self, obj): |
| if isinstance(obj, numpy.ndarray): |
| return obj.tolist() |
| if isinstance(obj, Tensor): |
| return obj.cpu().data.numpy().tolist() |
| elif (isinstance(obj, numpy.int32) or |
| isinstance(obj, numpy.int64) or |
| isinstance(obj, numpy.float32) or |
| isinstance(obj, numpy.float64)): |
| return obj.item() |
| elif isinstance(obj,Path): |
| return str(obj) |
| return json.JSONEncoder.default(self, obj) |
|
|
| if(__name__=="__main__"): |
| import torch |
| dic = {'x': torch.randn(2, 3), 'rec': numpy.array([[11, 22, 33], [44, 55, 66], [77, 88, 99]])} |
| s_dic=json.dumps(dic , cls=myJSONEncoder, |
| sort_keys=True, indent=2, |
| separators=(',', ': '), ensure_ascii=False) |
| with open('test.json', 'w', encoding='utf8') as f: |
| json.dump(dic,f, |
| |
| sort_keys=False, |
| indent=2, separators=(',', ': '), ensure_ascii=False) |
| |
| |
|
|
| def truncate_str(string:str,MAX_LEN:int,suffix_if_truncate="......")->str: |
| assert isinstance(string,str) |
| if len(string)> MAX_LEN: |
| string=string[:MAX_LEN]+suffix_if_truncate |
| return string |
| def map_string_to_int(string,MIN,MAX): |
| """ |
| Map strings evenly into [MIN, MAX] |
| """ |
| assert isinstance(MIN,int) |
| assert isinstance(MAX,int) |
| assert MAX-MIN>=2 |
| |
| sum = 0 |
| for char in string: |
| sum += ord(char) |
| |
| ret=2**sum |
| ret += sum |
| ret=ret%(MAX-MIN) |
| ret+=MIN |
| return ret |
| if 0: |
| import pprint |
| def print_optimizer(optimizer): |
| state_dict=optimizer.state_dict() |
| param_groups=state_dict['param_groups'] |
| |
| pprint.pprint(param_groups) |
|
|
|
|
| def dic_key_str_2_int(dic: dict) -> dict: |
| ret = {} |
| for k, v in dic.items(): |
| if isinstance(k, str) and k.isdigit(): |
| k = int(k) |
| ret[k] = v |
| return ret |
| def dic_key_str_2_int__nested(dic: dict) -> dict: |
| ret = {} |
| for k, v in dic.items(): |
| if isinstance(k, str) and k.isdigit(): |
| k = int(k) |
| if isinstance(v, dict): |
| v = dic_key_str_2_int__nested(v) |
| ret[k] = v |
| return ret |
| def dic_list_2_tuple_nested(dic: dict) -> dict: |
| ret = {} |
| for k, v in dic.items(): |
| if isinstance(k, list): |
| k = tuple(k) |
| if isinstance(v, list): |
| v = tuple(v) |
| if isinstance(v, dict): |
| v = dic_list_2_tuple_nested(v) |
| ret[k] = v |
| return ret |
|
|
|
|
| import re |
|
|
| def inverse_fstring(string:str,fmt:str,): |
| """ |
| Inverse of string format in python |
| from https://stackoverflow.com/questions/48536295/inverse-of-string-format-in-python |
| """ |
| reg_keys = '{([^{}:]+)[^{}]*}' |
| reg_fmts = '{[^{}:]+[^{}]*}' |
| pat_keys = re.compile(reg_keys) |
| pat_fmts = re.compile(reg_fmts) |
|
|
| keys = pat_keys.findall(fmt) |
| lmts = pat_fmts.split(fmt) |
| temp = string |
| values = [] |
| for lmt in lmts: |
| if not len(lmt)==0: |
| value,temp = temp.split(lmt,1) |
| if len(value)>0: |
| values.append(value) |
| if len(temp)>0: |
| values.append(temp) |
| return dict(zip(keys,values)) |
| def sort_strings_asc_A(l:list,fmt:str)->list: |
| """ |
| fmt: eg. 'home/frame{d}.png' |
| """ |
| ret=sorted(l, key= lambda s:int( inverse_fstring(s, fmt )['d']) ) |
| return ret |
| from natsort import natsorted |
| def ls_natsort(folder,re_="*"): |
| folder = Path(folder) |
| files = list(folder.glob(re_)) |
| return natsorted(files ) |
| return natsorted(files, key=lambda x: x.name) |
|
|
|
|
|
|
| if __name__=='__main__': |
| print( beijing_str_A() ) |
| if 1: |
| fmt = '{k1:}+{k2:}={k:3}' |
| res = '1+1=2' |
| print (inverse_fstring(res,fmt)) |
|
|
| fmt = '{name:} {age:} {gender}' |
| res = 'Alice 10 F' |
| print (inverse_fstring(res,fmt)) |
|
|
| fmt = 'Hi, {k1:}, this is {k2:}' |
| res = 'Hi, Alice, this is Bob' |
| print (inverse_fstring(res,fmt)) |
|
|