473,239 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

how to find available classes in a file ?

I want to have a (dynamically) list of all classes defined in a py-file.
Is there a way of getting this list, without manually parsing the file ?

thanks,
Stef Mientki
Jul 16 '07 #1
9 5149
On Jul 17, 1:44 am, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
I want to have a (dynamically) list of all classes defined in a py-file.
Is there a way of getting this list, without manually parsing the file ?

thanks,
Stef Mientki
I have written something that does something like this, but I am not
sure it is the pythonic way.
Bascially I am loading the module and then using dir() on the module
object I am looking for
attribute of the type classobj (for old style classes) and type type
(for new style classes).

I also heard of the inspect module, but I haven't checked it yet.

bests,
../alex
--
..w( the_mindstorm )p.
Jul 16 '07 #2
En Mon, 16 Jul 2007 20:13:19 -0300, Alex Popescu
<th***********************@gmail.comescribió:
On Jul 17, 1:44 am, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
>I want to have a (dynamically) list of all classes defined in a py-file.
Is there a way of getting this list, without manually parsing the file ?

I have written something that does something like this, but I am not
sure it is the pythonic way.
Bascially I am loading the module and then using dir() on the module
object I am looking for
attribute of the type classobj (for old style classes) and type type
(for new style classes).
There is also the pyclbr standard module
<http://docs.python.org/lib/module-pyclbr.htmlthat does not load the
module (just reparses enough of it to get the info needed). So a broken
import, or any other initialization error, won't hurt.
I also heard of the inspect module, but I haven't checked it yet.
inspect.getmembers(the_module, inspect.isclass) would do. But this
requires the module to be importable.

--
Gabriel Genellina

Jul 17 '07 #3
Stef Mientki <S.**************@mailbox.kun.nlwrites:
I want to have a (dynamically) list of all classes defined in a py-file.
Is there a way of getting this list, without manually parsing the file ?
>>import random
for name in dir(random):
... obj = getattr(random, name)
... if isinstance(obj, type):
... print "%(name)s is a type" % locals()
...
Random is a type
SystemRandom is a type
WichmannHill is a type
_BuiltinMethodType is a type
_MethodType is a type
>>>
And, since you asked for just getting the list, here's the list of
names:
>>[name for (name, obj) in
... [(name, getattr(random, name)) for name in dir(random)]
... if isinstance(obj, type)]
['Random', 'SystemRandom', 'WichmannHill', '_BuiltinMethodType', '_MethodType']

