Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8784 > unrolled thread
| Started by | Tim Johnson <tim@johnsons-web.com> |
|---|---|
| First post | 2011-07-04 10:11 -0800 |
| Last post | 2011-07-05 15:43 +0000 |
| Articles | 20 on this page of 22 — 8 participants |
Back to article view | Back to comp.lang.python
Testing if a global is defined in a module Tim Johnson <tim@johnsons-web.com> - 2011-07-04 10:11 -0800
Re: Testing if a global is defined in a module rantingrick <rantingrick@gmail.com> - 2011-07-04 12:40 -0700
Re: Testing if a global is defined in a module Tim Johnson <tim@johnsons-web.com> - 2011-07-04 12:30 -0800
Re: Testing if a global is defined in a module rantingrick <rantingrick@gmail.com> - 2011-07-04 14:30 -0700
Re: Testing if a global is defined in a module Tim Johnson <tim@johnsons-web.com> - 2011-07-04 13:59 -0800
Re: Testing if a global is defined in a module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-05 09:13 +1000
Re: Testing if a global is defined in a module Tim Johnson <tim@johnsons-web.com> - 2011-07-04 15:26 -0800
Re: Testing if a global is defined in a module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-05 09:33 +1000
Re: Testing if a global is defined in a module Tim Johnson <tim@johnsons-web.com> - 2011-07-04 16:01 -0800
Re: Testing if a global is defined in a module Chris Angelico <rosuav@gmail.com> - 2011-07-05 10:08 +1000
Re: Testing if a global is defined in a module Grant Edwards <invalid@invalid.invalid> - 2011-07-05 14:11 +0000
Re: Testing if a global is defined in a module "Waldek M." <wm@localhost.localdomain> - 2011-07-05 18:35 +0200
Re: Testing if a global is defined in a module Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-06 03:36 +1000
Re: Testing if a global is defined in a module Chris Angelico <rosuav@gmail.com> - 2011-07-06 08:45 +1000
Re: Testing if a global is defined in a module "Waldek M." <wm@localhost.localdomain> - 2011-07-06 08:00 +0200
Re: Testing if a global is defined in a module Grant Edwards <invalid@invalid.invalid> - 2011-07-07 14:18 +0000
Re: Testing if a global is defined in a module Chris Rebert <clp2@rebertia.com> - 2011-07-07 17:36 -0700
Re: Testing if a global is defined in a module Grant Edwards <invalid@invalid.invalid> - 2011-07-05 22:39 +0000
Re: Testing if a global is defined in a module Tim Johnson <tim@johnsons-web.com> - 2011-07-04 18:01 -0800
Re: Testing if a global is defined in a module Ian Kelly <ian.g.kelly@gmail.com> - 2011-07-04 22:28 -0600
Re: Testing if a global is defined in a module Tim Johnson <tim@johnsons-web.com> - 2011-07-05 07:30 -0800
Re: Testing if a global is defined in a module Grant Edwards <invalid@invalid.invalid> - 2011-07-05 15:43 +0000
Page 1 of 2 [1] 2 Next page →
| From | Tim Johnson <tim@johnsons-web.com> |
|---|---|
| Date | 2011-07-04 10:11 -0800 |
| Subject | Testing if a global is defined in a module |
| Message-ID | <mailman.603.1309803409.1164.python-list@python.org> |
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. ## A module has been imported, and we call `getmembers' members = inspect.getmembers(mod) ## While iterating thru `members', we test to see ## if an object is defined in the module. for m in members: obj = m[1] res = inspect.getmodule(obj) ## It appears that getmodule returns None for ## all but functions and classes. 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? thanks -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com
[toc] | [next] | [standalone]
| From | rantingrick <rantingrick@gmail.com> |
|---|---|
| Date | 2011-07-04 12:40 -0700 |
| Message-ID | <3dc7e7a8-9a90-499a-b544-bb9969d9ab55@t5g2000yqj.googlegroups.com> |
| In reply to | #8784 |
On Jul 4, 1:11 pm, Tim Johnson <t...@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. > > ## A module has been imported, and we call `getmembers' > members = inspect.getmembers(mod) > > ## While iterating thru `members', we test to see > ## if an object is defined in the module. > for m in members: > obj = m[1] > res = inspect.getmodule(obj) > ## It appears that getmodule returns None for > ## all but functions and classes. > > 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? > thanks > -- > Tim > tim at johnsons-web dot com or akwebsoft dot comhttp://www.akwebsoft.com Well if you follow the python style guide (and most accepted styles for global notation) then it's a trial exercise. You don't even have to import anything!!! :) >>> GLOBAL_STR = 'str' >>> GLOBAL_FLOAT = 1.33333 >>> GLoBaL_Bs = '' >>> dir() ['GLOBAL_FLOAT', 'GLOBAL_STR', 'GLoBaL_Bs', '__builtins__', '__doc__', '__name__', '__package__', 'item'] >>> for item in dir(): if item.isupper(): print 'Found Global!', item Found Global! GLOBAL_FLOAT Found Global! GLOBAL_STR ;-)
[toc] | [prev] | [next] | [standalone]
| From | Tim Johnson <tim@johnsons-web.com> |
|---|---|
| Date | 2011-07-04 12:30 -0800 |
| Message-ID | <mailman.606.1309811314.1164.python-list@python.org> |
| In reply to | #8787 |
* rantingrick <rantingrick@gmail.com> [110704 12:00]: > On Jul 4, 1:11 pm, Tim Johnson <t...@johnsons-web.com> wrote: > > Well if you follow the python style guide (and most accepted styles > for global notation) then it's a trial exercise. You don't even have > to import anything!!! :) > > >>> GLOBAL_STR = 'str' > >>> GLOBAL_FLOAT = 1.33333 > >>> GLoBaL_Bs = '' > >>> dir() > ['GLOBAL_FLOAT', 'GLOBAL_STR', 'GLoBaL_Bs', '__builtins__', '__doc__', > '__name__', '__package__', 'item'] > >>> for item in dir(): > if item.isupper(): > print 'Found Global!', item Thanks for the reply: *but* dir(<targetmodule>) will also show globals from other modules imported by the target module. So I would need a way to distinguish between those imported and those defined in <targetmodule> print(dir(targetmodule)) => ['Install', 'TestAddresses', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'chmod', 'consoleMessage', 'cp', 'debug', 'erh', 'exists', 'halt', 'is_list', 'load', 'makePath', 'mkdir', 'process', 'sys', 'traceback', 'usingCgi'] where 'TestAddresses' is a member of an imported module and 'usingCgi' is the only data variable defined in <targetmodule> regards -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com
[toc] | [prev] | [next] | [standalone]
| From | rantingrick <rantingrick@gmail.com> |
|---|---|
| Date | 2011-07-04 14:30 -0700 |
| Message-ID | <d2e317cc-1f48-4636-b888-0d661c081b2e@k16g2000yqm.googlegroups.com> |
| In reply to | #8792 |
On Jul 4, 3:30 pm, Tim Johnson <t...@johnsons-web.com> wrote: > > Thanks for the reply: *but* > dir(<targetmodule>) will also show globals from other modules imported > by the target module. So I would need a way to distinguish between > those imported and those defined in <targetmodule> Okay, then do some processing on the source. You can use regexps or the module mentioned earlier by Chris.
[toc] | [prev] | [next] | [standalone]
| From | Tim Johnson <tim@johnsons-web.com> |
|---|---|
| Date | 2011-07-04 13:59 -0800 |
| Message-ID | <mailman.610.1309816628.1164.python-list@python.org> |
| In reply to | #8794 |
* rantingrick <rantingrick@gmail.com> [110704 13:47]:
> On Jul 4, 3:30 pm, Tim Johnson <t...@johnsons-web.com> wrote:
> >
> > Thanks for the reply: *but*
> > dir(<targetmodule>) will also show globals from other modules imported
> > by the target module. So I would need a way to distinguish between
> > those imported and those defined in <targetmodule>
>
> Okay, then do some processing on the source. You can use regexps or
> the module mentioned earlier by Chris.
I think I'm making this unnecessarily complicated. I had used
something like:
from spam import TestAddresses ## just for grins
Which is something that I almost never do in practice.
Also, you pointed out the UC naming convention, which I was
unacquainted with, being self-employed, self-taught and a one-man
crew who doesn't spend as much time as he should reading PEPs.
I'm going to go for the predicate test as second argument to
getmembers with something like:
def isdata(self,obj,name):
"""Check if an object is of a type that probably means it's data."""
return (not (inspect.ismodule(obj) or inspect.isclass(obj) or
inspect.isroutine(obj) or inspect.isframe(obj) or
inspect.istraceback(obj) or inspect.iscode(obj)))
and name.issupper()
## Untested code
thanks again
--
Tim
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-07-05 09:13 +1000 |
| Message-ID | <4e124916$0$29970$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #8792 |
Tim Johnson wrote: > dir(<targetmodule>) will also show globals from other modules imported > by the target module. So I would need a way to distinguish between > those imported and those defined in <targetmodule> Why would you want to do that? Importing *is* a definition in <targetmodule>. Consider these two code snippets: #1 from math import pi #2 import math tau = 2*math.pi del math Why do you think it is necessary to distinguish pi from tau? Both names are local to the current namespace. > print(dir(targetmodule)) => > ['Install', 'TestAddresses', '__builtins__', '__doc__', > '__file__', '__name__', '__package__', 'chmod', 'consoleMessage', > 'cp', 'debug', 'erh', 'exists', 'halt', 'is_list', 'load', > 'makePath', 'mkdir', 'process', 'sys', 'traceback', 'usingCgi'] > where 'TestAddresses' is a member of an imported module and You are mistaken. TestAddresses is *not* a member of an imported module. It is a member of the current module, which may or may not happen to point to the same object as the other module as well. > 'usingCgi' is the only data variable defined in <targetmodule> It seems to me that your approach here is unnecessarily complex and fragile. I don't know what problem you are trying to solve, but trying to solve it by intraspecting differences that aren't differences is surely the wrong way to do it. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Tim Johnson <tim@johnsons-web.com> |
|---|---|
| Date | 2011-07-04 15:26 -0800 |
| Message-ID | <mailman.611.1309821881.1164.python-list@python.org> |
| In reply to | #8798 |
* Steven D'Aprano <steve+comp.lang.python@pearwood.info> [110704 15:18]: > > You are mistaken. TestAddresses is *not* a member of an imported module. It > is a member of the current module, which may or may not happen to point to > the same object as the other module as well. You are correct. I mispoke or misapplied. See my last post. > > > 'usingCgi' is the only data variable defined in <targetmodule> > > It seems to me that your approach here is unnecessarily complex and fragile. > I don't know what problem you are trying to solve, but trying to solve it > by intraspecting differences that aren't differences is surely the wrong > way to do it. See my last post... -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-07-05 09:33 +1000 |
| Message-ID | <4e124db1$0$29973$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #8800 |
Tim Johnson wrote: >> It seems to me that your approach here is unnecessarily complex and >> fragile. I don't know what problem you are trying to solve, but trying to >> solve it by intraspecting differences that aren't differences is surely >> the wrong way to do it. > See my last post... Yes, but what are you actually *trying to do*? "Detecting data members" is not an end in itself. Why do you think you need to detect data members? -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Tim Johnson <tim@johnsons-web.com> |
|---|---|
| Date | 2011-07-04 16:01 -0800 |
| Message-ID | <mailman.612.1309823965.1164.python-list@python.org> |
| In reply to | #8802 |
* Steven D'Aprano <steve+comp.lang.python@pearwood.info> [110704 15:48]: > Tim Johnson wrote: > > >> It seems to me that your approach here is unnecessarily complex and > >> fragile. I don't know what problem you are trying to solve, but trying to > >> solve it by intraspecting differences that aren't differences is surely > >> the wrong way to do it. > > See my last post... > > > Yes, but what are you actually *trying to do*? "Detecting data members" is > not an end in itself. Why do you think you need to detect data members? Steven, I'm building a documentation system. I have my own MVC framework and the goal is to have a documentation module for each project. Thanks again for the clarifications. I always learn a lot from you. -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-07-05 10:08 +1000 |
| Message-ID | <mailman.613.1309824509.1164.python-list@python.org> |
| In reply to | #8802 |
On Tue, Jul 5, 2011 at 10:01 AM, Tim Johnson <tim@johnsons-web.com> wrote: > Steven, I'm building a documentation system. I have my own MVC framework > and the goal is to have a documentation module for each project. > Is there a reason for not using Doxygen / Autodoc / etc, or at least something in the same style? They work from specially-formatted comments in the source code, rather than the compiled module object. ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2011-07-05 14:11 +0000 |
| Message-ID | <iuv63b$qiq$1@reader1.panix.com> |
| In reply to | #8805 |
On 2011-07-05, Chris Angelico <rosuav@gmail.com> wrote:
> On Tue, Jul 5, 2011 at 10:01 AM, Tim Johnson <tim@johnsons-web.com> wrote:
>>>Steven, I'm building a documentation system. I have my own MVC
>>>framework and the goal is to have a documentation module for each
>>>project.
>>
>
> Is there a reason for not using Doxygen / Autodoc / etc, or at least
> something in the same style? They work from specially-formatted
> comments in the source code, rather than the compiled module object.
Because those specially-formatted comments are wrong.
--
Grant Edwards grant.b.edwards Yow! I have many CHARTS
at and DIAGRAMS..
gmail.com
[toc] | [prev] | [next] | [standalone]
| From | "Waldek M." <wm@localhost.localdomain> |
|---|---|
| Date | 2011-07-05 18:35 +0200 |
| Message-ID | <ngpta76me2tm$.dlg@localhost.localdomain> |
| In reply to | #8826 |
Dnia Tue, 5 Jul 2011 14:11:56 +0000 (UTC), Grant Edwards napisał(a): > Because those specially-formatted comments are wrong. ... because? Not in sarcasm mode; just curious why you don't like them. Br. Waldek
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve+comp.lang.python@pearwood.info> |
|---|---|
| Date | 2011-07-06 03:36 +1000 |
| Message-ID | <4e134b99$0$29993$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #8846 |
Waldek M. wrote: > Dnia Tue, 5 Jul 2011 14:11:56 +0000 (UTC), Grant Edwards napisał(a): >> Because those specially-formatted comments are wrong. > > ... because? > Not in sarcasm mode; just curious why you don't like them. Because unless you are extremely disciplined, code and the comments describing them get out of sync. Quote: "At Resolver we've found it useful to short-circuit any doubt and just refer to comments in code as 'lies'. " --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2011-07-06 08:45 +1000 |
| Message-ID | <mailman.662.1309905903.1164.python-list@python.org> |
| In reply to | #8847 |
On Wed, Jul 6, 2011 at 3:36 AM, Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote: > > Because unless you are extremely disciplined, code and the comments > describing them get out of sync. Quote: > > "At Resolver we've found it useful to short-circuit any doubt and just > refer to comments in code as 'lies'. " > --Michael Foord paraphrases Christian Muirhead on python-dev, 2009-03-22 And yet, as I have found out by trying to embed V8 (Google's Javascript engine), those little comments can (a) prove the difference between a bug and a misfeature, and (b) be all the documentation there is. Okay, it's not QUITE the latter, but most things are not documented outside of the source, and I greatly appreciate those little comments! ChrisA
[toc] | [prev] | [next] | [standalone]
| From | "Waldek M." <wm@localhost.localdomain> |
|---|---|
| Date | 2011-07-06 08:00 +0200 |
| Message-ID | <1pvghkmjff37$.dlg@localhost.localdomain> |
| In reply to | #8847 |
Dnia Wed, 06 Jul 2011 03:36:24 +1000, Steven D'Aprano napisał(a): > Because unless you are extremely disciplined, code and the comments > describing them get out of sync. [...] True, but that gets far worse with external docs. Do you have in mind any better replacement? Br. Waldek
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2011-07-07 14:18 +0000 |
| Message-ID | <iv4f7o$mm0$1@reader1.panix.com> |
| In reply to | #8903 |
On 2011-07-06, Waldek M. <wm@localhost.localdomain> wrote:
> Dnia Wed, 06 Jul 2011 03:36:24 +1000, Steven D'Aprano napisa?(a):
>> Because unless you are extremely disciplined, code and the comments
>> describing them get out of sync. [...]
> True, but that gets far worse with external docs. Do you have in mind
> any better replacement?
You write code that's easy to read, and you provide the language with
introspection capabilities that allow you do do things like write a
program that will list the symbols exported by a module.
--
Grant Edwards grant.b.edwards Yow! Now that I have my
at "APPLE", I comprehend COST
gmail.com ACCOUNTING!!
[toc] | [prev] | [next] | [standalone]
| From | Chris Rebert <clp2@rebertia.com> |
|---|---|
| Date | 2011-07-07 17:36 -0700 |
| Message-ID | <mailman.765.1310085389.1164.python-list@python.org> |
| In reply to | #9032 |
On Thu, Jul 7, 2011 at 7:18 AM, Grant Edwards <invalid@invalid.invalid> wrote: > On 2011-07-06, Waldek M. <wm@localhost.localdomain> wrote: >> Dnia Wed, 06 Jul 2011 03:36:24 +1000, Steven D'Aprano napisa?(a): > >>> Because unless you are extremely disciplined, code and the comments >>> describing them get out of sync. [...] > >> True, but that gets far worse with external docs. Do you have in mind >> any better replacement? > > You write code that's easy to read Reminded me of this excellent presentation: "Uncomment Your Code" https://docs.google.com/present/view?id=ah82mvnssv5d_162dbgx78gw Cheers, Chris
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2011-07-05 22:39 +0000 |
| Message-ID | <iv03qf$at$1@reader1.panix.com> |
| In reply to | #8846 |
On 2011-07-05, Waldek M. <wm@localhost.localdomain> wrote:
> Dnia Tue, 5 Jul 2011 14:11:56 +0000 (UTC), Grant Edwards napisa?(a):
>> Because those specially-formatted comments are wrong.
>
> ... because?
In my experience, they're wrong because somebody changes the code and
not the comments.
> Not in sarcasm mode; just curious why you don't like them.
There've been too many times when I couldn't trust them.
--
Grant Edwards grant.b.edwards Yow! I'm totally DESPONDENT
at over the LIBYAN situation
gmail.com and the price of CHICKEN
...
[toc] | [prev] | [next] | [standalone]
| From | Tim Johnson <tim@johnsons-web.com> |
|---|---|
| Date | 2011-07-04 18:01 -0800 |
| Message-ID | <mailman.616.1309831138.1164.python-list@python.org> |
| In reply to | #8802 |
* Chris Angelico <rosuav@gmail.com> [110704 16:19]: > On Tue, Jul 5, 2011 at 10:01 AM, Tim Johnson <tim@johnsons-web.com> wrote: > > Steven, I'm building a documentation system. I have my own MVC framework > > and the goal is to have a documentation module for each project. > > > > Is there a reason for not using Doxygen / Autodoc / etc, or at least > something in the same style? They work from specially-formatted > comments in the source code, rather than the compiled module object. I want this as a component of my system. It will be bound to a relational data-structure-based 'core' that defines relationships between all files in the project. :) It's called *collateral* And yes, I have worked with comment-based systems, including my own, that worked with multi-language projects. Best regards -- Tim tim at johnsons-web dot com or akwebsoft dot com http://www.akwebsoft.com
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2011-07-04 22:28 -0600 |
| Message-ID | <mailman.619.1309840154.1164.python-list@python.org> |
| In reply to | #8802 |
On Mon, Jul 4, 2011 at 6:01 PM, Tim Johnson <tim@johnsons-web.com> wrote: >> Yes, but what are you actually *trying to do*? "Detecting data members" is >> not an end in itself. Why do you think you need to detect data members? > > Steven, I'm building a documentation system. I have my own MVC framework > and the goal is to have a documentation module for each project. It sounds like what you really want is to detect the names *exported* by the module, then. Why not do it the same way Python does it? If the module defines an "__all__" attribute, then it is taken to be a sequence of strings which are the exported names. Otherwise, the exported names are taken to be all the names in the module dict that don't begin with an underscore. Cheers, Ian
[toc] | [prev] | [next] | [standalone]
Page 1 of 2 [1] 2 Next page →
Back to top | Article view | comp.lang.python
csiph-web