471,344 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,344 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 5043
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 discussion thread is closed

Replies have been disabled for this discussion.

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.