Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #8793

Re: Testing if a global is defined in a module

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <chris@rebertia.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'subject:module': 0.04; 'ubuntu': 0.04; 'skip:` 10': 0.07; 'python': 0.08; 'foo': 0.09; 'integers': 0.09; "module's": 0.09; 'am,': 0.13; 'defined': 0.14; 'wrote:': 0.15; 'likewise': 0.16; 'received:209.85.213.174': 0.16; 'received:mail-yx0-f174.google.com': 0.16; 'refactored': 0.16; 'subject:global': 0.16; 'argument': 0.16; 'cc:addr:python-list': 0.16; 'mon,': 0.16; 'meant': 0.17; 'functions,': 0.19; 'cheers,': 0.19; 'cc:2**0': 0.21; 'header:In-Reply-To:1': 0.22; 'here?': 0.23; 'module,': 0.23; 'message-id:@mail.gmail.com': 0.28; 'import': 0.29; 'anyway.': 0.29; 'variables': 0.29; 'cc:addr:python.org': 0.30; 'classes': 0.30; 'module': 0.30; 'strings,': 0.30; 'class': 0.31; 'values': 0.31; "won't": 0.32; 'chris': 0.32; 'list': 0.32; 'example,': 0.35; '2.6': 0.35; 'module.': 0.35; 'johnson': 0.35; 'passed': 0.37; 'but': 0.37; 'using': 0.37; 'received:google.com': 0.38; 'received:209.85': 0.38; 'subject:: ': 0.38; 'something': 0.38; 'else': 0.38; 'should': 0.39; 'returned': 0.39; 'received:209': 0.40; 'exclude': 0.77; 'sender:addr:chris': 0.84; 'url:rebertia': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=rebertia.com; s=google; h=mime-version:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:cc:content-type; bh=PG1oRZBQQUe18skYG9umq0u1zmVzDIOB4ADusbsVBTk=; b=GVaTlZy+H1Sl9HK3Rz9D5wsgRcVYET+wfv8sQ7CwxSdDOHPJFVlMgiGCL74Mn4mjXw Mn2me0+Dk7ow7iwbROScrWThHUoKh4EeJJRm9MPRL9UMzm2LjvWdeJ/ELP3cwJbuRRao EI8ws1g9i6UkCF/fchOo7LmDYsXLU+FVhCo60=
MIME-Version 1.0
Sender chris@rebertia.com
In-Reply-To <20110704181133.GH1971@johnsons-web.com>
References <20110704181133.GH1971@johnsons-web.com>
Date Mon, 4 Jul 2011 14:03:32 -0700
X-Google-Sender-Auth fGbq_kW-7z5ZG15-QaycnM-b3nM
Subject Re: Testing if a global is defined in a module
From Chris Rebert <clp2@rebertia.com>
To Tim Johnson <tim@johnsons-web.com>
Content-Type text/plain; charset=UTF-8
Cc Python ML <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.607.1309813416.1164.python-list@python.org> (permalink)
Lines 30
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1309813416 news.xs4all.nl 21755 [2001:888:2000:d::a6]:60515
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:8793

Show key headers only | View raw


On Mon, Jul 4, 2011 at 11:11 AM, Tim Johnson <tim@johnsons-web.com> wrote:
> Using Python 2.6 on ubuntu 10.04.
> inspect module :
> I want to 'inspect' a module and get a list of all
> functions, classes and global variables in that module.
You meant "first defined in" that module.
<snip>
> Example, for a module name `mvcInstall', when a class
> name `Install' that is defined in the module
> is passed as an argument to inspect.getmodule, the
> values returned is something like
> "<module 'mvcInstall' from
> '/home/tim/prj/cgi/libraries/python/mvcInstall.py'>"
> Likewise for functions defined in the module.
>
> ** But ** when global variables such as strings, booleans,
> integers are passed as an argument to getmodule, the
> value returned is `None'.
>
> What else can I do here?

Look at the names in the module's import statements using the `ast`
module, and exclude those from the set of names defined in the module.
Won't work for `from foo import *`, but that's bad practice and should
be refactored anyway.

Cheers,
Chris
--
http://rebertia.com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Testing if a global is defined in a module Chris Rebert <clp2@rebertia.com> - 2011-07-04 14:03 -0700

csiph-web