ntuhgov/myutil.py
2024-12-11 12:46:09 +08:00

78 lines
1.6 KiB
Python
Executable file
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding=utf8
import pprint
import six
class MyPrettyPrinter(pprint.PrettyPrinter):
def format(self, object, context, maxlevels, level):
if six.PY2:
if isinstance(object, unicode):
return (object.encode("utf8"), True, False)
return pprint.PrettyPrinter.format(self, object, context, maxlevels, level)
def remove_space(s):
return s.replace(u" ", "").strip()
def minguo2ce(minguo):
pattern = "(\d+)\.([ 0-9]{1,2})\.([ 0-9]{1,2})"
s = re.search(pattern, minguo)
if s:
yy = int(s.group(1)) + 1911
try:
mm = int(s.group(2))
except:
mm = 1
try:
dd = int(s.group(3))
except:
dd = 1
return date(yy, mm, dd)
pattern = "(\d+)/([ 0-9]{1,2})/([ 0-9]{1,2})"
s = re.search(pattern, minguo)
if s:
yy = int(s.group(1)) + 1911
try:
mm = int(s.group(2))
except:
mm = 1
try:
dd = int(s.group(3))
except:
dd = 1
return date(yy, mm, dd)
return
def xstr(s):
return "" if s is None else s
def jprint(a):
import json
print(json.dumps(a, ensure_ascii=False, indent=4, sort_keys=True))
def PrintException():
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
print(
'EXCEPTION IN ({}, LINE {} "{}"): {}'.format(
filename, lineno, line.strip(), exc_obj
)
)