How can I get a list of all classes within current module in Python?
Try this:
import sys
current_module = sys.modules[__name__]
In your context:
import sys, inspect
def print_classes():
for name, obj in inspect.getmembers(sys.modules[__name__]):
if inspect.isclass(obj):
print(obj)
And even better:
clsmembers = inspect.getmembers(sys.modules[__name__], inspect.isclass)
Because inspect.getmembers()
takes a predicate.
Find classes available in a Module
Classes are accessed through constants. Classes defined within a module are listed as constants in that module. So you just need to choose the constants that refer to classes.
MyModule.constants.select {|c| MyModule.const_get(c).is_a? Class}
In Python, how do I get the list of classes defined within a particular file?
You can get both:
import importlib, inspect
for name, cls in inspect.getmembers(importlib.import_module("myfile"), inspect.isclass):
you may additionally want to check:
if cls.__module__ == 'myfile'
Trying to find all classes that include a module
[Note: @engineersmnky illustrates a way to do this using flat_map
eliminating the need for the matching_classes
parameter. I found it more difficult to understand, but it's a perfectly sound use of flat_map
and a worthy solution. The code is posted at https://repl.it/@engineersmnky/IllinformedMountainousAnkole ]
The following code uses recursion to descend the inverted tree of modules. The result (printed at the very end) is correct, and includes classes in two modules. (I've coded a minimal module and class hierarchy to use as an example.)
#!/usr/bin/env ruby
module ParentModule
module OtherModule; end
class ParentClassYes; include OtherModule; end
class ParentClassNo; end
module ChildModule
class ChildClassYes; include OtherModule; end
class ChildClassNo; end
end
end
def classes_for_module_tree(the_module, matching_classes = [])
the_module.constants.each_with_object(matching_classes) \
do |const, matching_classes|
value = the_module.const_get(const)
if value.is_a?(Class)
if value.included_modules.include?(ParentModule::OtherModule)
matching_classes << value
end
elsif value.is_a?(Module)
# Here is where we call this method recursively. We suspend collecting
# matches for this module, call the method to process the newly found
# (sub)module, then use the array returned by that invocation to resume
# processing this module.
matching_classes = classes_for_module_tree(value, matching_classes)
end
end
matching_classes
end
p classes_for_module_tree(ParentModule)
# prints: [ParentModule::ParentClassYes, ParentModule::ChildModule::ChildClassYes]
Python: find classes in module before class is defined
Personally, I would use a decorator to mark the classes that are important. You can place the list that will hold them at the top of the file where it will be noticable.
Here's a simple example:
# Classes are added here if they are important, because...
important_classes = []
def important(cls):
important_classes.append(cls)
return cls
@important
class ClassA(object):
pass
class ClassB(object):
pass
@important
class ClassC(object):
pass
# Now you can use the important_classes list however you like.
print(important_classes)
# => [<class '__main__.ClassA'>, <class '__main__.ClassC'>]
How do you get all classes defined in a module but not imported?
Inspect the __module__
attribute of the class to find out which module it was defined in.
Getting a list of classes that include a module
module MyMod; end
class A; include MyMod; end
class B < A; end
class C; end
ObjectSpace.each_object(Class).select { |c| c.included_modules.include? MyMod }
#=> [B, A]
See ObjectSpace::
each_object.
Related Topics
How to Check If a Class Is Defined
How to Use Unicorn as "Rails S"
Ruby - Replace the First Occurrence of a Substring with Another String
Repairing Postgresql After Upgrade to Osx Mavericks
Hot Deploy on Heroku with No Downtime
How to Create an Array in Ruby with Default Values
Ruby Koan: Constants Become Symbols
Ruby: Conditional Matrix? Case with Multiple Conditions
How to Handle Errors with Httparty
Rails 6.1, Ruby 3.0.0: Tests Error as They Cannot Load Rexml
How to Spawn an Eventmachine "Inside" a Rails App
Ruby: Extracting Words from String
Rubygems Do Not Install on Os X Lion
How to Remove a Substring After a Certain Character in a String Using Ruby