Difference between revisions of "Implementing a Standard Library"
Jump to navigation
Jump to search
PeterHarding (talk | contribs) |
PeterHarding (talk | contribs) |
||
Line 188: | Line 188: | ||
#========================================================================== | #========================================================================== | ||
</pre> | |||
=A Test Harness= | |||
Use this to test the performiq module... | |||
<pre> | |||
$ cat tst_module.py | |||
#!/usr/bin/env python | |||
import time | |||
import performiq | |||
from performiq import Logger | |||
debug_level = 0 | |||
verbose_flg = True | |||
print "performiq version [%s]" % performiq.VERSION | |||
print "Set timer - %5.3f" % performiq.Timer.init() | |||
Logger.Init(debug=True) | |||
Logger.Info("Testing - Info...") | |||
Logger.Warning("Testing - Warning...") | |||
Logger.Error("Testing - Error...") | |||
time.sleep(3) | |||
print "Get timer - %5.3f" % performiq.Timer.time() | |||
</pre> | </pre> | ||
Revision as of 12:11, 20 February 2013
Components
I use the following for building up utility scripts.
$ ls -lR ~/lib/py /home/pharding/lib/py: total 4 drwxr-xr-x+ 1 pharding Users 0 Feb 20 11:44 performiq ./performiq: total 7 -rw-r--r-- 1 pharding Users 108 Feb 20 11:06 __init__.py -rw-r--r-- 1 pharding Users 23 Feb 20 10:52 CONSTANTS.py -rw-r--r-- 1 pharding Users 361 Feb 20 10:55 Enum.py -rw-r--r-- 1 pharding Users 2121 Feb 20 11:32 Logger.py -rw-r--r-- 1 pharding Users 2121 Feb 20 11:32 Timer.py
env | grep PYTHONPATH PYTHONPATH=/home/pharding/lib/py
__init__.py
$ cat __init__.py import CONSTANTS from CONSTANTS import VERSION from Enum import Enum from Logger import Logger from Timer import Timer
CONSTANTS.py
$ cat CONSTANTS.py VERSION = "1.0.0"
Enum.py
$ cat Enum.py #========================================================================== class Enum(set): pass #-------------------------------------------------------------------- def __getattr__(self, name): if name in self: return name raise AttributeError #==========================================================================
Logger.py
$ cat Logger.py import os import sys import logging #========================================================================== class Logger: logger = None debug = False @classmethod def Info(cls, msg): global debug_level, verbose_flg if not cls.logger: cls.Init() cls.logger.info(' ' + msg) if cls.debug: sys.stderr.write("[%s::INFO] %s\n" % (cls.name, msg)) #---------------------------------------------------------------------- @classmethod def Error(cls, msg): global debug_level, verbose_flg if not cls.logger: cls.Init() cls.logger.error(msg) if cls.debug: sys.stderr.write("[%s::ERROR] %s\n" % (cls.name, msg)) #---------------------------------------------------------------------- @classmethod def Warning(cls, msg): global debug_level, verbose_flg if not cls.logger: cls.Init() cls.logger.warning('*****' + msg + '*****') if cls.debug: sys.stderr.write("[%s::WARNING] %s\n" % (cls.name, msg)) #---------------------------------------------------------------------- @classmethod def Init(cls, name='logger', log_dir='/c/temp', debug=False): cls.debug = debug cls.name = name cls.pid = os.getpid() if cls.debug: sys.stderr.write("[%s::Init] PID is %d\n" % (cls.name, cls.pid)) cls.log_file = '%s/%s.log' % (log_dir, name) try: cls.logger = logging.getLogger(name) cls.hdlr = logging.FileHandler(cls.log_file) cls.fmtr = logging.Formatter('%(asctime)s %(levelname)s %(message)s') cls.hdlr.setFormatter(cls.fmtr) cls.logger.addHandler(cls.hdlr) cls.logger.setLevel(logging.INFO) cls.logger.info("===== Started processing %s" % ('=' * 20)) cls.count = 0 except IOError, msg: sys.stderr.write(cls.log_file + ': cannot open: ' + `msg` + '\n') sys.exit(1) #==========================================================================
Timer.py
#========================================================================== class Timer: t_reference = None #---------------------------------------------------------------------- @classmethod def init(cls): return float(cls.get_reference_time(init=True)) * 0.001 #---------------------------------------------------------------------- @classmethod def time(cls): return float(cls.get_reference_time()) * 0.001 #---------------------------------------------------------------------- @classmethod def get_reference_time(cls, init=False): t_now = datetime.now() if (flg): cls.t_reference = t_now t = 0 else: t_delta = t_now - cls.t_reference t = ((t_delta.seconds * 1000000) + t_delta.microseconds)/1000.0 return t #---------------------------------------------------------------------- #==========================================================================
A Test Harness
Use this to test the performiq module...
$ cat tst_module.py #!/usr/bin/env python import time import performiq from performiq import Logger debug_level = 0 verbose_flg = True print "performiq version [%s]" % performiq.VERSION print "Set timer - %5.3f" % performiq.Timer.init() Logger.Init(debug=True) Logger.Info("Testing - Info...") Logger.Warning("Testing - Warning...") Logger.Error("Testing - Error...") time.sleep(3) print "Get timer - %5.3f" % performiq.Timer.time()
A Variant skel.py
This variant of my skel.py script uses the module.
$ cat tst_module.py #!/usr/bin/env python import time import performiq from performiq import Logger debug_level = 0 verbose_flg = True print "performiq version [%s]" % performiq.VERSION print "Set timer - %5.3f" % performiq.Timer.init() Logger.Init(debug=True) Logger.Info("Testing - Info...") Logger.Warning("Testing - Warning...") Logger.Error("Testing - Error...") time.sleep(3) print "Get timer - %5.3f" % performiq.Timer.time()