Source

imalse / util / util.py

"""This file contains general utility functions."""

from inspect import getmembers
import os
import importlib
import settings

def abstract_method():
    """ 
    
    This should be called when an abstract method is called that should have been
    implemented by a subclass. It should not be called in situations where no implementation
    (i.e. a 'pass' behavior) is acceptable. 
    
    """    
    raise NotImplementedError('Method not implemented!')

class DataEndException(Exception):
    """Custom exception."""
    pass

def load_scenario(scenario):    
    """Load the module of a specific scenario."""    
    try:
        return importlib.import_module('scenario.' + scenario)
    except ImportError as e:
        print e
        print "Scenario " + scenario + " could not be loaded."
        exit(0)

def load_experiment(experiment):
    """Load the module of a specific experiment."""
    try:
        return importlib.import_module('experiments.' + experiment)
    except ImportError as e:
        print e
        print "Experiment " + experiment +  " could not be loaded."
        exit(0)

def get_availabe_scenarios(scen_dir = settings.ROOT+'/scenario/'):
    """Search and return an array of available scenarios."""
    found_scenarios = []
    
    for f_name in os.listdir(scen_dir):
        if not ( f_name.lower().endswith('py') or f_name.lower().startswith('.')
                 or f_name.lower().endswith('pyc')):
            found_scenarios.append(f_name)
    
    return found_scenarios

def get_available_experiments(scen_dir = settings.ROOT+'/experiments/'):
    """Search and return an array of available experiments."""
    found_experiments = []
    
    for f_name in os.listdir(scen_dir):
        if f_name.endswith('exp.py'):
            found_experiments.append(f_name[:-3])
    
    return found_experiments

def print_members(cls):
    """For debug, print the member function of a class"""
    values = getmembers(cls)
    for val in values:
        print val[0]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.