Implementing a Standard Library
Revision as of 11:46, 20 February 2013 by PeterHarding (talk | contribs) (Created page with " =Components= I use the following for building up utility scripts. <pre> $ ls -lR ~/lib/py /home/pharding/lib/py: total 4 drwxr-xr-x+ 1 pharding Users 0 Feb 20 11:44 perfor...")
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
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
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) #==========================================================================
A Variant skel.py
This variant of my skel.py script uses the module.