from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from importlib import import_module
import pkgutil
import sys
import inspect
from . import layers


def import_recursive(package, clsmembers):
    """
    Takes a package and imports all modules underneath it
    """

    pkg_dir = package.__path__
    module_location = package.__name__
    for (_module_loader, name, ispkg) in pkgutil.iter_modules(pkg_dir):
        module_name = "{}.{}".format(module_location, name)  # Module/package
        module = import_module(module_name)
        clsmembers += [cls[1] for cls in inspect.getmembers(module, inspect.isclass)]
        if ispkg:
            import_recursive(module, clsmembers)


clsmembers = []
import_recursive(sys.modules[__name__], clsmembers)

for cls in clsmembers:
    if issubclass(cls, layers.ModelLayer) and cls is not layers.ModelLayer:
        layers.register_layer(cls.__name__, cls)

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from importlib import import_module
import pkgutil
import sys
from . import layers


def import_recursive(package):
    """
    Takes a package and imports all modules underneath it
    """

    pkg_dir = package.__path__
    module_location = package.__name__
    for (_module_loader, name, ispkg) in pkgutil.iter_modules(pkg_dir):
        module_name = "{}.{}".format(module_location, name)  # Module/package
        module = import_module(module_name)
        if ispkg:
            import_recursive(module)


def find_subclasses_recursively(base_cls, sub_cls):
    cur_sub_cls = base_cls.__subclasses__()
    sub_cls.update(cur_sub_cls)
    for cls in cur_sub_cls:
        find_subclasses_recursively(cls, sub_cls)


import_recursive(sys.modules[__name__])

model_layer_subcls = set()
find_subclasses_recursively(layers.ModelLayer, model_layer_subcls)

for cls in list(model_layer_subcls):
    layers.register_layer(cls.__name__, cls)

Allow to import subclasses of layers
Allow to import subclasses of layers

Summary:
We want it to be able to register children of layers who
are not direct children of ModelLayer.
This requires us to find subclasses of ModelLayer recursively.

Reviewed By: kittipatv, kennyhorror

Differential Revision: D5397120

fbshipit-source-id: cb1e03d72e3bedb960b1b865877a76e413218a71

  from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  from __future__ import unicode_literals
  
  from importlib import import_module
  import pkgutil
  import sys
- import inspect
  from . import layers
  
  
- def import_recursive(package, clsmembers):
+ def import_recursive(package):
      """
      Takes a package and imports all modules underneath it
      """
  
      pkg_dir = package.__path__
      module_location = package.__name__
      for (_module_loader, name, ispkg) in pkgutil.iter_modules(pkg_dir):
          module_name = "{}.{}".format(module_location, name)  # Module/package
          module = import_module(module_name)
-         clsmembers += [cls[1] for cls in inspect.getmembers(module, inspect.isclass)]
          if ispkg:
-             import_recursive(module, clsmembers)
+             import_recursive(module)
  
  
- clsmembers = []
- import_recursive(sys.modules[__name__], clsmembers)
+ def find_subclasses_recursively(base_cls, sub_cls):
+     cur_sub_cls = base_cls.__subclasses__()
+     sub_cls.update(cur_sub_cls)
+     for cls in cur_sub_cls:
+         find_subclasses_recursively(cls, sub_cls)
  
- for cls in clsmembers:
-     if issubclass(cls, layers.ModelLayer) and cls is not layers.ModelLayer:
-         layers.register_layer(cls.__name__, cls)
  
+ import_recursive(sys.modules[__name__])
+ 
+ model_layer_subcls = set()
+ find_subclasses_recursively(layers.ModelLayer, model_layer_subcls)
+ 
+ for cls in list(model_layer_subcls):
+     layers.register_layer(cls.__name__, cls)
+ 
Allow to import subclasses of layers
## Code Before:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from importlib import import_module
import pkgutil
import sys
import inspect
from . import layers


def import_recursive(package, clsmembers):
    """
    Takes a package and imports all modules underneath it
    """

    pkg_dir = package.__path__
    module_location = package.__name__
    for (_module_loader, name, ispkg) in pkgutil.iter_modules(pkg_dir):
        module_name = "{}.{}".format(module_location, name)  # Module/package
        module = import_module(module_name)
        clsmembers += [cls[1] for cls in inspect.getmembers(module, inspect.isclass)]
        if ispkg:
            import_recursive(module, clsmembers)


clsmembers = []
import_recursive(sys.modules[__name__], clsmembers)

for cls in clsmembers:
    if issubclass(cls, layers.ModelLayer) and cls is not layers.ModelLayer:
        layers.register_layer(cls.__name__, cls)

## Instruction:
Allow to import subclasses of layers
## Code After:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from importlib import import_module
import pkgutil
import sys
from . import layers


def import_recursive(package):
    """
    Takes a package and imports all modules underneath it
    """

    pkg_dir = package.__path__
    module_location = package.__name__
    for (_module_loader, name, ispkg) in pkgutil.iter_modules(pkg_dir):
        module_name = "{}.{}".format(module_location, name)  # Module/package
        module = import_module(module_name)
        if ispkg:
            import_recursive(module)


def find_subclasses_recursively(base_cls, sub_cls):
    cur_sub_cls = base_cls.__subclasses__()
    sub_cls.update(cur_sub_cls)
    for cls in cur_sub_cls:
        find_subclasses_recursively(cls, sub_cls)


import_recursive(sys.modules[__name__])

model_layer_subcls = set()
find_subclasses_recursively(layers.ModelLayer, model_layer_subcls)

for cls in list(model_layer_subcls):
    layers.register_layer(cls.__name__, cls)
