Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'subject:module': 0.04; 'value,': 0.04; 'parameter': 0.05; 'rod': 0.07; 'script,': 0.07; 'imported.': 0.09; 'value:': 0.09; 'def': 0.13; 'constants': 0.16; 'namespace,': 0.16; 'parser,': 0.16; 'sys.argv[0]': 0.16; 'this:': 0.16; 'works.': 0.23; 'command': 0.24; 'parameters': 0.25; 'module': 0.26; 'import': 0.27; 'tried': 0.27; 'raise': 0.28; 'value.': 0.28; 'constant': 0.29; 'importing': 0.29; 'class': 0.29; 'seem': 0.29; 'server': 0.30; 'databases,': 0.30; 'message)': 0.30; 'there': 0.33; 'calling': 0.34; 'to:addr:python- list': 0.35; 'none': 0.35; 'to:no real name:2**1': 0.35; 'question': 0.36; 'checks': 0.37; 'created': 0.37; 'but': 0.37; 'reference': 0.37; 'charset:us-ascii': 0.37; 'received:128': 0.38; 'skip:_ 10': 0.38; 'doing': 0.38; 'should': 0.38; 'skip:o 20': 0.38; 'files': 0.39; 'itself.': 0.39; 'called': 0.40; 'to:addr:python.org': 0.40; 'below': 0.62; 'our': 0.63; 'account': 0.66; 'sfxlen:4': 0.67; 'placed': 0.69; 'pfxlen:big': 0.77; 'subject:its': 0.84; 'value):': 0.84; 'subject:own': 0.91 Date: Wed, 21 Mar 2012 11:25:03 -0400 From: Rod Person To: python-list@python.org Subject: class checking its own module for an attribute X-Mailer: Claws Mail 3.7.10cvs7 (GTK+ 2.16.6; i586-pc-mingw32msvc) Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1332343845 news.xs4all.nl 6889 [2001:888:2000:d::a6]:40238 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:21986 We have a module called constants.py, which contains related to server names, databases, service account users and there passwords. In order to be able to use constants as command line parameters for calling from our batch files I created the class below that checks to make sure the parameter value is a valid constant and if so return it's value. class ConstantParameterCheck(Action): def __call__(self, parser, namespace, value, option_string=None): if value: if not hasattr(CCBH.constants, value): message = ("Invalid parameter value: {0!r}. Value must be a value constant in CCBH.constants.".format(value)) raise ArgumentError(self, message) setattr(namespace, self.dest, getattr(CCBH.constants, value)) This works great, but it has to be placed in every script, so I decided why not place this in the constants.py module so it can just be imported. So I did that and got it to work like this: class ConstantParameterCheck(Action): def __call__(self, parser, namespace, value, option_string=None): from CCBH import constants if value: if not hasattr(constants, value): message = ("Invalid parameter value: {0!r}. Value must be a value constant in CCBH.constants.".format(value)) raise ArgumentError(self, message) setattr(namespace, self.dest, getattr(CCBH.constants, value)) The question is there a way I can do this with out having to import constants when what it's doing is importing itself. It would seem to me that there should be a way for a module to reference itself. In that thinking I have tried if not(hasattr(__file__, value): if not(hasattr(__name__, value): and even: this = sys.argv[0] if not(hasattr(this, value): None of which works. -- Rod Person http://www.rodperson.com rodperson@rodperson.com 'Silence is a fence around wisdom'