Or the list of type objects themselves:
>>[obj for obj in
... [getattr(random, name) for name in dir(random)]
... if isinstance(obj, type)]
[<class 'random.Random'>, <class 'random.SystemRandom'>, <class 'random.WichmannHill'>, <type 'builtin_function_or_method'>, <type 'instancemethod'>]
--
\ "Pinky, are you pondering what I'm pondering?" "Well, I think |
`\ so, Brain, but I can't memorize a whole opera in Yiddish." -- |
_o__) _Pinky and The Brain_ |
Ben Finney
Jul 17 '07 #4
Gabriel Genellina wrote:
En Mon, 16 Jul 2007 20:13:19 -0300, Alex Popescu
<th***********************@gmail.comescribió:
>On Jul 17, 1:44 am, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
>>I want to have a (dynamically) list of all classes defined in a py-file.
Is there a way of getting this list, without manually parsing the file ?

I have written something that does something like this, but I am not
sure it is the pythonic way.
Bascially I am loading the module and then using dir() on the module
object I am looking for
attribute of the type classobj (for old style classes) and type type
(for new style classes).

There is also the pyclbr standard module
<http://docs.python.org/lib/module-pyclbr.htmlthat does not load the
module (just reparses enough of it to get the info needed). So a broken
import, or any other initialization error, won't hurt.
thanks Gabriel, Alex and Ben.
This is indeed a good working solution,
because pyclbr also specifies in which module the class is definied.
In the other suggestions, you also get the class definitions of imported modules.

cheers,
Stef Mientki
Jul 17 '07 #5
On Jul 17, 4:41 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Mon, 16 Jul 2007 20:13:19 -0300, Alex Popescu
<the.mindstorm.mailingl...@gmail.comescribió:
On Jul 17, 1:44 am, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
I want to have a (dynamically) list of all classes defined in a py-file.
Is there a way of getting this list, without manually parsing the file?
I have written something that does something like this, but I am not
sure it is the pythonic way.
Bascially I am loading the module and then using dir() on the module
object I am looking for
attribute of the type classobj (for old style classes) and type type
(for new style classes).

There is also the pyclbr standard module
<http://docs.python.org/lib/module-pyclbr.htmlthat does not load the
module (just reparses enough of it to get the info needed). So a broken
import, or any other initialization error, won't hurt.
I also heard of the inspect module, but I haven't checked it yet.

inspect.getmembers(the_module, inspect.isclass) would do. But this
requires the module to be importable.

--
Gabriel Genellina
I may be wrong but I think I've found a difference between my
dir(module) approach
and the inspect.getmembers(module, inspect.isclass): the first one
returns the
classes defined in the module, while the later also lists the imported
available
classes.

../alex
--
..w( the_mindstorm )p.
Jul 17 '07 #6
En Tue, 17 Jul 2007 11:05:17 -0300, Alex Popescu
<th***********************@gmail.comescribió:
On Jul 17, 4:41 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
On Jul 17, 1:44 am, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
I want to have a (dynamically) list of all classes defined in a
py-file.

inspect.getmembers(the_module, inspect.isclass) would do. But this
requires the module to be importable.

I may be wrong but I think I've found a difference between my
dir(module) approach
and the inspect.getmembers(module, inspect.isclass): the first one
returns the
classes defined in the module, while the later also lists the imported
available
classes.
Let's try, using the standard module wave.py which imports class Chunk
from chunk.py:

pyimport inspect
pyimport wave
pyinspect.getmembers(wave, inspect.isclass)
[('Chunk', <class chunk.Chunk at 0x00ADEC00>), ('Error', <class
'wave.Error'>), ...
pydir(wave)
['Chunk', 'Error', 'WAVE_FORMAT_PCM', 'Wave_read', 'Wave_write', ...

Chunk appears on both. (That's not a surprise, since inspect.getmembers
uses dir() internally).
If you want to include *only* classes defined in the module itself, you
could test the __module__ attribute:

pywave.Chunk.__module__
'chunk'
pywave.Wave_read.__module__
'wave'

How to filter when the class is defined deeply inside a package is left as
an exercise to the reader :)

--
Gabriel Genellina

Jul 17 '07 #7
On Jul 17, 4:41 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Mon, 16 Jul 2007 20:13:19 -0300, Alex Popescu
<the.mindstorm.mailingl...@gmail.comescribió:
On Jul 17, 1:44 am, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
I want to have a (dynamically) list of all classes defined in a py-file.
Is there a way of getting this list, without manually parsing the file?
I have written something that does something like this, but I am not
sure it is the pythonic way.
Bascially I am loading the module and then using dir() on the module
object I am looking for
attribute of the type classobj (for old style classes) and type type
(for new style classes).

There is also the pyclbr standard module
<http://docs.python.org/lib/module-pyclbr.htmlthat does not load the
module (just reparses enough of it to get the info needed). So a broken
import, or any other initialization error, won't hurt.
I also heard of the inspect module, but I haven't checked it yet.

inspect.getmembers(the_module, inspect.isclass) would do. But this
requires the module to be importable.

--
Gabriel Genellina
I think I have found a difference between my dir(module) based
implementation
and the inspect.getmember(module, inspect.isclass) functionality. Mine
is
returning only the classes defined in the provided module, while the
later is
returning also the imported classes. Am I getting the results
wrongly?
(I don't seem to see this explanation in the inspect.getmembers doc or
I am just
misreading it).

../alex
--
..w( the_mindstorm )p.
Jul 18 '07 #8
Alex Popescu <the.mindstorm.mailinglist <atgmail.comwrites:
>
On Jul 17, 4:41 am, "Gabriel Genellina" <gagsl-... <atyahoo.com.ar>
wrote:
En Mon, 16 Jul 2007 20:13:19 -0300, Alex Popescu
<the.mindstorm.mailingl... <atgmail.comescribi

I apologize for posting the previous message a couple of times,
but I wasn't seeing it displayed on the Google groups.

../alex
--
..w( the_mindstorm )p.


Jul 18 '07 #9
Alex Popescu <th***********************@gmail.comwrites:
[...]
I may be wrong but I think I've found a difference between my
dir(module) approach
and the inspect.getmembers(module, inspect.isclass): the first one
returns the
classes defined in the module, while the later also lists the imported
available
classes.
FWIW, see doctest.DocTestFinder._from_module() for a way to tell if an
object is from a module. This can be fooled if you've managed to get
hold of two copies of a module, though, which unfortunately is
possible.

I hope the import system is much cleaner in Python 3 :-/ (seems there
are efforts in that direction, though I'm not up-to-date with it).
John
Jul 22 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ed Severn | last post by:
I'm sorry of this question has been posed and answered many times before. But I have avoided using the "package" statement because of this. Most of my classes have no "package" statement, and...
13
by: Jean-François Doyon | last post by:
Hello, I'm using MetaClasses to create classes. How do I make these new classes "globally" available? I probably just have to assign them to something magic, but I can't seem to figure out...
4
by: Alan Mailer | last post by:
Again, I'm new to VB.net and there is something I need help with: Like (I assume) many of us, over time I want to be able to create some VB.net classes that I might want to use in more than one...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.