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 9 5165
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.
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
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
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
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.
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
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.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |