refactoring some code and added docstring

added stopwatch.py(modify from utils.timer.py) for test
This commit is contained in:
jeffy-g
2017-04-04 21:23:28 +09:00
parent 8fd424814a
commit e9e8ef964c
4 changed files with 206 additions and 62 deletions

107
utils/stopwatch.py Normal file
View File

@@ -0,0 +1,107 @@
# coding: utf-8
import time
import os
# 2017/04/05:
class Stopwatch(object):
"""
--- on python console ---
import re
from utils.stopwatch import Stopwatch
# measurement re.sub
def m_re_sub(t, set_count, executes, texts):
t.reset()
while set_count:
set_count -= 1
with t:
while executes:
executes -= 1
ret = re.sub("[a|s]+", "-", texts)
# stat string
return str(t)
# measurementor
stpwth = Stopwatch("test")
# statistics loop: 1000(exec re.sub: 100000)
m_re_sub(stpwth, 1000, 100000, "asdfadsasdaasdfadsasda")
----------- records -----------
text: "asdfadsasda"
'elapsed record(ms): min=0.000602411446948, max=220.85578571'
'elapsed record(ms): min=0.000602411446948, max=217.331377504'
text: "asdfadsasdaasdfadsasda"
'elapsed record(ms): min=0.000602411446948, max=287.784902967'
'elapsed record(ms): min=0.000602411432737, max=283.653264016'
NOTE: about max
The value is large only at the first execution,
Will it be optimized, after that it will be significantly smaller
"""
# time.clock() is μs? 1/1000ms
# https://docs.python.jp/2.7/library/time.html#time.clock
_tfunc = time.clock if os.name == "nt" else time.time
def __init__(self, name='', logger=None):
self.name = name
self.start = Stopwatch._tfunc()
self.__last = self.start
# __last field is means last checkpoint system clock value?
self.logger = logger
self.reset()
@property
def stat(self):
# :return: (float, float)
return (self.min, self.max)
@property
def elapsed(self):
# :return: time as ms
return (Stopwatch._tfunc() - self.start) * 1000
@property
def last(self):
return self.__last * 1000
def __update_stat(self, v):
# :param v: unit of ms
if self.min == 0.0: self.min = v
if self.max < v: self.max = v
if self.min > v: self.min = v
def checkpoint(self, name=''):
span = self.elapsed
self.__update_stat(span)
text = u'Stopwatch("{tname}") - {checkpoint} - {last:.6f}ms ({elapsed:.12f}ms elapsed)'.format(
tname=self.name,
checkpoint=unicode(name, "utf-8"),
last=self.last,
elapsed=span
).strip()
self.__last = Stopwatch._tfunc()
if self.logger:
self.logger.debug(text)
else:
print(text)
def reset(self):
self.min = 0.0
self.max = 0.0
def __enter__(self):
self.start = Stopwatch._tfunc()
return self
def __exit__(self, type, value, traceback):
# https://docs.python.org/2.7/reference/datamodel.html?highlight=__enter__#object.__exit__
# If the context was exited without an exception, all three arguments will be None
self.checkpoint('finished')
# ex: "type=None, value=None, traceback=None"
# print "type=%s, value=%s, traceback=%s" % (type, value, traceback)
return True
def __repr__(self):
return "elapsed record(ms): min=%s, max=%s" % self.stat

View File

@@ -5,30 +5,26 @@ import re
def sequential_rep(text_, *args):
# type: (basestring, *list) -> basestring
"""
params
text_: string content
args : <pattern>, <replacement>, <pattern>, <replacement>, ...
return
empty string when text_ length was zero or invalid.
:param text_: string content
:param args: like <pattern>, <replacement>, <pattern>, <replacement>, ...
:return: if text_ length was zero or invalid parameters then no manipulation to text_
"""
if not text_ or not len(text_):
return ""
arg_len = len(args)
i = 0
while i < arg_len:
text_ = re.sub(args[i], args[i + 1], text_)
i += 2
if arg_len % 2 == 0 and isinstance(text_, basestring) and len(text_) > 0:
i = 0
while i < arg_len:
text_ = re.sub(args[i], args[i + 1], text_)
i += 2
return text_
def replaceLTGT(text_):
def replace_ltgt(text_):
# type: (basestring) -> basestring
"""if fit name contained "<" or ">" then reprace to named html entity by EVE client.
for fit name.
:param text_: string content of fit name from exported by EVE client.
:return: if text_ is not instance of basestring then no manipulation to text_.
"""
return text_.replace("&lt;", "<").replace("&gt;", ">") if isinstance(text_, unicode) else text_
return text_.replace("&lt;", "<").replace("&gt;", ">") if isinstance(text_, basestring) else text_