예제 #1
0
    def execute(self, kernel="python3"):  # pylint: disable=method-hidden
        """
        Execute the notebook and create the ouptut file. 
        """
        executed = True

        try:
            nb_path = os.path.split(self.notebook_path)[0] + '/'
            ep = ExecutePreprocessor(timeout=600, kernel_name=kernel)
            retval = ep.preprocess(self.notebook,
                                   {'metadata': {
                                       'path': nb_path
                                   }})

            for item in retval:
                if isinstance(item, nbformat.notebooknode.NotebookNode):
                    output_path = self.notebook_path.replace(
                        '.ipynb', '_output.ipynb')
                    with open(output_path, mode="w", encoding="utf-8") as f:
                        nbformat.write(item, f)

        except CellExecutionError as ex:
            executed = False
            Logger.add_log("CellExecutionError in notebook({}):".format(
                self.notebook_path))
            Logger.add_log(str(ex))

        return executed
예제 #2
0
    def __call__(self, *args, **kwargs):

        # Funciton return value
        return_value = None

        # Start time
        function_start = datetime.now()

        # Base message
        spacer = '\t' * 8
        out_message_base = "Module: {} - Function: {} ".format(
            self.function.__module__, self.function.__name__)

        out_message_base += "\n{}ARGUMENTS: {}".format(spacer, args)

        try:
            # Execute funciton, if exception log it
            return_value = self.function(*args, **kwargs)
        except Exception as ex:
            out_message_base += "\n{}EXCEPTION: {}".format(spacer, str(ex))

        # Add function return
        out_message_base += "\n{}RETURNS: {}".format(spacer, return_value)

        # Add clock to function
        span = datetime.now() - function_start
        out_message_base += "\n{}EXECUTION: {}".format(spacer, str(span))

        # Finally log it and return the function return value
        Logger.add_log(out_message_base)

        return return_value
예제 #3
0
    def load_configuration(self, config): # pylint: disable=method-hidden
        """
        Load configuration with tags and replacement formats
        """
        config_content = None

        with open(config,"r") as f:
            config_content = f.readlines()
            config_content = "\n".join(config_content)

        if config_content:
            config_content = json.loads(config_content)

            self.notebooks.extend(config_content['notebooks'])

            for cf in config_content['replacements']:
                self.tags[cf['tag']] = cf['values']
    
        Logger.add_log("Notebooks: {} ".format(self.notebooks))
        Logger.add_log("Tags: {} ".format(self.tags))
예제 #4
0
class Grid(object):
    def __init__(self, size):
        self.size = size
        self.cells = self.empty()
        self.logger = Logger()

    def empty(self):
        return [[None] * self.size for _ in range(self.size)]

    def from_state(self, state):
        """ in case of using saved state of game
            currently function is redundant
        """
        cells = self.empty()
        for x in range(self.size):
            for y in range(self.size):
                tile = state[x][y]
                cells[x][y] = Tile(tile.pos, tile.value) if tile else None
        return cells

    def random_available_cell(self):
        cells = self.available_cells()
        if len(cells):
            return cells[floor(random() * len(cells))]

    def available_cells(self):
        cells = []
        self.for_each_cell(lambda x, y, tile:
                           cells.append(Vector(x, y)) if not tile else None)
        return cells

    def for_each_cell(self, callback):
        for x in range(self.size):
            for y in range(self.size):
                callback(x, y, self.cells[x][y])

    def cells_available(self, cell=None):
        if cell is None:
            return bool(len(self.available_cells()))
        else:
            return not self.cell_occupied(cell)

    def cell_occupied(self, cell):
        return bool(self.cell_content(cell))

    def cell_content(self, cell):
        if self.within_bounds(cell):
            return self.cells[cell.x][cell.y]
        else:
            return None

    def insert_tile(self, tile):
        self.logger.add_log('+ insert {}'.format(tile))
        self.cells[tile.pos.x][tile.pos.y] = tile

    def remove_tile(self, tile):
        self.logger.add_log('- remove {}'.format(tile))
        self.cells[tile.pos.x][tile.pos.y] = None

    def within_bounds(self, position):
        return (0 <= position.x < self.size and
                0 <= position.y < self.size)
예제 #5
0
    def update_notebook(self, notebook_tags):  # pylint: disable=method-hidden
        """
        If a notebook has tags and that tag is listed in the configuraiton, 
        split all lines with '=' in it into right/left. 

        If right is in the replacement names replace left with the value from
        the configuraiton. 

        Then write out the source cell to the notebook again. 
        """
        modification_count = 0

        if self.notebook["cells"] and notebook_tags:
            """
            Filter for cells that :
                1. Are code cells
                2. Have metadata
                3. Have 'tags' field in metadata 
            """
            code_cells = [
                x for x in self.notebook['cells'] if x['cell_type'] == 'code'
                and len(x['metadata']) and 'tags' in x['metadata']
            ]

            # All of these we know have tags associated
            for tag in notebook_tags.keys():
                # Go through our tags to see if it applies
                current_cells = [
                    x for x in code_cells if tag in x['metadata']['tags']
                ]

                for current in current_cells:
                    # It's one we are after, now go through the source and parse out
                    # formats of XXX = YYYY and if present, we have to update it.
                    raw_source = self._split_source(current['source'])

                    # If there is only one, it comes back as a string not a list
                    if isinstance(raw_source, str):
                        raw_source = [raw_source]

                    modified = False
                    for line_idx in range(len(raw_source)):
                        # Single line
                        line = raw_source[line_idx]
                        # Split left=right
                        res = self._split_source_line(line)
                        # Adjust if neccesary
                        if len(res) == 2:
                            replacement = None if res[0] not in notebook_tags[
                                tag] else notebook_tags[tag][res[0]]
                            if replacement:
                                modification_count += 1
                                updated = "{}={}".format(
                                    res[0],
                                    '"{}"'.format(replacement) if isinstance(
                                        replacement, str) else replacement)
                                Logger.add_log("UPDATE {} [{}] = [{}]".format(
                                    self.notebook_path, raw_source[line_idx],
                                    updated))
                                raw_source[line_idx] = updated
                                modified = True

                    if modified:
                        # If we've modified anything, update the source
                        current['source'] = "\n".join(raw_source)

        return modification_count
예제 #6
0
from utils import Logger, FunctionTrace, Configuration, NotebookUtil

# Load configuration
config = Configuration()
config.load_configuration("./testconfig.json")

if not len(config.notebooks):
    Logger.add_log("No notebooks identified in configuration")
    quit()

if not len(config.tags):
    Logger.add_log("No tags identified in configuration")
    quit()

# Execute each notebook with replacements as needed
for notebook_path in config.notebooks:
    try:
        print("Processing {}".format(notebook_path))
        nb_util = NotebookUtil(notebook_path)
        nb_util.update_notebook(config.tags)
        success = nb_util.execute()
        if not success:
            print("Notebook {} failed to execute".format(notebook_path))
            break
        print("Notebook execution complete")
    except Exception as ex:
        message = "{} failed with {}".format(notebook_path, str(ex))
        Logger.add_log(message)
        print(message)
        break